[Solution] Late For Work Codeforces Solution
Debajyoti has a very important meeting to attend and he is already very late. Harsh, his driver, needs to take Debajyoti to the destination for the meeting as fast as possible.
Harsh needs to pick up Debajyoti from his home and take him to the destination so that Debajyoti can attend the meeting in time. A straight road with traffic lights connects the home and the destination for the interview. The traffic lights are numbered in order from to .
Each traffic light cycles after seconds. The -th traffic light is (in which case Harsh can cross the traffic light) for the first seconds, and (in which case Harsh must wait for the light to turn ) for the remaining seconds, after which the pattern repeats. Each light's cycle repeats indefinitely and initially, the -th light is seconds into its cycle (a light with has just turned ). In the case that Harsh arrives at a light at the same time it changes colour, he will obey the new colour. Formally, the -th traffic light is from and from (after which it repeats the cycle). The -th traffic light is initially at the -th second of its cycle.
From the -th traffic light, exactly seconds are required to travel to the next traffic light (that is to the -th light). Debajyoti's home is located just before the first light and Debajyoti drops for the interview as soon as he passes the -th light. In other words, no time is required to reach the first light from Debajyoti's home or to reach the interview centre from the -th light.
Harsh does not know how much longer it will take for Debajyoti to get ready. While waiting, he wonders what is the minimum possible amount of time he will spend driving provided he starts the moment Debajyoti
arrives, which can be anywhere between to seconds from now. Can you tell Harsh the minimum possible amount of time he needs to spend on the road?
The first line of input will contain two integers, and (, ) denoting the number of traffic lights and the cycle length of the traffic lights respectively.
lines of input follow. The -th line will contain two integers and (, ) describing the -th traffic light.
The following line of input contains integers () — the time taken to travel from the -th to the -th traffic light.
Output a single integer — the minimum possible amount of time Harsh will spend driving.
No comments:
Post a Comment