There is a rooted tree of
vertices rooted at vertex . Each vertex has a value associated with it.
You choose a vertex (possibly the root) from the tree and remove all vertices on the path from the root to the vertex , also including . This will result in a forest of zero or more connected components.
The beauty of a connected component is the of the values of all vertices in the component. Find the maximum value of the sum of beauties of the obtained connected components for any choice of .
Here, stands for Greatest Common Divisor.
Input Format
- The first line contains a single integer — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer — the size of the tree.
- The second line of each test case contains space-separated integers denoting the values associated with each vertex.
- The next lines contain two space-separated integers and — denoting an undirected edge between nodes and .
It is guaranteed that the edges given in the input form a tree.
Output Format
For each test case output the maximum value of the sum of beauties of the obtained connected components for any choice of .
Constraints
- and
- It is guaranteed that the edges given in the input form a tree.
- The sum of over all test cases does not exceed
Sample Input 1
1
10
15 30 15 5 3 15 3 3 5 5
1 2
1 5
2 3
2 4
5 6
5 7
5 8
7 9
7 10
Sample Output 1
33
Explanation
The tree from the sample is as follows.
If vertex is chosen, vertices , and are removed.
The resulting forest contains five connected components and .
The beauties of the connected components are , , , and respectively. Thus the answer is .
It can be shown that this is the maximum value possible for any choice of .
No comments:
Post a Comment