又开始做背包题了,这题典型的多重背包,不过在进行背包处理之前需要先进行一下排序(限制大的在限制小的之前排肯定不合适),不然dp的结果会出现错误。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 410;
struct node{
int num, hig, lim;
friend bool operator <(node a,node b)
{
return a.lim < b.lim;
}
};
int k;
node block[N];
bool dp[40010];
int main(void)
{
cin>>k;
for(int i=1; i<=k; ++i)
scanf("%d%d%d", &block[i].hig, &block[i].lim, &block[i].num);
sort(block+1, block+1+k);
memset(dp, false, sizeof(dp));
dp[0] = true;
for(int i=1; i<=k; ++i)
{
int limit = block[i].lim;
int num = block[i].num;
int high = block[i].hig;
int cnt = 1;
while(cnt < num)
{
int tmp = cnt * high;
for(int j=limit; j>=tmp; --j)
if(dp[j-tmp])
dp[j] = true;
num -= cnt;
cnt += cnt;
}
if(num)
{
int tmp = num * high;
for(int j=limit; j>=tmp; --j)
if(dp[j-tmp])
dp[j] = true;
}
}
int ans = -1;
for(int i=40000; i>=0; --i)
if(dp[i])
{
ans = i;
break;
}
cout<<ans<<endl;
return 0;
}