#P10654. Efficient Information Transmission on Mercury
Efficient Information Transmission on Mercury
Efficient Information Transmission on Mercury
On Mercury, there are servers arranged in a line, connected by bidirectional cables such that the -th cable connects server and server . When Earth sends a message to server at time , that server immediately stores the message permanently and holds a copy in its buffer for seconds (for server ). Each server will forward the message at the first opportunity available after reception. However, due to external factors, the -th cable can only transmit information during the time interval . Specifically, when a cable becomes active, if one of its two connected servers still holds the message in its buffer (i.e. within its -second retention period) then the other server instantaneously receives the message. In other words, if a server receives the message at time , it will forward this message at time provided that [ \max{T, l} \le T+t_{\text{server}} \quad \text{and} \quad \max{T, l} \le r,. ] You are allowed to choose arbitrarily. Your goal is to ensure that, after the message is sent to server , all servers eventually receive it. Output the minimum possible duration from the Earth sending time to the moment when the final server receives the message. If it is impossible to deliver the message to every server, output .
Input/Output Timing Note: The transmissions on a cable occur exactly at time (where is the sending server's reception time) if that time does not exceed both (the buffer expiry) and (the cable’s active window).
inputFormat
The first line contains two integers and (), representing the number of servers and the index of the server that first receives the message from Earth. The second line contains integers , where is the number of seconds that server retains the message in its buffer. Each of the following lines contains two integers and , representing the active time interval of the -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 if it is impossible.
sample
3 2
3 3 3
1 5
2 6
0