hdu3466--Backpack Dp--proud Merchants

Source: Internet
Author: User

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 &LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.