#1043: Full Backpack Time limit: 20000ms single point time limit: 1000ms memory limit: 256MB description
Say then before the story, small hi and small ho struggled to finally get the vast number of lottery tickets! And now, it's time for Little Ho to pick up the reward!
Wait, why is this story familiar? This is going to start with the parallel universe theory ... All in all, in another universe, Little Ho's problems have changed slightly!
Small Ho now has M-ticket, and the prize area has n prizes, respectively, marking 1 to N, where the first prize of the need (i) to redeem the lottery, and can be redeemed countless times , in order to make the hard-won lottery is not wasted, small ho to each of the prizes are assessed points, The score value of the prize for the I-item is value (i), which indicates his preference for the prize. Now he wants to know which prizes can be changed by virtue of the lotteries in his hand, so that the sum of the prizes ' preferences can be maximized.
Tip One: Cut, is not 0~1 into a 0~k?
Tip Two: OCD patients will always optimize the state transfer equation over and over again
Tip Three: Also do not forget to optimize the space Oh!
Input
Each test point (input file) has and has only one set of test data.
The first act of each set of test data is two positive integers n and m, indicating the number of prizes, and the number of lotteries in small Ho's hands.
The next n lines describe each line describing a prize, where I behaves two integers need (i) and value (i), meaning as described earlier.
Test Data Assurance
For 100% of data, the value of n does not exceed the value of 500,m no more than 10^5
For 100% of data, need (i) does not exceed 2*10^5, value (i) does not exceed 10^3
Output
For each set of test data, output an integer ans that represents the total preference value that small ho can get.
-
-
Sample input
-
-
5 1000144 990487 436210 673567 581056 897
-
-
Sample output
-
5940
Last night, I finally know what it means to be 1 of 0-1 backpacks. zjgsu611 Youdao 0-n backpack, is the same routine
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<string>#include<stack>#include<queue>#include<algorithm>Const intINF =0x3f3f3f;Const intMAXN = 5e2+Ten;Const intMMAXN = 2e5+Ten;using namespacestd;intW[MAXN];intA[MAXN];intDP[MMAXN];intMain () {intn,m; while(SCANF ("%d%d", &n,&m)! =EOF) {memset (DP,0,sizeof(DP)); for(intI=0; i<n;i++) {scanf ("%d%d",&w[i],&A[i]); } for(intI=0; i<n;i++){ for(intj=1; j*w[i]<=m;j++){ for(intk=m;k>=j*w[i];k--) {Dp[k]= Max (dp[k],dp[k-j*w[i]]+j*A[i]); } }} cout<<dp[m]<<Endl; } //cout << "Hello world!" << Endl; return 0;}
View Code
hiho1433 0-k Backpack