Problem H
Altruistic Amphibians
The frogs realize that if a frog $A$ climbs up on the back of frog $B$ before it jumps, the first frog $A$ stands a better chance of escaping the pit: it can escape if $h_ B+l_ A$ is strictly larger than the depth of the pit.
Furthermore, if frog $B$ carrying frog $A$ on its back climbs up on the back of frog $C$, the situation is even better for frog $A$: it can now escape the pit if $h_ C+h_ B+l_ A$ is strictly larger than the depth of the pit.
The frogs can build even higher piles of frogs this way, the only restriction is that no frog may carry other frogs of weight in total amounting to its own weight or heavier. Once a pile has been used to allow a frog to escape, the frogs in the pile jump back to the bottom of the pit and they can then form a new pile (possibly consisting of a different set of frogs). The question is simply how many frogs can escape the pit assuming they collaborate to maximize this number?
Input
The first line of input contains two integers $n$ and $d$ ($1 \le n \leq 100\, 000$, $1 \le d \le 10^8$), where $n$ is the number of frogs and $d$ is the depth of the pit in µm. Then follow $n$ lines each containing three integers $l, w, h$ ($1 \le l, w, h \le 10^8$), representing a frog with leap capacity $l$ µm, weight $w$ µg, and height $h$ µm. The sum of all frogs’ weights is at most $10^8$ µg.
Output
Output the maximum number of frogs that can escape the pit.
Sample Input 1 | Sample Output 1 |
---|---|
3 19 15 5 3 12 4 4 20 10 5 |
3 |
Sample Input 2 | Sample Output 2 |
---|---|
3 19 14 5 3 12 4 4 20 10 5 |
2 |