#P5992. [PA 2015] Rozstaw szyn

[PA 2015] Rozstaw szyn

Description

Given a tree with nn nodes and mm leaf nodes, where the mm leaf nodes are nodes 11 to mm. Each leaf node has a weight rir_i.

You need to assign a weight to each of the remaining nmn-m nodes so that the sum of the absolute differences of weights between every pair of adjacent nodes in the tree is minimized.

Input Format

The first line contains two positive integers n,mn, m, representing the number of nodes and the number of leaf nodes.

The next n1n-1 lines each contain two positive integers u,vu, v, indicating that there is an edge between uu and vv.

The next mm lines each contain one positive integer, in order r1,r2,,rmr_1, r_2, \ldots, r_m, representing the weight of each leaf.

Output Format

Output one integer, the minimum possible sum of the absolute differences of weights between adjacent nodes in the tree.

6 4
1 5
2 5
3 6
4 6
5 6
5
10
20
40
35

Hint

For 100%100\% of the testdata, 2n5×1052 \le n \le 5 \times 10^5, 1mn1 \le m \le n, 1u,vn1 \le u, v \le n, 1ri5×1051 \le r_i \le 5 \times 10^5.

Translated by ChatGPT 5