#abc348e. E - Minimize Sum of Distances
E - Minimize Sum of Distances
Score: points
问题陈述
给定一个有 个顶点的树。顶点编号为 到 ,第 条边连接顶点 和 。
你还给定了一个正整数序列 ,长度为 。设 为顶点 和 之间的边数,对于 ,设 $\displaystyle f(x) = \sum_{i=1}^{N} (C_i \times d(x, i))$。找出 。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a tree with vertices. The vertices are numbered to , and the -th edge connects vertices and .
You are also given a sequence of positive integers of length . Let be the number of edges between vertices and , and for , let $\displaystyle f(x) = \sum_{i=1}^{N} (C_i \times d(x, i))$. Find .
Constraints
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
Output
Print the answer in one line.
Sample Input 1
4
1 2
1 3
2 4
1 1 1 2
Sample Output 1
5
For example, consider calculating . We have .
Thus, $f(1) = 0 \times 1 + 1 \times 1 + 1 \times 1 + 2 \times 2 = 6$.
Similarly, . Since is the minimum, print 5
.
Sample Input 2
2
2 1
1 1000000000
Sample Output 2
1
, which is the minimum.
Sample Input 3
7
7 3
2 5
2 4
3 1
3 6
2 1
2 7 6 9 3 4 6
Sample Output 3
56