Proud merchants
Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/65536 K (Java/Others)
Total submission (s): 925 accepted submission (s): 368
Problem descriptionrecently, ISEA went to an existing ent 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 wocould refuse to make a trade with you if your money were than Qi, and ISEA evaluated every item A value VI.
If he had m units of money, what's the maximum value ISEA cocould get?
Inputthere 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.
Outputfor each test case, output one integer, indicating maximum value ISEA cocould get.
Sample input2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3
Sample output5
11
Authorisea @ whu
Source2010 ACM-ICPC multi-university training Contest (3) -- host by whu
Recommendzhouzeyong
This is a backpack deformation question. A restriction is added to the question, that is, you can purchase this item only when the amount of money you have exceeds a certain limit.
Sort Q-P in ascending order, and then perform the dp of the 01 backpack.
# Include < Stdio. h >
# Include < Algorithm >
# Include < Iostream >
Using Namespace STD;
Const Int Maxn = 5005 ;
Int DP [maxn];
Struct Node
{
Int P, Q, V;
} Node [ 505 ];
Bool CMP (node A, Node B)
{
Return (A. Q - A. p) < (B. Q - B. p );
}
Int Main ()
{
Int N, m;
Int I, J;
Int P, Q, V;
While (Scanf ( " % D " , & N, & M) ! = EOF)
{
For (I = 0 ; I <= M; I ++ )
DP [I] = 0 ;
For (I = 0 ; I < N; I ++ )
{
Scanf ( " % D " , & Node [I]. P, & Node [I]. Q, & Node [I]. V );
}
Sort (node, node + N, CMP );
For (I = 0 ; I < N; I ++ )
{
For (J = M; j > = Node [I]. P; j -- )
{
If (J > = Node [I]. q)
DP [J] = Max (DP [J], DP [J - Node [I]. p] + Node [I]. V );
}
}
Int Ans = 0 ;
For (I = 1 ; I <= M; I ++ )
If (ANS < DP [I]) ans = DP [I];
Printf ( " % D \ n " , ANS );
}
Return 0 ;
}