You are given an undirected tree of
nodes. A positive weight is assigned to the node for all .
For all , the -th edge connects the nodes and , and has a restriction value of .
An array consisting of non-negative integers is called valid if:
- For all , the condition holds.
The profit of a valid array is defined as
Find the maximum possible value of for some valid array .
Input Format
- The first line of input contains a single integer - the number of test cases. The description of test cases follow.
- The first line of each test case contains a single integer - the number of nodes in the tree.
- The second line of each test case contains integers denoting the weights assigned to every node.
- The next lines contain three space-separated integers each, with the -th line containing , , and .
Output Format
For each test case, output the maximum possible value of .
Constraints
- and
- It is guaranteed that
- It is guaranteed that the input forms a valid tree for all test cases.
- The sum of over all test cases do not exceed
Sample Input 1
3
3
6 9 4
1 2 2
2 3 1
6
1 2 2 6 100 100
1 2 9
2 3 17
2 4 3
3 5 1
3 6 4
3
120734269 1000000000 1
1 2 300000000
2 3 300000000
Sample Output 1
16
527
300000000000000000
Explanation
Test case-1: All valid arrays are:
- ;
- ;
- ;
- ;
- ;
- ;
The maximum profit among all valid arrays is , which is the answer.
Test case-2: The optimal is .
Test case-3: The optimal is .
No comments:
Post a Comment