Note: The time limit for this problem is 4s, twice the default.
Bessie is on vacation! Due to some recent technological advances, Bessie will travel via technologically sophisticated flights, which can even time travel. Furthermore, there are no issues if two "parallel" versions of Bessie ever meet.
In the country there are N airports numbered 1,2,…,N and M time-traveling flights (1≤N,M≤200000). Flight j leaves airport cj at time rj, and arrives in airport dj at time sj (0≤rj,sj≤109, sj<rj is possible). In addition, she must leave ai time for a layover at airport i (1≤ai≤109). (That is to say, if Bessie takes a flight arriving in airport i at time s, she can then transfer to a flight leaving the airport at time r if r≥s+ai. The layovers do not affect when Bessie arrives at an airport.)
Bessie starts at city 1 at time 0. For each airport from 1 to N, what is the earliest time when Bessie can get to at it?
INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input contains N and M.
The next M lines describe flights. The jth of these lines contains cj, rj, dj, sj in that order. (1≤cj,dj≤N, 0≤rj,sj≤109)
The next line describes airports. It contains N space separated integers, a1,…,aN.
OUTPUT FORMAT (print output to the terminal / stdout):
There are N lines of output. Line i contains the earliest time when Bessie can get to airport i, or -1 if it is not possible for Bessie to get to that airport.
SAMPLE INPUT:
3 3
1 0 2 10
2 11 2 0
2 1 3 20
10 1 10
SAMPLE OUTPUT:
0
0
20
Bessie can take the 3 flights in the listed order, which allows her to arrive at airports 1 and 2 at time 0, and airport 3 at time 20.
Note that this route passes through airport 2 twice, first from time 10-11 and then from time 0-1.
SAMPLE INPUT:
3 3
1 0 2 10
2 10 2 0
2 1 3 20
10 1 10
SAMPLE OUTPUT:
0
10
-1
In this case, Bessie can take flight 1, arriving at airport 2 at time 10. However, she does not arrive in time to also take flight 2, since the departure time is 10 and she cannot make a 1 time-unit layover.
SCORING:
Inputs 3-5: rj<sj for all j, i.e. all flights arrive after they depart.
Inputs 6-10: N,M≤5000
Inputs 11-20: No additional constraints.
Problem credits: Brandon Wang