悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h>
#include<string.h>
int f[1000];
int p[1000],w[1000],num[1000];
int V;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
void ZeroOnePack(int cost,int weight)
{
int i;
for(i=V;i>=cost;i--)
f[i]=max(f[i],f[i-cost]+weight);
}
void CompletePack(int cost,int weight)
{
int i;
for(i=cost;i<=V;i++)
f[i]=max(f[i],f[i-cost]+weight);
}
void MultiplePack(int cost,int weight,int count)
{
if(count*cost>=V)
CompletePack(cost,weight);
else
{
int i=1;
while(i<count)
{
ZeroOnePack(cost*i,weight*i);
count=count-i;
i=i*2;
}
ZeroOnePack(cost*count,weight*count);
}
}
int main()
{
int i,t,m;
scanf("%d",&t);
while(t--)
{
memset(f,0,sizeof(f));
scanf("%d%d",&V,&m);
for(i=1;i<=m;i++)
scanf("%d%d%d",&p[i],&w[i],&num[i]);
for(i=1;i<=m;i++)
MultiplePack(p[i],w[i],num[i]);
printf("%d\n",f[V]);
}
return 0;
}