[www.luogu.org] P1156 A trap of garbage *Solution*

A cow fell into a trap of garbage, whose depth is D(meter(s)), and is now at the bottom (Height=0).
He could get out of the trap when his position is more than D(meter(s)) in height.
There are farmers throwing G pieces of garbage into the trap in total. The cow will choose either to put the garbage below him and rise up himself in height, or to swallow the garbage and get more time surviving in the trap.
Now we know the time farmers throw any piece of garbage, the height of any piece of garbage, and its ability to feed the cow.
The cow can struggle to live with an initial living time of 10(hours). Then when will he get out the trap?
Input:
D G
T_i F_i H_i (1<=i<=G, indicating the time of throwing down the piece of garbage, its ability to feed the cow and its height)
Output:
t(indicating the least time the cow can get out of the trap, if and only if he is able to get out and won't die of hunger;
or indicating the longest time the cow will survive in the trap, if and only if he is not able to get out any longer)
Sample Input:
20 4
5 4 9
9 3 2
12 6 10
13 1 1
Sample Output:
13
Explanations:
The cow could choose to put Garbage No.1, 3, and 4 below him and get a height of 9+10+1=20 to get out of the trap.
And of course, he should eat up Garbage No.2 to make himself survive 3 more hours.
For all 11 testcases: 1<=D,G<=100; 1<=T_i<=1000, 1<=F_i<=30, 1<=H_i<=25 (1<=i<=G)

A typical DP problem with more than one solution, for its very little data. I simply show the easiest way to solve it here.

First of all, it is very important to sort all the pieces of garbage by the time it would be thrown down.

Suppose f[i] indicates the longest surviving time at the height of i:

for j->1 to G; for i->D to 0;

f[i+h[j]]=max(f[i+H[j]],f[i]);f[i]+=F[i];

If the depth can be reached, print the current time T[i] immediately, or we could print the value of f[0], indicating the longest surviving time (The pity cow eats every piece of garbage and will die at the bottom of the trap in despair T_T)