## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] Late For Work Codeforces Solution

F. Late For Work
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 $n$ traffic lights connects the home and the destination for the interview. The traffic lights are numbered in order from $1$ to $n$.

Each traffic light cycles after $t$ seconds. The $i$-th traffic light is $\text{green}$ (in which case Harsh can cross the traffic light) for the first ${g}_{i}$ seconds, and $\text{red}$ (in which case Harsh must wait for the light to turn $\text{green}$) for the remaining $\left(t-{g}_{i}\right)$ seconds, after which the pattern repeats. Each light's cycle repeats indefinitely and initially, the $i$-th light is ${c}_{i}$ seconds into its cycle (a light with ${c}_{i}=0$ has just turned $\text{green}$). In the case that Harsh arrives at a light at the same time it changes colour, he will obey the new colour. Formally, the $i$-th traffic light is $\text{green}$ from $\left[0,{g}_{i}\right)$ and $\text{red}$ from $\left[{g}_{i},t\right)$ (after which it repeats the cycle). The $i$-th traffic light is initially at the ${c}_{i}$-th second of its cycle.

From the $i$-th traffic light, exactly ${d}_{i}$ seconds are required to travel to the next traffic light (that is to the $\left(i+1\right)$-th light). Debajyoti's home is located just before the first light and Debajyoti drops for the interview as soon as he passes the $n$-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 $n$-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

Solution Click Below:-  👉
👇👇👇👇👇

arrives, which can be anywhere between $0$ to $\mathrm{\infty }$ seconds from now. Can you tell Harsh the minimum possible amount of time he needs to spend on the road?

Input

The first line of input will contain two integers, $n$ and $t$ ($2\le n\le 2\cdot {10}^{5}$$2\le t\le {10}^{9}$) denoting the number of traffic lights and the cycle length of the traffic lights respectively.

$n$ lines of input follow. The $i$-th line will contain two integers ${g}_{i}$ and ${c}_{i}$ ($1\le {g}_{i}$0\le {c}_{i}) describing the $i$-th traffic light.

The following line of input contains $n-1$ integers ${d}_{1},{d}_{2},\dots ,{d}_{n-1}$ ($0\le {d}_{i}\le {10}^{9}$) — the time taken to travel from the $i$-th to the $\left(i+1\right)$-th traffic light.

Output

Output a single integer — the minimum possible amount of time Harsh will spend driving.