No.139 Intersections
Problem Statement
yuki-kun has decided to visit a friend's house, which is at distance $L$ from his home.
To reach the friend's house, he must cross the \(N\) intersections between them.
The i-th intersection starts at distance \(X_i\) from his home and has width \(W_i\).
That is, the interval \((X_i,X_i + W_i)\) is an intersection.
The traffic light installed at each intersection alternates between green and red every \(T_i\) units of time.
The light at every intersection turned green at the moment yuki-kun departed.
yuki-kun walks straight ahead at a speed of distance $1$ per unit time.
However, when yuki-kun arrives at distance \(X_i\), just before an intersection, if there is a possibility that he would be inside the intersection while the light is red, he waits at the position of distance \(X_i\) until that possibility disappears.
Find the time elapsed from when yuki-kun leaves his home until he arrives at the friend's house.
Input
Each input is given in the following format. All values are integers.\(N\) \(L\) \(X_1\) \(W_1\) \(T_1\) \(X_2\) \(W_2\) \(T_2\) ... \(X_N\) \(W_N\) \(T_N\)
\(1 \le N \le 100\)
\(1 \le L \le 10^5\)
\(1 \le T \le 10^7\)
The intersections are listed in increasing order of distance from his home. Neither his home nor the friend's house lies inside an intersection. That is, the following relation holds.
\(0 \le X_1 \lt X_1+W_1 \le X_2 \lt X_2+W_2 \le ... \le X_N \lt X_N+W_N \le L\)
Also, the duration of a green light is sufficient to cross the intersection. That is, the following relation holds.
\(W_i \le T_i\)
Output
Output on one line the time elapsed from when yuki-kun leaves his home until he arrives at the friend's house.
Sample
Sample 1
Input
1 8 5 2 2
Output
11
The light is green when he reaches the intersection, but if he kept going, it would turn red before he finished crossing.
Therefore, he waits for time 3 before the intersection until the next green light.
Sample 2
Input
3 10 1 2 10 3 4 20 7 3 30
Output
10
He does not need to wait at any intersection. Today is a lucky day.
Sample 3
Input
4 20 3 2 2 6 3 3 10 2 9 15 3 5
Output
35
He ended up waiting at the light at every intersection. Some days are just unlucky.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
kmjp