The True FA Student
InterviewTime limit1sMemory limit512 MB
Find the minimum time to reach zone N, choosing at each crosswalk whether to wait for the green signal or take the footbridge.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Simulation
- Solved
- No attempts yet
Problem
Sogang University has an FA system that gives an F grade to any student who misses a single course five times. Geunsoo has already missed the Algorithm Design and Analysis course four times, and one more absence will earn him an F. The classroom is not that far away, but the crosswalks on the way slow him down.
Recently the school built a footbridge next to every crosswalk so that students are not late because of traffic signals. With a footbridge, he can cross the road without waiting for the signal, but the route length changes, so it may take longer. Geunsoo wants to reach school as soon as possible by deciding whether to wait for each signal or use the footbridge.
Geunsoo must pass N crosswalks in a row to reach school. For convenience, the N crosswalks divide the road into N+1 zones numbered 0 to N. The crosswalk between zone i-1 and zone i is called the i-th crosswalk. Geunsoo starts in zone 0, and the classroom is in zone N.
For each crosswalk, Geunsoo recorded four values:
- : the time to move from zone i-1 to zone i using the crosswalk.
- : the time to move from zone i-1 to zone i using the footbridge.
- : the number of minutes the i-th signal stays green. It turns red exactly minutes after turning green.
- : the number of minutes the i-th signal stays red. It turns green exactly minutes after turning red.
Note that is not the time it takes to cross the i-th crosswalk. It is the time to move between zones using the crosswalk. Crossing a crosswalk takes no time at all. For example, if a signal is green from minute to minute , and Geunsoo arrives at exactly minute , the signal has already turned red, so he cannot cross. If he arrives at any minute from to , he can cross immediately.
Geunsoo is standing in front of the first crosswalk, and every signal has just turned green at the same moment. Find the minimum time for Geunsoo to reach the classroom.
Input
The first line contains the integer , the number of crosswalks.
Each of the next lines contains four integers , , , and , separated by spaces.
Output
Print one line containing , the minimum number of minutes for Geunsoo to reach the classroom. is always a non-negative integer.