#P10654. Efficient Information Transmission on Mercury

    ID: 12680 Type: Default 1000ms 256MiB

Efficient Information Transmission on Mercury

Efficient Information Transmission on Mercury

On Mercury, there are nn servers arranged in a line, connected by n1n-1 bidirectional cables such that the ii-th cable connects server ii and server i+1i+1. When Earth sends a message to server jj at time SS, that server immediately stores the message permanently and holds a copy in its buffer for tit_i seconds (for server ii). Each server will forward the message at the first opportunity available after reception. However, due to external factors, the ii-th cable can only transmit information during the time interval [li,ri][l_i, r_i]. Specifically, when a cable becomes active, if one of its two connected servers still holds the message in its buffer (i.e. within its tt-second retention period) then the other server instantaneously receives the message. In other words, if a server receives the message at time TT, it will forward this message at time max{T,l}\max\{T, l\} provided that [ \max{T, l} \le T+t_{\text{server}} \quad \text{and} \quad \max{T, l} \le r,. ] You are allowed to choose SS arbitrarily. Your goal is to ensure that, after the message is sent to server jj, all servers eventually receive it. Output the minimum possible duration from the Earth sending time SS to the moment when the final server receives the message. If it is impossible to deliver the message to every server, output 1-1.

Input/Output Timing Note: The transmissions on a cable occur exactly at time max{T,li}\max\{T, l_i\} (where TT is the sending server's reception time) if that time does not exceed both T+tserverT+t_{\text{server}} (the buffer expiry) and rir_i (the cable’s active window).

inputFormat

The first line contains two integers nn and jj (1jn1 \le j \le n), representing the number of servers and the index of the server that first receives the message from Earth. The second line contains nn integers t1,t2,,tnt_1, t_2, \ldots, t_n, where tit_i is the number of seconds that server ii retains the message in its buffer. Each of the following n1n-1 lines contains two integers lil_i and rir_i, representing the active time interval of the ii-th cable.

outputFormat

Output a single integer: the minimum possible duration from the Earth sending time to the moment when all servers have received the message, or 1-1 if it is impossible.

sample

3 2
3 3 3
1 5
2 6
0