#P5992. [PA 2015] Rozstaw szyn
[PA 2015] Rozstaw szyn
Description
Given a tree with nodes and leaf nodes, where the leaf nodes are nodes to . Each leaf node has a weight .
You need to assign a weight to each of the remaining 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 , representing the number of nodes and the number of leaf nodes.
The next lines each contain two positive integers , indicating that there is an edge between and .
The next lines each contain one positive integer, in order , 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 of the testdata, , , , .
Translated by ChatGPT 5
京公网安备 11011102002149号