#E. Minimize Sum of Distances

    Type: Default 1000ms 256MiB

Minimize Sum of Distances

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

[ABC348E] Minimize Sum of Distances

题目描述

给定一棵 NN 个顶点的树,每个点有一个点权 CiC_i。定义 d(a,b)d(a,b) 表示 aabb 之间的简单路径的边数(即距离),定义 $\displaystyle\ f(x)\ =\ \sum_{i=1}^{N}\ (C_i\ \times\ d(x,\ i))$ ,求  min1  v  N f(v)\displaystyle\ \min_{1\ \leq\ v\ \leq\ N}\ f(v)

输入格式

第一行一个整数 NN ,接下来 N1N-1 行每行两个整数 Ai,BiA_i,B_i 表示树边,最后一行 NN 个整数 CiC_i 表示点权。

输出格式

一个整数表示答案。

输入输出样例 #1

输入 #1

4
1 2
1 3
2 4
1 1 1 2

输出 #1

5

输入输出样例 #2

输入 #2

2
2 1
1 1000000000

输出 #2

1

输入输出样例 #3

输入 #3

7
7 3
2 5
2 4
3 1
3 6
2 1
2 7 6 9 3 4 6

输出 #3

56

说明/提示

数据范围

  • 1  N  1051\ \leq\ N\ \leq\ 10^5
  • 1  Ai, Bi  N1\ \leq\ A_i,\ B_i\ \leq\ N
  • 1  Ci  1091\ \leq\ C_i\ \leq\ 10^9

20250311集训

Not Attended
Status
Done
Rule
IOI
Problem
7
Start at
2025-3-11 19:30
End at
2025-3-11 21:30
Duration
2 hour(s)
Host
Partic.
9