2014
03-30

# Proud Merchants

Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more.
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?

There are several test cases in the input.

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.

There are several test cases in the input.

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.

2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3

5
11

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>

using namespace std;

#pragma pack(4)

const double  eps = 1e-8;
const double   pi = acos(-1.0);
const int     inf = 0x7f7f7f7f;

#define loop(a,n)                            \
for(int i=0;n>i;i++)                     \
cout<<a[i]<<(i!=n-1?' ':'\n')
#define loop2(a,n,m)                         \
for(int i=0;n>i;i++)                     \
for(int j=0;m>j;j++)                 \
cout<<a[i][j]<<(j!=m-1?' ':'\n')

#define   at(a,i) ((a)&(1<<(i)))
#define   nt(a,i) ((a)^(1<<(i)))
#define set1(a,i) ((a)|(1<<(i)))
#define set0(a,i) ((a)&(~(1<<(i))))

#define cmp(a,b) (fabs((a)-(b))<eps?0:(((a)-(b))>eps?+1:-1))

#define lmax(a,b) ((a)>(b)?(a):(b))
#define lmin(a,b) ((a)<(b)?(a):(b))
#define fmax(a,b) (cmp(a,b)>0?(a):(b))
#define fmin(a,b) (cmp(a,b)<0?(a):(b))

const int MAXV = 5002;

struct node
{
int p,q,v;
bool operator < (node argu) const
{
return q-p>argu.q-argu.p;
}
}a[502];

int n,m,dp[MAXV];

int main()
{
#ifndef ONLINE_JUDGE
freopen("Proud Merchants.txt","r",stdin);
#else
#endif

while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=0;n>i;i++)
{
scanf("%d %d %d",&a[i].p,&a[i].q,&a[i].v);
}
sort(a,a+n);
memset(dp,-1,sizeof(dp)); dp[m]=0;
int ans=0;
for(int i=0;n>i;i++)
{
for(int j=max(0,a[i].q-a[i].p);m>=j+a[i].p;j++)
{
if(dp[j+a[i].p]!=-1)
{
dp[j]=max(dp[j],dp[j+a[i].p]+a[i].v);
ans=max(ans,dp[j]);
}
}
}
printf("%d\n",ans);
}

return 0;
}