Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Premier Round 1
  3. 2E

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

2E. Merge Three Vertices

2500 pts·2s·256 MB

Input

Output

Samples

Input
t
3
Case 1
3
-100 5 -100
1 2
2 3
Case 2
4
10 7 8 9
1 2
1 3
1 4
Case 3
5
-1 -1 3 -1 -1
1 2
2 3
3 4
4 5
Explanation
Case 1
Case 2

Don't apply any operations.

Case 3
Output
Case 1
5
Case 2
34
Case 3
3

You are given a tree with nnn vertices. Each vertex vvv has an integer value ava_vav​ (it may be negative).

In one operation, choose three distinct vertices v,u,wv,u,wv,u,w such that vvv is adjacent to uuu and uuu is adjacent to www, so v−u−wv-u-wv−u−w is a path of length 222 and uuu is the middle vertex.

Then vertices vvv and www are deleted, and vertex uuu stays. For every former neighbor xxx of vvv with x≠ux \ne ux=u, add an edge between xxx and uuu. For every former neighbor yyy of www with y≠uy \ne uy=u, add an edge between yyy and uuu. The value of uuu remains aua_uau​. After this operation, the graph is still a tree.

You may perform any number of operations (possibly zero). Your goal is to maximize the sum of values of the remaining vertices.

Constraints

  • 1≤t≤1001 \le t \le 1001≤t≤100
  • 1≤n≤2×1051 \le n \le 2 \times 10^51≤n≤2×105
  • −109≤av≤109-10^9 \le a_v \le 10^9−109≤av​≤109
  • The sum of nnn over all test cases does not exceed 2×1052 \times 10^52×105
tna1a2⋯anv1u1v2u2⋮vn−1un−1}×tt \\[0.5em] \left. \begin{array}{l} n \\[0.3em] a_1 \quad a_2 \quad \cdots \quad a_n \\[0.3em] v_1 \quad u_1 \\ v_2 \quad u_2 \\ \vdots \\ v_{n-1} \quad u_{n-1} \end{array} \right\} \times ttna1​a2​⋯an​v1​u1​v2​u2​⋮vn−1​un−1​​⎭⎬⎫​×t
ans}×t\left. \begin{array}{l} \text{ans} \end{array} \right\} \times tans​}×t

Apply an operation on path 1−2−31-2-31−2−3.

Apply an operation on path 2−3−42-3-42−3−4 to obtain path 1−3−51-3-51−3−5, and then apply an operation on this path.