Bank Hacking (思维题)

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 n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.

Let us define some keywords before we proceed. Bank i and bankj are neighboring if and only if there exists a wire directly connecting them. Banki and bank j aresemi-neighboring if and only if there exists anonline bankk such that bank i and bankk are neighboring and bankk and bank j areneighboring.

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

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 x if and only if all these conditions are met:

  1. Bank x is online. That is, bank x is not hacked yet.
  2. Bank x is neighboring to some offline bank.
  3. The strength of bank x 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.

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.

The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.

Each of the next n - 1 lines contains two integersui andvi (1 ≤ ui, vi ≤ n,ui ≠ vi) — meaning that there is a wire directly connecting banksui andvi.

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.

Output

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

Example
Input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output
5
Input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output
93
Input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output
8
Note

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

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

In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength93.

题意:

一个人要黑银行的电脑,n家银行网络相连,每家银行都有一个权值,当黑客的能力>权值才能黑银行电脑,每黑一家银行,与之相邻和次相邻的银行权值会加1,刚开始可以任意选择一家银行黑掉,但是之后黑掉的银行必须旁边有已经被黑掉的电脑,问黑客的能力至少多少才能黑掉所有银行

思路:
查询一遍,
情况一:如果MAX只有一个,并且所有MAX-1在它的旁边,那么输出值就是MAX;
情况二:如果MAX有多个,那么如果存在某个点,能够让它的子节点包含所有MAX,那么输出值就是MAX+1;
情况三:剩余所有情况为MAX+2;

另一种想法(感觉会简单很多)
从N点开始,意思就是它的相邻点+1,其余点都+2,查询最大值变可

#include
#include
#include
using namespace std;
const int maxn=3e5+10;

int n;
int a[maxn];
int next_is_max[maxn];
int next_is_max_1[maxn];
int sum1=0;
int sum2=0;
int main()
{
    cin>>n;
    int maxn=-1999999999;
    int nn;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(maxn < a[i]){
            maxn=a[i];
            nn=i;
        }
    }
    for(int i=1;i


你可能感兴趣的:(Bank Hacking (思维题))