In the Ninja world, assets owned by a person are represented by a tree, where the total asset value is equal to the sum of all its vertices.
A wealthy father wants to split all his assets between his two children, he has appointed you to help him. You are given a tree containing ‘N’ vertices, vertices are numbered from 0 to N-1, you are also given an array ‘Assets’ denoting the asset-value of ith vertex.
Splitting of assets can simply be done by removing an edge from the tree, this results in the formation of two trees. You have to remove an edge such that the partition is as fair as possible. In a fair partition, the difference between the assets of the children should be as small as possible.
Return the minimum possible difference between the asset values of the children after partition.
For Example :If N = 5 and Assets = { 10, 20, 30, 40, 50 } and tree is denoted as:

Then we have 4 choices of deletion:
Edge 0-1 is deleted: now, the one of the tree contains node-1 and node-3, and the other contains node-0, node-2 and node-3, subtree difference is = 90 - 60 = 30
Edge 1-3 is deleted: subtree difference is = 110 - 40 = 70
Edge 0-2 is deleted: subtree difference is = 80 - 70 = 10
Edge 2-4 is deleted: subtree difference is = 100 - 50 = 50
Clearly removing the edge between node-0 and node-2 leads to the most optimal partition where the difference is equal to 10. Hence we return the value 10.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’ denoting the number of assets.
The next line contains N integers, where Assets[i] denotes the asset-value of the ith asset.
The next N-1 lines each contain two integers ‘U’ and ‘V’ denoting that there is an edge between node-U and node-V.
Output Format :
For each test case, print the lowest possible difference between assets after splitting.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
2 ≤ N ≤ 200
0 ≤ Assets[i] ≤ 10^6
0 ≤ U, V ≤ N-1
Time limit: 1 sec
2
5
10 20 30 40 50
0 1
2 0
1 3
4 2
2
10 10
0 1
10
0
For test case 1 :
We will return 10 because:
Edge 0-1 is deleted: subtree difference is = 90 - 60 = 30
Edge 1-3 is deleted: subtree difference is = 110 - 40 = 70
Edge 0-2 is deleted: subtree difference is = 80 - 70 = 10
Edge 2-4 is deleted: subtree difference is = 100 - 50 = 50
For test case 2 :
We will return 0 because:
If the only edge, that is the edge between node-0 and node-1 is deleted, both the children get assets of the same worth.
2
2
50 50
1 0
2
0 100
0 1
0
100
Try removing edges one by one to find the optimal answer.
Try to remove the edges one by one to find the optimal partition.
The steps are as follows :
O( N ^ 2 ), where N denotes the number of assets
We remove the edges one by one, and each time calculate the sum of one of the partition trees formed. Consider a bamboo tree (tree in form of a straight-chain), we will be applying operations of the order of ~N each time to calculate the sum of one of the partitions for each edge removal.
Hence the time complexity is O( N^2 ).
O( N ^ 2 ), where N denotes the number of assets
We remove edges one by one, and each time create an adjacency list of order ~N to apply DFS.
Hence the space complexity is O( N^2 ).