Test instructions: There are n kinds of goods, there is a candy in each commodity, if buy this kind of goods to send more B candy, only the first time to buy the time to send. Now there are M yuan, how many sweets can I buy?
Idea: to buy a product for the first time to send candy, for this time to carry out a 01 backpack, that is, can only buy once. Then the product to a complete backpack, at this time do not send candy, can also buy more.
1#include <bits/stdc++.h>2 #definePII pair<int,int>3 #defineINF 0x7f7f7f7f4 #defineLL Long Long5 using namespacestd;6 Const intn=2050;7 LL Dp[n];8 intN, M, W[n], a[n], b[n];9 Ten LL cal () One { AMemset (DP,0,sizeof(DP)); - for(intI=1; i<=n; i++)//Gifts - { the for(intJ=m; j-w[i]>=0; j--)//first one, 01 backpacks. - { - if(dp[j-w[i]]+ A[i] + b[i] >Dp[j]) -dp[j]=dp[j-w[i]]+ A[i] +B[i]; + } - for(intj=0; j+w[i]<=m; J + +)//one more full backpack. + { A if(Dp[j+w[i]] < dp[j]+A[i]) atdp[J+w[i]] = dp[j]+A[i]; - } - } -LL sum=-1; - for(intI=m; I>0; i--) sum=max (sum, dp[i]); - returnsum; in } - to intMain () + { -Freopen ("Input.txt","R", stdin); the intT; *Cin>>T; $ while(t--)Panax Notoginseng { -scanf"%d%d", &m, &n); the for(intI=1; i<=n; i++) scanf ("%d%d%d", &w[i], &a[i], &b[i]); +printf"%lld\n", Cal ()); A } the + return 0; -}
AC Code
HDU 5410 CRB and his Birthday (01 backpack, full backpack, mixed)