以最低价格要求与实际价格的差值作为关键字进行排序,然后当作01背包处理即可
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=501;
struct node
{
int p;
int q;
int v;
bool operator < (const node &a)const
{
return q-p<a.q-a.p;
}
}a[maxn];
int n,m,dp[maxn*10];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);
sort(a,a+n);
int ans=0;
for(int i=0;i<n;i++)
for(int j=m;j>=a[i].q;j--)
{
dp[j]=max(dp[j-a[i].p]+a[i].v,dp[j]);
ans=max(ans,dp[j]);
}
printf("%d\n",ans);
}
return 0;
}