There are
reindeer at the North Pole, all battling for the highest spot on the "Top Reindeer" leaderboard on the front page of CodeNorses (a popular competitive reindeer gaming website). Interestingly, the "Top Reindeer" title is just a measure of upvotes and has nothing to do with their skill level in the reindeer games, but they still give it the utmost importance.
Currently, the -th reindeer has a score of . You would like to influence the leaderboard with some operations. In an operation, you can choose a reindeer, and either increase or decrease his score by unit. Negative scores are allowed.
You have requirements for the resulting scores. Each requirement is given by an ordered pair , meaning that after all operations, the score of reindeer must be less than or equal to the score of reindeer .
Your task is to perform the minimum number of operations so that all requirements will be satisfied.
The first line contains two integers and (; ) — the number of reindeer and requirements, respectively.
The second line contains integers (), where is the current score of reindeer .
The next lines describe the requirements.
The -th of these lines contains two integers and (; ) — the two reindeer of the -th requirement.
Print integers (), where is the score of the -th reindeer after all operations.
If there are multiple solutions achieving the minimum number of operations, you may output any.
We can prove that there is always an optimal solution such that for all .
7 6 3 1 4 9 2 5 6 1 2 2 3 3 4 4 5 5 6 6 7
1 1 4 4 4 5 6
4 6 6 5 8 2 3 1 4 1 3 2 1 2 2 3 3 1
6 6 6 2
10 18 214 204 195 182 180 176 176 172 169 167 1 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 6 1 6 2 6 3 6 4 6 5 6 7 6 8 6 9 6 10
204 204 195 182 180 167 176 172 169 167
ConversionConversion EmoticonEmoticon