题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int dp[10005];
int v,n;
void ZeroOnePack(int cost,int weight)
{
for(int j=v; j>=cost; j--)
dp[j]=max(dp[j],dp[j-cost]+weight);
}
void CompletePack(int cost,int weight)
{
for(int j=cost; j<=v; j++)
dp[j]=max(dp[j],dp[j-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount)
{
if(cost*amount>=v)
CompletePack(cost,weight);
else
{
int k=1;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount-=k;
k+=k;
}
ZeroOnePack(amount*cost,amount*weight);
}
}
int main()
{
int c;
scanf("%d",&c);
while(c--)
{
memset(dp,0,sizeof(dp));
scanf("%d%d",&v,&n);
int cost[1005],weight[1005],num[1005];
for(int i=0; i<n; i++)
scanf("%d%d%d",&cost[i],&weight[i],&num[i]);
for(int i=0; i<n; i++)
MultiplePack(cost[i],weight[i],num[i]);
printf("%d\n",dp[v]);
}
return 0;
}