Compressed Tree
Contest: Educational Codeforces Round 158 (Rated for Div. 2)
Difficulty rating: 2200
Tags: dfs and similar, dp, graphs, greedy, sortings, trees

You are given a tree consisting of $$$n$$$ vertices. A number is written on each vertex; the number on vertex $$$i$$$ is equal to $$$a_i$$$.

You can perform the following operation any number of times (possibly zero):

- choose a vertex which has at most $$$1$$$ incident edge and remove this vertex from the tree.

Note that you can delete all vertices.

After all operations are done, you're compressing the tree. The compression process is done as follows. While there is a vertex having exactly $$$2$$$ incident edges in the tree, perform the following operation:

- delete this vertex, connect its neighbors with an edge.

It can be shown that if there are multiple ways to choose a vertex to delete during the compression process, the resulting tree is still the same.

Your task is to calculate the maximum possible sum of numbers written on vertices after applying the aforementioned operation any number of times, and then compressing the tree.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) — the number of vertices.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$).

Each of the next $$$n - 1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$v_i$$$ and $$$u_i$$$, the labels of vertices it connects ($$$1 \le v_i, u_i \le n$$$, $$$v_i \ne u_i$$$). These edges form a tree.

Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$5 \cdot 10^5$$$.

Output format:
For each test case, print a single integer — the maximum possible sum of numbers written on vertices after applying the aforementioned operation any number of times, and then compressing the tree.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Example input:
3
4
1 -2 2 1
1 2
3 2
2 4
2
-2 -5
2 1
7
-2 4 -2 3 3 2 -1
1 2
2 3
3 4
3 5
4 6
4 7

Example output:
3
0
9

Editorial:
We can use dynamic programming on tree to solve this problem. After we're done removing vertices, a vertex will be left in the tree after the compression process if and only if its degree is not $$$2$$$. So, we can try to choose the number of children for each vertex in dynamic programming, and depending on this number of children, the vertex we're considering is either removed from the tree during the compression, or left in the tree.
But if we want to use dynamic programming, we have to root the tree at some vertex, so the degree of each vertex depends not only on the number of its children we left in the tree, but also on whether there exists an edge leading from the ancestor to that vertex. So, for each vertex, we will consider two values of dynamic programming: the best answer for its subtree if there is an edge leading into it from the parent (i. e. we haven't deleted everything outside of that subtree), and the answer if there is no edge leading from the parent (i. e. we deleted everything outside of that subtree).
Let the first of these two values be $$$dp_v$$$ — the maximum answer for subtree of $$$v$$$ if there is at least one non-deleted vertex not in the subtree of vertex $$$v$$$ (i.e there is an edge from vertex $$$v$$$ up the tree).
Let's look at the dp transitions, depending on the number of children of vertex $$$v$$$ that are not deleted:
• $$$0$$$: $$$dp_v = \max(dp_v, a_v)$$$;
• $$$1$$$: $$$dp_v = \max(dp_v, \max\limits_u dp_u)$$$ ($$$a_v$$$ is not taken into account, because $$$v$$$ has $$$2$$$ incident edges and will be compressed);
• at least $$$2$$$: $$$dp_v = \max(dp_v, a_v + \sum\limits_u dp_u)$$$.
Then let us consider the case when the resulting tree is in the subtree of some vertex $$$v$$$. We can update the global answer depending on the number of children of vertex $$$v$$$ that are not deleted:
• $$$0$$$: $$$ans = \max(ans, a_v)$$$;
• $$$1$$$: $$$ans = \max(ans, a_v + \max\limits_u dp_u)$$$;
• $$$2$$$: $$$ans = \max(ans, \max\limits_{u_1, u_2} dp_{u_1} + dp_{u_2})$$$ ($$$a_v$$$ is not taken into account, because $$$v$$$ has $$$2$$$ incident edges and will be compressed);
• at least $$$3$$$: $$$ans = \max(ans, a_v + \sum\limits_u dp_u)$$$.
For convenience, we can calculate auxiliary dynamic programming for children of vertex $$$v$$$: $$$sum_i$$$ is the maximum sum of $$$dp_u$$$ for $$$i$$$ children. From above written transitions, we can see that, $$$sum_3$$$ can store maximum sum for at least $$$3$$$ children.
Don't forget that we can delete all vertices, so the answer is at least $$$0$$$.