#P1466D. 13th Labour of Heracles
13th Labour of Heracles
Description
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with weighted vertices. A tree is a connected graph with edges.
Let us define its -coloring as an assignment of colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all colors.
A subgraph of color consists of these edges from the original tree, which are assigned color , and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals .
There is also a value of a -coloring, which equals the sum of values of subgraphs of all colors. Given a tree, for each from to calculate the maximal value of a -coloring.
In the first line of input, there is a single integer () denoting the number of test cases. Then test cases follow.
First line of each test case contains a single integer (). The second line consists of integers (), equals the weight of -th vertex. In each of the following lines, there are two integers , () describing an edge between vertices and . It is guaranteed that these edges form a tree.
The sum of in all test cases will not exceed .
For every test case, your program should print one line containing integers separated with a single space. The -th number in a line should be the maximal value of a -coloring of the tree.
Input
In the first line of input, there is a single integer () denoting the number of test cases. Then test cases follow.
First line of each test case contains a single integer (). The second line consists of integers (), equals the weight of -th vertex. In each of the following lines, there are two integers , () describing an edge between vertices and . It is guaranteed that these edges form a tree.
The sum of in all test cases will not exceed .
Output
For every test case, your program should print one line containing integers separated with a single space. The -th number in a line should be the maximal value of a -coloring of the tree.
Note
The optimal -colorings from the first test case are the following:

In the -coloring all edges are given the same color. The subgraph of color contains all the edges and vertices from the original graph. Hence, its value equals .

In an optimal -coloring edges and are assigned color . Edge is of color . Hence the subgraph of color consists of a single connected component (vertices ) and its value equals . The subgraph of color contains two vertices and one edge. Its value equals .

In an optimal -coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: , , .