Time limit: 1s
Memory limit: 256MB
Input:
Output:
Task
You are given a tree with nodes. Each node has values assigned to it, . You must calculate the sum cost of every pair nodes , where and ≠ . For a pair of nodes , we define the cost of that path to be the sum of value of all nodes that lie inside the path.
Input
The first line of the input will contain (), the number of nodes. The second line will contain numbers, the number, representing the (). Each of the next lines will contain a pair of nodes , each representing an edge of the tree.
Restrictions
- For tests worth points ()
- For tests worth more points, for
Output
The output will contain the sum of costs over all pairs of nodes , such that
Example
stdin
5
2 7 14 22 77
1 2
1 3
2 4
4 5
stdout
442