City Maintenance

Given a tree with a price on every vertex, find the maximum, over all choices of a removed vertex, of the sum of the maximum price within each remaining connected component.

Hard8TreeDFSGreedyImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

The country of Algorithm has NN cities and N1N-1 highways that connect them. Every city reaches every other city along the highways, directly or indirectly.

The country removes one city. When a city is removed, every highway attached to it disappears with it, and the removed city is no longer maintained.

Maintaining a city needs a maintenance machine. Each city has a minimum machine price. If a city's minimum price is xx, only a machine that costs xx or more can maintain that city.

A machine moves only along highways. City maintenance is not urgent, so one machine is used for each set of cities that the remaining highways still connect to one another.

You run the company that builds the machines. Your revenue is the smallest cost of maintaining every remaining city. Write a program that prints the highest revenue that removing one city can produce.

Input

The first line has a positive integer NN (2N1062 \le N \le 10^6). The cities are numbered 1 through NN.

The second line has the minimum machine price of city 1 through city NN in order, separated by spaces. Every price is a positive integer, and the sum of all prices is less than 2312^{31}.

Each of the next N1N-1 lines has the numbers of the two cities that one highway connects, separated by a space.

Output

Print the highest revenue that removing one city can produce.