Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3126 Accepted Submission(s): 1288
The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi.
If he had M units of money, what’s the maximum value iSea could get?
Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money.
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description.
The input terminates by end of file marker.
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[5005];
int p[505],q[505],c[505];
int n,m;
int pd1(int p[],int q[],int c[])
{
int temp;
for(int i=1;i<=n-1;i++)
for(int j=i+1;j<=n;j++)
{
if(q[i]-p[i]>q[j]-p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=q[i];
q[i]=q[j];
q[j]=temp;
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(p,0,sizeof(p));
memset(q,0,sizeof(q));
memset(c,0,sizeof(c));
memset(dp,0,sizeof(dp));
for(int k=1;k<=n;k++)
scanf("%d%d%d",&p[k],&q[k],&c[k]);
pd1(p,q,c);
for(int i=1;i<=n;i++)
for(int j=m;j>=q[i];j--)
dp[j]=max(dp[j],dp[j-p[i]]+c[i]);
printf("%d\n",dp[m]);
}
return 0;
}