| Codeforces Round 1087 (Div. 2) |
|---|
| Finished |
You are given a tree$$$^{\text{∗}}$$$ with $$$n$$$ vertices numbered from 1 to $$$n$$$, where each vertex $$$i$$$ has an initial value $$$a_i$$$. You perform $$$k$$$ operations. The total is $$$0$$$ initially. In each operation:
$$$^{\text{∗}}$$$A tree is a connected graph without cycles.
$$$^{\text{†}}$$$A leaf is any vertex without children.
$$$^{\text{‡}}$$$A subtree of vertex $$$v$$$ is the subgraph of $$$v$$$, all its descendants, and all the edges between them.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10 ^ 4$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 3 \cdot 10^5$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial values of vertices.
Each of the following $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$, denoting an edge connecting vertex $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\cdot 10 ^ 5$$$.
For each test case, output an integer — the maximum possible total.
75 419 20 39 81 21 21 32 42 55 312 21 39 8 211 21 33 42 55 2129 216 32 83 2211 21 33 44 55 315 15 15 15 151 21 31 41 51 112 11 10000000001 27 28 3 5 7 9 1 64 37 55 22 33 66 1
161101681601100000000032
In the first test case:
After rooting the tree at $$$1$$$, for every non-leaf vertex $$$u$$$, move its value to the leaf in its subtree that is farthest from $$$u$$$ (breaking ties by smallest index). Under this rooting:
After redistribution under this rooting, the values of vertices $$$4$$$ and $$$5$$$ remain at their positions.
After redistribution, the value at vertex $$$5$$$ remains unchanged.
The total obtained is $$$19 + 39 + 101 + 2 = 161$$$.
In the sixth test case, choose vertex $$$2$$$ as the root. Add its value to the total, obtaining $$$1\,000\,000\,000$$$.
| Name |
|---|


