Codeforces Round 408 (Div. 2)
C
Bank Hacking
Codeforces
1900
原题
← BD →
翻译中…

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.

翻译中…

image

翻译中…

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 $*a*_{*i*}$.

翻译中…

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 neighboring and 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:

翻译中…
  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·10^{5}$) — the total number of banks.

翻译中…

The second line contains $*n*$ integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ ($- 10^{9} ≤ *a*_{*i*} ≤ 10^{9}$) — the strengths of the banks.

翻译中…

Each of the next $*n* - 1$ lines contains two integers $*u*_{*i*}$ and $*v*_{*i*}$ ($1 ≤ *u*_{*i*}, *v*_{*i*} ≤ *n*$, $*u*_{*i*} ≠ *v*_{*i*}$) — meaning that there is a wire directly connecting banks $*u*_{*i*}$ and $*v*_{*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.

翻译中…

Output

翻译中…

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

翻译中…

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 strength $93$.

翻译中…

样例

Input #1
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output #1
5
InputCopy OutputCopy
Input #2
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output #2
93
InputCopy OutputCopy
Input #3
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output #3
8
InputCopy OutputCopy
题目信息
题号
796C
时间限制
2 s
内存限制
256 MB
标签
构造
数据结构
动态规划
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。