P1060 happy Jin Ming, P1060 happy Jin Ming
Description
Jin Ming was very happy today. The new house purchased at home had the key. There was a very spacious room dedicated to him in the new house. Even more pleased, his mother said to him yesterday: "You have the final say about the items you need to purchase and how to arrange in your room, as long as the price does not exceed N yuan ". Jin Ming started to make a budget early this morning, but he wants to buy too many things and will definitely exceed his mother's limit of N yuan. Therefore, he defined an importance for each item and divided it into five equal values: an integer of 1 ~ 5 indicates that 5th is the most important. He also found the price of each item on the Internet (all in integer yuan ). He hopes that the sum of the product of the price and importance of each item will be maximized without exceeding N yuan (which can be equal to N yuan.
Set the price of item j to v [j], and the importance to w [j]. k items are selected, numbered j1, j2 ,......, Jk, then the sum is:
V [j1] * w [j1] + v [j2] * w [j2] +... + V [jk] * w [jk]. (Where * is the multiplication number)
Please help Jin Ming design a shopping order that meets requirements.
Input/Output Format
Input Format:
The input row 1st is two positive integers separated by a space:
N m (N (<30000) indicates the total amount of money, and m (<25) indicates the number of items to be purchased .)
From row 2nd to row m + 1, row j gives the basic data of the item numbered J-1, each row has 2 non-negative integers
V p (where v indicates the price of the item (v <= 10000), and p indicates the importance of the item (1 ~ 5 ))
Output Format:
The output has only one positive integer, which is the maximum value (<100000000) of the sum of the product of price and importance of an item that does not exceed the total amount of money ).
Input and Output sample
Input example #1:
1000 5800 2400 5300 5400 3200 2
Output sample #1:
3900
Description
Topic 2 of NOIP 2006 popularization Group
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=100001; 7 struct node 8 { 9 int w;10 int v;11 }a[MAXN];12 int dp[101][30001];13 int main()14 {15 int maxn,n;16 scanf("%d%d",&maxn,&n);17 for(int i=1;i<=n;i++)18 scanf("%d%d",&a[i].w,&a[i].v);19 for(int i=1;i<=n;i++)20 {21 for(int j=1;j<=maxn;j++)22 {23 if(j>=a[i].w)24 dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i].w]+a[i].v*a[i].w);25 else26 dp[i][j]=dp[i-1][j];27 }28 }29 printf("%d",dp[n][maxn]);30 return 0;31 }