#P1643. Roller-coaster

Roller-coaster

Problem Description

A train starts from the starting point with an initial height of 00 and travels to the right at a constant speed of 11 (i.e., it moves 1 unit of horizontal distance per second). The departure time of the train is a given constant DD (i.e., it departs from the starting point at time DD). Track construction can begin at time 00.

There are nn track segments to be constructed, numbered 11 to nn. Each track segment ii is described by three integer parameters:

  • Length LiL_i: a positive integer, representing the horizontal distance of the track segment.
  • Height change SiS_i: an integer, representing the change in height after the train travels through this track segment. If Si>0S_i > 0, it is an uphill segment (height increases by SiS_i); if Si<0S_i < 0, it is a downhill segment (height decreases by Si|S_i|).
  • Construction time TiT_i: a positive integer, representing the time required to construct this track segment.

You need to determine the traveling order of these segments (the order in which the train passes through them) and the construction order (the order in which they are built).

Constraints

  1. Construction order constraint: Only one track segment can be constructed at a time, and construction cannot be interrupted. For the kk-th segment in the traveling order, it must be completed before the train arrives at the starting point of that segment, i.e., its completion time \le the train's arrival time at the starting point of that segment.
  2. Non-negative height: At any moment during the journey, the height of the train must not fall below 00.
  3. Final height zero: After traveling through all nn track segments, the train's height must be exactly 00.

Objective

Among all traveling orders and construction plans that satisfy the above conditions, minimize the maximum height reached by the train during the journey. If no feasible solution exists, output 1-1.

Input Format

The first line contains two integers n,Dn, D.

The next nn lines each contain three integers Li,Si,TiL_i, S_i, T_i.

Output Format

Output an integer representing the minimum possible maximum height, or 1-1 if no solution exists.

Examples

Sample Input 1

2 5
2 1 3
1 -1 2

Sample Output 1

1

Explanation: Both track segments can be completed before departure (total construction time 3+2=53+2=5). Traveling uphill first and then downhill results in a maximum height of 11.

Sample Input 2

7 20
3 2 3
4 3 4
2 -1 2
3 -2 3
2 1 2
1 -2 1
2 -1 2

Sample Output 2

3

Explanation: By following the traveling order 2,4,1,3,5,7,62,4,1,3,5,7,6 (i.e., track segment 2, track segment 4, track segment 1, track segment 3, track segment 5, track segment 7, track segment 6), the height never exceeds 33 throughout the journey, and this value is optimal.

Sample Input 3

7 20
3 2 3
4 3 4
2 -1 2
3 -2 3
2 1 2
1 2 1
2 -1 2

Sample Output 3

-1

Explanation: It is impossible to achieve a final height of zero, so no solution exists.

Constraints

  • 1n201 \leq n \leq 20
  • 1D10101 \leq D \leq 10^{10}
  • 1Li,Ti10101 \leq L_i, T_i \leq 10^{10}
  • Si1010|S_i| \leq 10^{10}