1 8 2 2 100 4 4 100 2
400
拿来试模板的题目。
#include<cstdio> #include<cstring> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) int n,m; int p[105],h[105],c[105]; int dp[105]; void ZeroOnePack(int cost, int weight) { for (int i = n; i >= cost; --i) dp[i] = max(dp[i], dp[i-cost] + weight); } void CompletePack(int cost, int weight) { for (int i = cost; i <= n; ++i) dp[i] = max(dp[i], dp[i-cost] + weight); } void MultiplePack(int cost, int weight, int amount) { if (cost * amount >= n) CompletePack(cost, weight); else { int k = 1;//二进制优化 for(;k < amount;) { ZeroOnePack(k * cost, k * weight); amount -= k; k *= 2; } ZeroOnePack(amount * cost, amount * weight); } } int main() { int cas; scanf("%d",&cas); for(; cas--;) { scanf("%d%d",&n,&m); for(int i=1; i<=m; ++i) scanf("%d%d%d",p+i,h+i,c+i); memset(dp,0,sizeof(dp)); for(int i=1;i<=m;++i) MultiplePack(p[i],h[i],c[i]); printf("%d\n",dp[n]); } return 0; }