DMOPC '16 Contest 1 P4 - Tree Appraisal

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.8s
Memory limit: 256M

Author:
Problem type

Suppose there is a tree with N nodes (numbered from 1 to N), which is a graph with N nodes, N1 edges, no cycles, and exactly one available path to travel between any pair of nodes. In this tree, each node is made of something which is probably really valuable, and therefore node i has a value of Ki.

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 a to b in the tree. The appraised value, in unmarked moneys, of that path will be equal to:

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 a to b is not considered distinct from one which goes from b to a. Formally, find the total of appraise(a,b) across all pairs (a,b), where 1a<bN, and the appraise function is as described.

Input Specification

On the first line, there will be one integer N.
On the second, there will be N space separated integers from K1 to KN. These indicate the values of nodes of the tree in increasing order.
On the subsequent N1 lines, there will be two integers, aj and bj, such that aj is not equal to bj. This indicates an undirected edge going between aj and bj.

Constraints

1N200000

0Ki1000

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. (2631)

Subtask 1 [20%]

1N100

Subtask 2 [35%]

1N2000

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

Copy
2
3 4
1 2

Sample Output 1

Copy
7

Sample Input 2

Copy
4
618 843 585 569
3 2
1 4
2 4

Sample Output 2

Copy
19926

Explanation for Sample Output 2

The tree has four nodes, connected in a line: 3241, and the values have been assigned as 585 for node 3, 843 for node 2, 569 for node 4, and 618 for node 1. Therefore, there are six paths we must consider:

  • 32 with sum 1428 and length 1;
  • 34 with sum 1997 and length 2;
  • 31 with sum 2615 and length 3;
  • 24 with sum 1412 and length 1;
  • 21 with sum 2030 and length 2;
  • 41 with sum 1187 and length 1.

Thus, the answer is the following: 14281+19972+26153+14121+20302+11871, which is equal to 19926.

Sample Input 3

Copy
5
0 1 1 1 1
1 2
1 3
1 4
1 5

Sample Output 3

Copy
28

Comments


  • 0
    Shoot  commented 58 days ago

    beautiful!


  • 3
    imaxblue  commented on Oct. 11, 2016, 8:55 p.m.

    do I mod anything?


    • 4
      k_53P  commented on Oct. 11, 2016, 9:32 p.m. edit 2

      You don't need to worry about overflow, it is guaranteed that the answer will be less than 2631. (statement updated)