SuperSale
There is a SuperSale in a SuperHiperMarket. Every person can take only one object of each kind, i.e. one TV, one carrot, but for extra low price. We are going with a whole family to that SuperHiperMarket. Every person can take as many objects, as he/she can carry out from the SuperSale. We have given list of objects with prices and their weight. We also know, what is the maximum weight that every person can stand. What is the maximal value of objects we can buy at SuperSale?
Input Specification
The input consists of T test cases. The number of them (1<=T<=1000) is given on the first line of the input file.
Each test case begins with a line containing a single integer number N that indicates the number of objects (1 <= N <= 1000). Then follows Nlines, each containing two integers: P and W. The first integer (1<=P<=100) corresponds to the price of object. The second integer (1<=W<=30) corresponds to the weight of object. Next line contains one integer (1<=G<=100) it’s the number of people in our group. Next G lines contains maximal weight (1<=MW<=30) that can stand this i-th person from our family (1<=i<=G).
Output Specification
For every test case your program has to determine one integer. Print out the maximal value of goods which we can buy with that family.
Sample Input
2
3
72 17
44 23
31 24
1
26
6
64 26
85 22
52 4
99 18
39 13
54 9
4
23
20
20
26
Output for the Sample Input
72
514
01背包问题,唯一有点变形的就是这道题中是一群人组团去的,只要对每个人做01背包的动态规划,最后再求和即可
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,g,mw; 8 int p[1050],w[1050]; 9 int dp[35]; 10 11 int main() 12 { 13 int kase; 14 15 scanf("%d",&kase); 16 17 while(kase--) 18 { 19 scanf("%d",&n); 20 for(int i=0;i<n;i++) 21 scanf("%d %d",&p[i],&w[i]); 22 scanf("%d",&g); 23 24 int total=0; 25 for(int k=0;k<g;k++) 26 { 27 scanf("%d",&mw); 28 memset(dp,0,sizeof(dp)); 29 for(int i=0;i<n;i++) 30 for(int j=mw;j>=w[i];j--) 31 if(dp[j-w[i]]+p[i]>dp[j]) 32 dp[j]=dp[j-w[i]]+p[i]; 33 int Max=-1; 34 for(int i=0;i<=mw;i++) 35 if(dp[i]>Max) 36 Max=dp[i]; 37 total+=Max; 38 } 39 40 printf("%d\n",total); 41 } 42 43 return 0; 44 }