#P10654. [ROI 2017] 水星上的服务器 (Day 2)

[ROI 2017] 水星上的服务器 (Day 2)

Problem Description

There are nn servers on Mercury and n1n-1 data cables. The ii-th cable connects server ii and server i+1i+1 in both directions.

Suppose server jj receives a message sent from Earth. You need to transmit the message to all other servers as quickly as possible.

After a server ii receives the message from Earth, it can immediately store a copy in its database, and then keep it in its buffer for tit_i seconds before deleting it.

Due to some external factors, the ii-th data cable can transmit information only during the time interval [li,ri][l_i,r_i]. When a cable is active, if at least one of the two servers at its ends still has the message in its buffer, then the other one can obtain the message as well. (After a server receives the message, it will forward the message at the earliest possible time.)

You may choose any time to send the message from Earth to server jj. In the end, all servers must receive this message. Find the earliest time at which you can send the message. If no feasible solution exists, output 1-1.

Input Format

The first line contains an integer nn.

The second line contains nn integers t1,t2,,tnt_1,t_2,\dots,t_n.

In the next n1n-1 lines, each line contains two integers li,ril_i,r_i.

Output Format

Output nn lines, each containing one integer. The number on line ii is the answer when j=ij=i. If it is impossible, output 1-1.

1
10
0
2
3 5
6 8
3
1
3
1 2 4
7 10
3 5
-1
5
5
4
1 0 3 2
4 6
5 5
7 10
5
5
4
-1

Hint

Sample Explanation

For sample group #3:

If server 11 receives the patch first, then server 33 cannot get the patch, because channel 22 closes before channel 11 opens. If server 22 receives the patch at second 55, then server 33 can receive the patch immediately. After that, when channel 11 opens at second 77, server 11 will receive the patch. If server 33 receives the patch at second 55, then server 22 can receive the patch immediately. After that, when channel 11 opens at second 77, server 11 will receive the patch.

For sample group #4:

If server 22 receives the patch first, since server 22 never caches it and channel 22 opens only at second 55, in order for server 33 to get the patch, you can only choose to send the patch to server 22 at second 55. If server 33 receives the patch first, you may choose second 44. Server 33 will cache it until second 77, so both server 22 and server 44 can get the patch.

Constraints

Note: This problem provides only part of the testdata. For the full testdata, please go to LOJ P2771 for judging.

For all data, 0liri1090 \le l_i \le r_i \le 10^9.

Subtask ID Score 1n1 \le n \le 0ti0 \le t_i \le 0ri0 \le r_i \le Special Property
11 2020 500500 None
22 1010 50005000 / 50005000 ti=5000t_i=5000
33 50005000 / ri=5000r_i=5000
44 50005000 None
55 1515 2×1052 \times 10^5 / 10910^9 ti=109t_i=10^9
66 10910^9 / ri=109r_i=10^9
77 2020 10910^9 None

Translated by ChatGPT 5