Problem A
Altruistic Amphibians

Picture from szftlgs.com, CC0
A set of frogs have accidentally fallen to the bottom of a large pit. Their only means of escaping the pit is to jump out of it. Each frog $i$ is described by three parameters $(l_ i,w_ i,h_ i)$ where $l_ i$ is its leap capacity, $w_ i$ its weight, and $h_ i$ its height. The leap capacity specifies how high that frog can jump. If a frog’s leap capacity is strictly larger than the depth of the pit, the frog can directly escape the pit. However, these frogs are altruistic. Rather than selfishly saving themselves and leaving the frogs with too limited leap capacity behind, they collectively aim to save as many of them from the pit as possible.

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?


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 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
Sample Input 2 Sample Output 2
3 19
14 5 3
12 4 4
20 10 5

Please log in to submit a solution to this problem

Log in