#P15291. [MCO 2023] Love Letter
[MCO 2023] Love Letter
Description
There are dragons numbered from to . Dragon has age . . Dragon is Evirir the dragon.
Evirir has a letter it wants to send to dragon . To avoid awkwardness caused by age difference, Evirir can send the letter to another dragon (not necessarily dragon ). The dragon who received the letter can then send the letter to another dragon, and so on. The goal is to eventually send the letter to dragon .
For all (), when dragon has the letter, it can send the letter to dragon in time. A dragon can "send" the letter to itself in time. However, there are pairs of dragons who are close friends. If dragons and are close friends, then it takes time instead for dragon to send a letter to dragon (and vice versa).
For each dragon (), answer the question (independently): What is the minimum total time needed for dragon to send a letter to dragon ?
Note: denotes the absolute value of . For example, , , and .
Input Format
The first line contains two space-separated integers and (, ) -- the number of dragons and the number of pairs of close friends.
The second line contains space-separated integers () -- the dragons' ages.
Then, lines follow. Each of the lines contains two space-separated integers and (, ), which means that dragons and are close friends. It is guaranteed that the same pair of close friends will not appear twice (if appears, then and will not appear afterwards).
Output Format
Output space-separated integers , where is the minimum total time needed for dragon to send the letter to dragon .
8 4
50 30 23 10 3 67 69 47
3 7
3 1
2 4
7 1
0 7 0 7 14 2 0 3
3 0
2 3 1
0 1 1
Hint
Note
Explanation for the first sample:
When , it takes time because dragon already has the letter.
When , since dragon and are close friends, dragon can send the letter directly to dragon in time.
When , dragon can send the letter to dragon first in time (they are close friends), then dragon sends the letter to dragon in time. The total time taken is .
When , dragon can just send the letter directly to dragon , taking time.
Here is one optimal way each for the remaining 's ( means dragon sends the letter to dragon using time):
- Dragon : $1 \xrightarrow{0} 3 \xrightarrow{7} 2 \xrightarrow{0} 4$
- Dragon : $1 \xrightarrow{0} 3 \xrightarrow{7} 2 \xrightarrow{0} 4 \xrightarrow{7} 5$
- Dragon : $1 \xrightarrow{0} 3 \xrightarrow{0} 7 \xrightarrow{2} 6$
- Dragon :
Scoring
Subtask 1 ( points): ,
Subtask 2 ( points):
Subtask 3 ( points):
Subtask 4 ( points): for all ()
Subtask 5 ( points): The sequence is non-decreasing, i.e. for
Subtask 6 ( points): No additional constraints
京公网安备 11011102002149号