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?

Input

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.

Output

For each test case, output one integer, indicating maximum value iSea could get.

Sample Input

2 10

10 15 10

5 10 5

3 10

5 10 5

3 5 6

2 7 3

Sample Output

5

11

//很明显这一道题是用01背包写,但是又和普通的01背包不一样,因为这一道题多了一个q的限制

//如果一个物品是5 9,一个物品是5 6,
//对第一个进行背包的时候只有dp[9],dp[10],…,dp[m],
//再对第二个进行背包的时候,如果是普通的,应该会借用前面的dp[8],dp[7]之类的,
//但是现在这些值都是0,所以会导致结果出错.
//(重点):于是要想到只有***后面要用的值前面都可以得到***,那么才不会出错 //也就是说有两个物品A(p1,q1)和B(p2,q2)如果你想进行背包,则在这一题中必须要满足把p1实际的大小装下
//还要满足剩下的空间够把q2装下,在进行背包的顺序中先A后B为: p1+q2,先B后A为: p2+q1,
//则p1+q2>p2+q1,即q1-p1<q2-p2
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const int INF=1e9+7;
const int maxn=5100;
typedef long long ll; int n,m;
int dp[maxn];
struct node
{
int p,q,v,t;
} a[maxn]; int cmp(node A,node B)
{
return A.t<B.t;
} int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
int i,j;
memset(a,0,sizeof(a));
memset(dp,0,sizeof(dp));
for(i=0; i<n; i++)
{
scanf("%d %d %d",&a[i].p,&a[i].q,&a[i].v);
a[i].t=a[i].q-a[i].p;
}
sort(a,a+n,cmp);
for(i=0; i<n; i++)
{
for(j=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;
}

Proud Merchants HDU - 3466 (思路题--有排序的01背包)的更多相关文章

  1. Re0:DP学习之路 Proud Merchants HDU - 3466

    解法 排序+01背包 这里的排序规则用q-p升序排列这里是一个感觉是一个贪心的策略,为什么这样做目前也无法有效的证明或者说出来 然后就是01背包加了一个体积必须大于什么值可以装那么加一个max(p,q ...

  2. Proud Merchants HDU - 3466 01背包&&贪心

    最近,我去了一个古老的国家.在很长一段时间里,它是世界上最富有.最强大的王国.结果,这个国家的人民仍然非常自豪,即使他们的国家不再那么富有.商人是最典型的,他们每个人只卖一件商品,价格是Pi,但是如果 ...

  3. HDU 2639 骨头收集者 II【01背包 】+【第K优决策】

    题目链接:https://vjudge.net/contest/103424#problem/H 题目大意:与01背包模板题类似,只不过要我们求第K个最大的总价值. 解题分析: 其基本思想是将每个状态 ...

  4. hdu 3466 Proud Merchants(有排序的01背包)

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  5. HDU 1203 I NEED A OFFER!(01 背包DP)

    点我看题目 题意 : 中文题不详述. 思路 :类似于01背包的DP,就是放与不放的问题,不过这个要求概率,至少得到一份offer的反面就是一份也得不到,所以先求一份也得不到的概率,用1减掉就可以得到所 ...

  6. HDU 5887 Herbs Gathering(搜索求01背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做, ...

  7. HDU 2955 Robberies(概率DP,01背包)题解

    题意:给出规定的最高被抓概率m,银行数量n,然后给出每个银行被抓概率和钱,问你不超过m最多能拿多少钱 思路:一道好像能直接01背包的题,但是有些不同.按照以往的逻辑,dp[i]都是代表i代价能拿的最高 ...

  8. hdu 2126 Buy the souvenirs 二维01背包方案总数

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. HDU 2546 饭卡(带限制的01背包变形)

    思路:有几个解法,如下 1)先拿出5块买最贵的菜,剩下的菜再进行01背包.如何证明正确性?设最贵的菜价e,次贵的菜价s,设减去5后的余额为x,会不会产生这样的情况,假设用5元买了e,余额最多能买到x- ...

随机推荐

  1. Python进行数据分析(一)初步学习 对时区进行计数

    time_zones[:10] Out[19]: [u'America/New_York', u'America/Denver', u'America/New_York', u'America/Sao ...

  2. sql server常用函数、常用语句

    一.常用函数 1.字符串函数 : charindex(':','abc:123')    --寻找一个字符在一段字符串中起始的位置 len('zhangsan')   --获取一段字符串的长度 lef ...

  3. 【POJ】3177 Redundant Paths

    [算法]边双连通分量 [题意&题解]http://blog.csdn.net/geniusluzh/article/details/6619575 (注意第一份代码是错误的) 一些细节: 1. ...

  4. 【Atcoder】AGC 020 D - Min Max Repetition 二分+构造

    [题意]定义f(A,B)为一个字符串,满足: 1.长度为A+B,含有A个‘A',B个'B'. 2.最长的相同字符子串最短. 3.在满足以上2条的情况下,字典序最小. 例如, f(2,3) = BABA ...

  5. windows下启动mysql服务的命令行启动和手动启动方法

    1.图形界面下启动mysql服务. 在图形界面下启动mysql服务的步骤如下: (1)打开控制面板->管理工具->服务,如下图所示: 可以看到Mysql服务目前的状态是未启动(未写已启动的 ...

  6. 51Nod 1256 扩展欧几里得求乘法逆元

    给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的. Input 输入2个数M, N中间用 ...

  7. 【洛谷 P4360】 [CEOI2004]锯木厂选址(斜率优化)

    题目链接 一开始我的\(dp\)方程列错了,其实也不能说列错了,毕竟我交上去还是把暴力的分都拿到了,只是和题解的不一样,然后搞半天没搞出来去看题解,又看不懂,对不上,原来状态设置不一样自闭了. \(f ...

  8. Linux终端提示符PS1设置(颜色)

    \d :代表日期,格式为weekday month date,例如:"Mon Aug 1"\H :完整的主机名称.例如:我的机器名称为:fc4.linux,则这个名称就是fc4.l ...

  9. mysql中列的增删改

    增加列: ); ) after id; ) first; 修改列名: ); #change可改名字与字段类型 mysql> alter table a change uid uid int; Q ...

  10. linux 实现自动创建ftp用户并创建文件夹

    创建一个 createuser.sh的脚本文件 #!/bin/sh #传入的文件名 name=$1 #创建该用户所对应的ftp文件夹   /srv/ftp是我的ftp服务器的根目录 mkdir /sr ...