CF796C.Bank Hacking

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.

There are nn banks, numbered from 11 to nn . There are also n1n-1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank ii has initial strength aia_{i} .

Let us define some keywords before we proceed. Bank ii and bank jj are neighboring if and only if there exists a wire directly connecting them. Bank ii and bank jj are semi-neighboring if and only if there exists an online bank kk such that bank ii and bank kk are neighboring and bank kk and bank jj are neighboring.

When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 11 .

To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank xx if and only if all these conditions are met:

  1. Bank xx is online. That is, bank xx is not hacked yet.
  2. Bank xx is neighboring to some offline bank.
  3. The strength of bank xx is less than or equal to the strength of Inzane's computer.

Determine the minimum strength of the computer Inzane needs to hack all the banks.

输入格式

The first line contains one integer nn ( 1<=n<=31051<=n<=3·10^{5} ) — the total number of banks.

The second line contains nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} ( 109<=ai<=109-10^{9}<=a_{i}<=10^{9} ) — the strengths of the banks.

Each of the next n1n-1 lines contains two integers uiu_{i} and viv_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n , uiviu_{i}≠v_{i} ) — meaning that there is a wire directly connecting banks uiu_{i} and viv_{i} .

It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.

输出格式

Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.

输入输出样例

  • 输入#1

    5
    1 2 3 4 5
    1 2
    2 3
    3 4
    4 5
    

    输出#1

    5
  • 输入#2

    7
    38 -29 87 93 39 28 -55
    1 2
    2 5
    3 2
    2 4
    1 7
    7 6
    

    输出#2

    93
  • 输入#3

    5
    1 2 7 6 7
    1 5
    5 3
    3 4
    2 4
    

    输出#3

    8

说明/提示

In the first sample, Inzane can hack all banks using a computer with strength 55 . Here is how:

  • Initially, strengths of the banks are [1,2,3,4,5][1,2,3,4,5] .
  • He hacks bank 55 , then strengths of the banks become [1,2,4,5,][1,2,4,5,-] .
  • He hacks bank 44 , then strengths of the banks become [1,3,5,,][1,3,5,-,-] .
  • He hacks bank 33 , then strengths of the banks become [2,4,,,][2,4,-,-,-] .
  • He hacks bank 22 , then strengths of the banks become [3,,,,][3,-,-,-,-] .
  • He completes his goal by hacking bank 11 .

In the second sample, Inzane can hack banks 44 , 22 , 33 , 11 , 55 , 77 , and 66 , in this order. This way, he can hack all banks using a computer with strength 9393 .

首页