Spring Tree
题号:NC237189
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

There are n iron balls numbered 1 to n, and the weight of the i th ball is w_i. We use n - 1 springs to connect them into a tree.

The ball numbered 1 is the root of the tree. Now we grasp the root and hang the whole tree.

The initial length of each spring is 1. If the weight of the subtree it hangs is g, its length will become .

At this time, PaiGuDragon come up with an evil idea. It plans to rearrange the w sequence, that is, PaiGuDragon is going to generate an array p_1,p_2,...,p_n which is a permutation of 1,2....,n, and then the weight of the i th ball will become

Considering all the arrangements, what is the maximum of maximum depth of the tree after hanging?

Here, the maximum depth is defined as the maximum value of the sum of the spring lengths on the path from the leaf to the root.

输入描述:

The first line contains an integer .

The second line contains n integers  - the initial state of weight.

Each of the next n-1 lines contains 2 integers - an edge between u,v.

It's guaranteed that the given graph is a tree.

输出描述:

Output an integer. maximum of maximum depth of the tree.
示例1

输入

复制
4
1 2 3 4
1 2
2 3
3 4

输出

复制
23

说明

In the test case, keep original arrangement is a good idea.

maximum depth = (1+4) + (1+4+3) + (1+4+3+2) = 23