Experiences#8724

Experiences

0.5 seconds
256 megabytes
Hard

Company X has employees. The company has a strict hierarchical tree-like structure – the CEO (Chief Executive Officer) stands at the top (root of the tree), he has some number of direct subordinates, who also have direct subordinates and so on, until we reach regular employees, who have no subordinates (leaves of the tree). The employees are numbered with integers from to . The CEO has number 1, but the other numbers have nothing to do with the hierarchy. Each employee has some experience – the -th employee has experience, denoted by non-negative integer W_i.

The company has a large number of group projects to complete and the management has decided to split all of the employees into different groups (teams), so that the following conditions are satisfied:

The management knows that after a group project is finished, the total experience of the group, assigned to the project, increases by W_{max} - W_{min}, where W_{max} is the maximal experience, and W_{min} is the minimal experience among the group members. The total experience increase for the company is equal to the sum of the experience increases of all teams. The management wants to maximize the total experience increase for the company, by splitting the employees into the best possible teams' configuration, following the two conditions mentioned above.

Write a program experience to calculate the maximum possible experience increase for the company.

Input

The first line of the standard input contains a single integer - number of employees in the company (1 \leq N \leq 10^5).

The second line contains space separated non-negative integers W_1, W_2, \ldots, W_N – the experience of each employee of the company (0 \leq W_i \leq 10^9).

Then N-1 lines follow, each containing space separated integers and in the mentioned order. These numbers represent the subordinate relations in the company – the employee with number is a direct subordinate of the employee with number .

Output

The program should print to the standard output one integer – the maximum total experience increase for the company.

Examples

Example 1

Loading
Loading

Note

One possible configuration that maximizes the total experience increase is {1, 5, 3}, {6, 2, 4}, {7}.

ejoi_experience.png

There is another configuration with the same maximal total experience increase – {1, 5}, {3}, {6, 2, 4}, {7}.