Description
Recently, ISea went to an ancient country. For such a long time, it is the most wealthy and powerful kingdom in the world. As a result, the people in this country is still very proud even if their nation hasn ' t been so wealthy any more.
The merchants were the most typical, each of the them only sold exactly one item, the price is Pi, but they would refuse to M Ake a trade with the 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?
Input
There is several test cases in the input.
Each test case begin with the integers N, M (1≤n≤500, 1≤m≤5000), indicating the items ' number and the initial mone Y.
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1≤pi≤qi≤100, 1≤vi≤1000), their meaning is I n the description.
The input terminates by end of file marker.
Output
For each test case, output one integer, indicating maximum value ISea could get.
Sample Input
2 1010 15 105 10 53 105 10 53 5 62 7 3
Sample Output
511 Main idea: Take into account the order to be sorted. Sort according to Q-p, from small to large, q-p that the least amount of money needed for the following item, greedy thoughts, and then 01 backpacks
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intdp[5500];intn,m;structedge{intp; intQ; intv;} a[5500];BOOLCMP (Edge I, Edge j) {returnI.Q-I.P <J.Q-J.P;}intMain () { while(~SCANF ("%d%d",&n,&m)) {memset (DP,0,sizeof(DP)); for(inti =1; I <= N; i++) scanf ("%d%d%d",&a[i].p,&a[i].q,&a[i].v); Sort (a+1, a+n+1, CMP); for(inti =1; I <= N; i++){ for(intj = m; J >= A[i].q; j--) {Dp[j]= Max (dp[j],dp[j-a[i].p]+a[i].v); }} printf ("%d\n", Dp[m]); } return 0;}
View Code
hdu3466--Backpack Dp--proud Merchants