Here 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 example 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 pe Rson can stand. What is the maximal value of objects we can buy at SuperSale? Input SpecificationThe 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 intege R (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 SpecificationFor 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 Input2372 1744 2331 24126664 2685 2252 499 1839 1354 Output for the Sample Input72514 question: I am buying g products. Everyone has a certain weight of mw and n products, each item has a value of p and weight of w. All users are required to purchase each item only once, and the total value of the last purchase is the largest. Idea: 01 backpack problem. The state transition equation is dp [j] = max (dp [j-w [I] + p [I], dp [j]). Code:
#include <stdio.h>#include <string.h>int t, n, p[1005], w[1005], g, mw, i, j, dp[30005], sum;int max(int a, int b) {return a > b ? a : b;}int main() {scanf("%d", &t);while (t --) {sum = 0;int s = 0;scanf("%d", &n);for (i = 0; i < n; i ++) {scanf("%d%d", &p[i], &w[i]);s += w[i];}memset(dp, 0, sizeof(dp));for (i = 0; i < n; i ++)for (j = s; j >= w[i]; j --) {if (dp[j - w[i]] || j - w[i] == 0)dp[j] = max(dp[j - w[i]] + p[i], dp[j]);}scanf("%d", &g);while (g --) {int Max = 0;scanf("%d", &mw);for (i = 0; i <= mw; i ++)Max = max(Max, dp[i]);sum += Max;}printf("%d\n", sum);}return 0;}