Experience

Given a rooted tree with values on nodes, partition nodes into vertex-disjoint downward paths to maximize the sum over paths of (max value minus min value).

Hard8TreeDynamic programmingGreedyDFSNo attempts yetTime limit2sMemory limit512 MB

Problem

Company X has NN employees. The company has a strict hierarchical tree structure. The CEO (chief executive officer) is at the top (the root of the tree) and has some number of direct subordinates, who have direct subordinates of their own, and so on down to the regular employees, who have no subordinates (the leaves of the tree).

The employees are numbered from 1 to NN. The CEO has number 1, and the other numbers have nothing to do with the hierarchy. Each employee has some experience: employee ii has experience WiW_i, a non-negative integer.

The company has a large number of group projects to complete, so the management splits all of the employees into teams. The split must satisfy both conditions below.

  • Each team consists of at least one person, and each person belongs to exactly one team.
  • Each team consists only of people who are consecutive subordinates of one another. A group of employees j1,j2,j3,j4,j_1, j_2, j_3, j_4, \dots is a valid team when j2j_2 is a direct subordinate of j1j_1, j3j_3 is a direct subordinate of j2j_2, j4j_4 is a direct subordinate of j3j_3, and so on.

After a group project is finished, the total experience of the team assigned to that project increases by WmaxWminW_{\max} - W_{\min}, where WmaxW_{\max} is the largest experience and WminW_{\min} is the smallest experience among the members of the team. The total experience increase for the company is the sum of the increases of all teams. The management wants to maximize the total increase for the company by choosing the best possible split under the two conditions above.

Write a program that computes the maximum possible experience increase for the company.

Input

The first line contains a single integer NN, the number of employees in the company.

The second line contains NN space separated non-negative integers W1,W2,,WNW_1, W_2, \dots, W_N, the experience of each employee.

Each of the next N1N - 1 lines contains two space separated integers uu and vv in that order. Employee vv is a direct subordinate of employee uu.

Output

Print one integer, the maximum total experience increase for the company.

Constraints

  • 1N1000001 \le N \le 100000
  • 0Wi1090 \le W_i \le 10^9

Note

The picture shows a company of 7 employees. The number inside a circle is the employee number, and the red number next to it is that employee's experience. One split that reaches the maximum total increase is {1, 5, 3}, {6, 2, 4}, {7}. The split {1, 5}, {3}, {6, 2, 4}, {7} reaches the same maximum.