Paimon's Tree
Time limit4sMemory limit1024 MB
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 initially white vertices in her left pocket and decided to play with it. A tree with nodes is a connected undirected graph with edges.
Paimon gives you an integer sequence of length . First, select a vertex of the tree and paint it black. Then perform the following operation times.
During the -th operation, select a white vertex that is directly connected to a black vertex by an edge. Set the weight of that edge to , and paint black. After these 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 (), the number of test cases. Each test case is described as follows.
The first line contains an integer (), the length of the sequence.
The second line contains integers ().
The next lines contain the edges. The -th of these lines contains two integers and (), meaning that vertices and are connected by an edge.
It is guaranteed that at most 10 test cases satisfy .
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 . This produces the weighted tree shown in the image below. The longest simple path has length .
