Tree
Time limit2sMemory limit1024 MB
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 vertices are fashionable now, and each vertex has a number 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 is maximal. Here is the path between vertices and , and is the length of the path 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 (), the number of vertices in the tree. The second line contains integers (), where is the number written on vertex . Each of the following lines contains a pair of integers , (), an edge of the tree.
Output
Print the maximum value of the quantity described in the statement.