# Codeforces Round #408 (Div. 2) C. Bank Hacking（二分）

xiaoxiao2021-03-26  3

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 bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboringand bank k and bank j 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 1.

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:

Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. 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 integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — meaning that there is a wire directly connecting banks ui and vi.

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.

Examples 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

题目要求你hack掉所有的银行，且你需要的电脑的强度最小，让你求出最小的强度。

每hack一个银行，与其相连的或者半相连的银行的强度都要加1，与其相邻就是两者之间有边，如果i和k是半相邻，那么i与j之间有边，j与k之间有边（如果j银行被hack了，那么i和k就不是半相邻关系了）。

你第一开始可以hack任意一个比你电脑强度要低或者等于你电脑强度的银行，在此之后，你能hack小于等于你电脑强度的银行，且该银行没被hack过，还有就是这个银行必须连着一个被hack过的银行。

因为要求最小值，第一开始就想到了用二分，但是我一开始是用选择一个强度最大的银行开始hack，wa了三发之后发现了问题，比如这样的数据就有问题

5

6 5 6 4 3

1 2

2 3

3 4

4 5

这样的话得出的最小值就是8，然而答案是7，然后就想到一个银行的强度最多被增加两次，如果那个银行与第一个被hack的银行直接相连，那么就只会被增加一次，这样直接枚举每一个点作为起点答案能不能行，就可以了。

#include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define LL long long using namespace std; const int inf = 1e9+10; int n; LL l,r; int a[300010],t[300010]; vector<int> e[300010]; bool judge(LL x) { for(int i=1;i<=n;i++) t[i] = a[i]; int maxn = -inf; int c = -1; int cnt = 0; for(int i=1;i<=n;i++) { if(a[i] > x) return false; t[i]+=2; if(t[i] > x) cnt++; } if(n == 1) return true; if(cnt == 0) return true; for(int i=1;i<=n;i++) { int tmp = cnt; if(t[i] > x) tmp--; for(int j=0;j<e[i].size();j++) { int xx = e[i][j]; if(xx == i) continue; if(t[xx] == x + 1) tmp--; } if(tmp == 0) return true; } return false; } LL binarysearch() { LL ans = l; while(l <= r) { LL mid = (l + r) / 2; if(judge(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } return ans; } int main(void) { int i,j; while(scanf("%d",&n)==1) { for(i=1;i<=n;i++) { scanf("%d",&a[i]); e[i].clear(); l = min(l,(LL)a[i]); r = max(r,(LL)a[i]); } r += n; for(i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); e[x].push_back(y); e[y].push_back(x); } LL ans = binarysearch(); cout << ans << endl; } return 0; }