Cheerio Contest 1 S1/J4 - Going to School

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 512M

Author:
Problem type

Bob is getting ready to walk to RHHS for the first day of school! The route from Bob's home to RHHS can be modelled as a number line with his house located at point 0 and RHHS located at point D. To get to school, however, he must cross N signalled intersections, each located at distinct integer points (excluding point 0 and point D) and cycles between red and green. The i^\text{th} light is located at point p_i and stays red for r_i seconds and stays green for g_i seconds.

At t=0, all the traffic lights have just turned red, and Bob has just begun his walk, travelling 1 unit per second towards the school. Since Bob is a law-abiding citizen, he will only cross an intersection if the light is green. Otherwise, he will wait at the intersection until the light turns green. Bob knows the school is quite far away, so he wants to know the total time he will take to get to school. Can you write a program to help him?

Constraints

For all subtasks:

  • N < D \le 10^9
  • 1 \le p_i < D
  • p_1 < p_2 < p_3 < \dots < p_N
Points Awarded N r_i, g_i
5 points N=1 1 \le r_i, g_i \le 10^9
5 points 1 \le N \le 2 \times 10^5 1 \le r_i, g_i \le 10^9 and r_i = g_i
5 points 1 \le N \le 2 \times 10^5 1 \le r_i, g_i \le 10^9

Input Specification

The first line contains two integers N and D.

The next N lines each contain three integers p_i, r_i, and g_i.

Output Specification

Output the number of seconds Bob will take to get to school. Please note that this number may not fit inside a 32-bit integer.

Sample Input

3 25
6 28 6
15 15 22
19 4 9

Sample Output

62

Explanation for Sample Output

At t=0, Bob is at point 0, and all the traffic lights have just turned red. When he arrives at point 6, the traffic light is red, so he is forced to wait. At t=28, he can continue.

When he arrives at point 15, the light has just turned red, so he is forced to wait again. A t=52, he resumes his walk. When he reaches point 19, the light has just turned green, so he can continue walking as usual. He arrives at RHHS at t=62.


Comments

There are no comments at the moment.