#include<iostream> #include<string.h> using namespace std; int dp[5010]; struct node{ int ss,vv,cc; }q[5010]; int max1(int a,int b) { if(a>b) return a; return b; } int cmp(node a,node b) { return (a.ss-a.cc)<(b.ss-b.cc); } int main() { int i,j,n,m,k; int c,v; while(scanf("%d%d",&n,&m)!=EOF) { memset(dp,0,sizeof(dp)); memset(q,0,sizeof(q)); for(i=0;i<n;i++) { scanf("%d%d%d",&q[i].cc,&q[i].ss,&q[i].vv); } sort(q,q+n,cmp); for(i=0;i<n;i++) { for(j=m;j>=q[i].ss;j--) { dp[j]=max1(dp[j],dp[j-q[i].cc]+q[i].vv); } } printf("%d\n",dp[m]); }return 0; }
原文地址:http://blog.csdn.net/l133236116/article/details/42643417