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
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:
-
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.
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.