Under construction

Still being built. Things may change

Tree

Time limit2sMemory limit1024 MB

Summary
Find the maximum over all vertex pairs of the path length times the smaller of the two endpoint values in a tree.
Level

Medium7 of 10

Topics
Tree, Union-find, Sorting
Solved
No attempts yet

Problem

Genius Evgenievich has long wanted a tree. Trees with nn vertices are fashionable now, and each vertex has a number xix_i written on it. The long-awaited day came, and Genius Evgenievich bought such a tree. After returning from the store, he began computing various values on it. Everything was easy for him until he tried to find a path in this tree whose value length(path(u,v))⋅min⁡(xu,xv)length(path(u, v)) \cdot \min(x_u, x_v) is maximal. Here path(u,v)path(u, v) is the path between vertices uu and vv, and length(path)length(path) is the length of the path pathpath in edges. For many days now, Genius Evgenievich has not gone outside. His friends, naturally, have started to worry. They ask you to help Genius Evgenievich solve this problem.

Input

The first line contains one integer nn (1≤n≤1051 \le n \le 10^5), the number of vertices in the tree. The second line contains nn integers xix_i (1≤xi≤1091 \le x_i \le 10^9), where xix_i is the number written on vertex ii. Each of the following n−1n-1 lines contains a pair of integers aia_i, bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n), an edge of the tree.

Output

Print the maximum value of the quantity described in the statement.

Examples1

  1. Example 1

    Input
    8
    9 2 8 8 7 2 1 2
    2 1
    3 1
    4 1
    5 2
    6 4
    7 5
    8 5
    
    Expected output
    21