AtCoder Beginner Contest 416
F
Paint Tree 2
AtCoder
⌀ 2115
原题
← DG →
翻译中…

Problem Statement

翻译中…

You are given a tree $T$ with $N$ vertices numbered from $1$ to $N$, and an integer $K$. The $i$-th edge $(1\le i\le N - 1)$ connects vertices $U_i$ and $V_i$. Also, vertex $i$ $(1\le i\le N)$ has an integer $A_i$ written on it. Initially, all vertices are colored white.

翻译中…

You perform the following action at least $0$ times and at most $K$ times:

翻译中…
  • Choose a path in tree $T$ such that all vertices on the path are colored white. Then, color all vertices on the chosen path black.
翻译中…

Find the maximum possible sum of integers written on vertices that are colored black after finishing the actions.

翻译中…

Constraints

翻译中…
  • $2\le N\le 2\times 10^5$
  • $1\le K\le 5$
  • $1\le A_i\le 10^9$
  • $1\le U_i < V_i \le N$
  • The given graph is a tree.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N K
A_1 A_2 \ldots A_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

翻译中…

Output

翻译中…

Output the answer.

翻译中…

样例

Input #1
4 1
1 2 4 8
1 2
1 3
1 4
Output #1
13
If you choose the path with vertices $3$ and $4$ as endpoints, you can color vertices $1,3,4$ black. In this case, the sum of integers written on the colored vertices is $1+4+8=13$. You cannot make the sum of integers written on black vertices greater than $13$, so output $13$.
Input #2
7 2
1 2 3 4 5 6 7
1 2
1 3
2 4
2 5
3 6
3 7
Output #2
27
For example, you can make the sum of integers written on black vertices $27$ by performing operations as follows: - Choose the path with vertices $4$ and $5$ as endpoints. Color vertices $2,4,5$ black. - Choose the path with vertices $6$ and $7$ as endpoints. Color vertices $3,6,7$ black. You cannot make the sum of integers written on black vertices greater than $27$, so output $27$.
Input #3
11 3
1 9 1 3 7 9 10 9 7 3 4
7 8
2 7
5 7
3 4
7 11
1 9
1 10
3 6
1 7
3 7
Output #3
52
题目信息
题号
abc416_f
时间限制
3 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。