因为剩余金额会对状态产生影响,所以应该先处理p-q较小的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 501
#define M 5001
typedef struct _node{
int p, q, v;
}node;
node s[N];
int comp(const void *a, const void *b){
int m = ((node*)a)->q - ((node*)a)->p;
int n = ((node*)b)->q - ((node*)b)->p;
return n - m;
}
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int f[M];
int main()
{
int n, m, p, q, v, i, j, ans;
while(scanf("%d%d", &n, &m) != EOF){
memset(f, 0, sizeof(f));
for(i=0; i<n; i++){
scanf("%d%d%d", &s[i].p, &s[i].q, &s[i].v);
}
qsort(s, n, sizeof(node), comp);
for(i=0; i<n; i++)
for(j=m+s[i].p-s[i].q; j>=s[i].p; j--)
/* for(j=p; j<m+p-q; j++)*/
f[j] = max(f[j-s[i].p]+s[i].v, f[j]);
ans = 0;
for(i=0; i<=m; i++)
ans = max(ans, f[i]);
printf("%d\n", ans);
}
return 0;
}