Under construction

Still being built. Things may change

Paimon's Tree

Time limit4sMemory limit1024 MB

Summary
Choose a starting vertex and a growth order so the i-th added edge gets weight a_i, then maximize the diameter of the resulting weighted tree.
Level

Hard9 of 10

Topics
Dynamic programming, Tree, DFS
Solved
No attempts yet

Problem

Paimon found a tree with (n+1)(n + 1) initially white vertices in her left pocket and decided to play with it. A tree with (n+1)(n + 1) nodes is a connected undirected graph with nn edges.

Paimon gives you an integer sequence a1,a2,⋯ ,ana_1, a_2, \cdots, a_n of length nn. First, select a vertex of the tree and paint it black. Then perform the following operation nn times.

During the ii-th operation, select a white vertex xix_i that is directly connected to a black vertex yiy_i by an edge. Set the weight of that edge to aia_i, and paint xix_i black. After these nn operations, you get a tree whose edges all have weights.

If you select the vertices optimally, what is the maximum length of the diameter of the weighted tree? The diameter of a weighted tree is the longest simple path in the tree. The length of a simple path is the sum of the weights of the edges on that path.

Input

There are multiple test cases. The first line contains an integer TT (1≤T≤5×1031 \le T \le 5 \times 10^3), the number of test cases. Each test case is described as follows.

The first line contains an integer nn (1≤n≤1501 \le n \le 150), the length of the sequence.

The second line contains nn integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_n (1≤ai≤1091 \le a_i \le 10^9).

The next nn lines contain the edges. The ii-th of these lines contains two integers uiu_i and viv_i (1≤ui,vi≤n+11 \le u_i, v_i \le n + 1), meaning that vertices uiu_i and viv_i are connected by an edge.

It is guaranteed that at most 10 test cases satisfy n>20n > 20.

Output

For each test case, output one line containing the maximum length of the diameter of the tree.

Hint

In the first test case, the vertices are selected in the order 1,3,4,5,2,61, 3, 4, 5, 2, 6. This produces the weighted tree shown in the image below. The longest simple path has length 1616.

Examples1

  1. Example 1

    Input
    2
    5
    1 7 3 5 4
    1 3
    2 3
    3 4
    4 5
    4 6
    1
    1000000000
    1 2
    
    Expected output
    16
    1000000000