Standard multi-backpack application, as long as you read the nine articles about the backpack, you should have done the question;
It is easier to use structs for comparison;
This problem is quite painful because the number is weighted in the middle and cannot be found after half a day. After reading PO3 several times, I still did not find it... with a strange heart, I searched for it. i'm going...
Alas. Pay attention to the habit of writing code later...
1 #include<iostream> 2 #define maxn 105 3 #define max(a,b) (a>b?a:b) 4 int dp[maxn]; 5 int n,m,t; 6 struct node 7 { 8 int amount,weight,price; 9 }game[maxn];10 void zeroOnePack(int cost,int weight)11 {12 for(int i=n;i>=cost;i--)13 dp[i]=max(dp[i],dp[i-cost]+weight);14 }15 void completePack(int cost ,int weight)16 {17 for(int i=cost;i<=n;i++)18 dp[i]=max(dp[i],dp[i-cost]+weight);19 }20 void multiPack(int cost,int weight,int amount)21 {22 23 if(cost * amount >= n)24 completePack(cost,weight);25 else26 {27 int k=1;28 while( k< amount)29 {30 zeroOnePack(k*cost,k*weight);31 amount-=k;32 k*=2;33 }34 zeroOnePack(amount*cost,amount*weight);35 }36 }37 38 int main()39 {40 //freopen("2191.txt","r",stdin);41 int i,j;42 while(~scanf("%d",&t))43 {44 while(t--)45 {46 memset(dp,0,sizeof(dp));47 memset(game,0,sizeof(game));48 scanf("%d %d" ,&n,&m);49 for(i=0;i<m;i++)50 scanf("%d %d %d",&game[i].price,&game[i].weight,&game[i].amount);51 for(i=0;i<m;i++)52 multiPack(game[i].price,game[i].weight,game[i].amount);53 printf("%d\n",dp[n]);54 }55 }56 return 0;57 }