Suppose there is a tree with
You are interested in inspecting the tree in a specific way in order to extract its monetary value. A path is a sequence of nodes such that each node after the first is connected to the previous node by an edge, and no node is visited more than once. Suppose that there is a path from
the path's length (equal to one less than the number of nodes included in the path)
…multiplied by:
the sum of the values of all nodes included in the path
In order to determine how many unmarked moneys you can get from a tree, you must find the sum of the appraised values of all distinct pairwise paths in the tree. A path going from
Input Specification
On the first line, there will be one integer
On the second, there will be
On the subsequent
Constraints
It is recommended to use 64-bit integers. It is guaranteed that the answer will not exceed the upper limit of a 64-bit signed integer. (
Subtask 1 [20%]
Subtask 2 [35%]
Subtask 3 [45%]
No additional constraints.
Output Specification
Print a single integer, the sum of the appraisal values of all distinct paths in the tree.
Sample Input 1
2
3 4
1 2
Sample Output 1
7
Sample Input 2
4
618 843 585 569
3 2
1 4
2 4
Sample Output 2
19926
Explanation for Sample Output 2
The tree has four nodes, connected in a line:
with sum and length ; with sum and length ; with sum and length ; with sum and length ; with sum and length ; with sum and length .
Thus, the answer is the following:
Sample Input 3
5
0 1 1 1 1
1 2
1 3
1 4
1 5
Sample Output 3
28
Comments
beautiful!
do I mod anything?
You don't need to worry about overflow, it is guaranteed that the answer will be less than
. (statement updated)