AlgoUniversity
  • Go Back
Discussion
Tree Decrements :

Author

Ayush Gangwani

Difficulty Level : Medium

Submissions : 69

Asked In : Tiktok

Marks :15

: 2 | : 0

You are given a tree $$$T$$$ containing $$$n$$$ nodes. There is a value associated with each node. You can perform the following operation on this tree:

  • Select two nodes and decrement thier values by 1 at a cost equal to the distance between the two nodes, i.e., the number of edges in the simple path between them. It is possible to select the same node for the operation and decrease its value by $$$2$$$ at a cost of $$$0$$$.

Find the minimum cost to decrease the values of all the nodes to $$$0$$$ using the given operation. It is guaranteed that the values of all the nodes can be decreased to exactly zero.

Input

The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The description of $$$t$$$ testcase follows.

The first line of each test case contains a single integer $$$n$$$ — the number of vertices in the tree $$$(2 \le n \le 10^5)$$$.

The second line contains $$$n$$$ space separated integers $$$a_1,a_2,...a_n$$$ $$$(1 \le a_i \le 10^9)$$$ denoting the values of the nodes of the tree.

The next $$$n-1$$$ lines describe the tree. Each contains two space separated integers $$$u$$$ and $$$v$$$ $$$(1 \le u, v \le n; $$$ $$$u \neq v \hspace{1 pt};)$$$ denoting an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that these edges form a tree.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output

For each test case, print the minimum cost to decrease the values of all the nodes to $$$0$$$ using the given operation.

Example

Input
3
3
2 1 1
1 2
1 3
3
3 1 2
1 2
1 3
5
3 2 4 2 5
1 2
1 3
3 4
3 5
Output
2
1
2

Note

In sample test case 1, the optimal strategy is to choose the nodes $$$(1, 2)$$$ and $$$(1, 3)$$$, respectively, in the two operations.

In sample test case 2, the optimal strategy is to choose nodes $$$1$$$ and $$$2$$$ at cost $$$1$$$. The values become $$$[2, 0, 2]$$$. Now nodes $$$1$$$ and $$$1$$$, followed by $$$3$$$ and $$$3$$$, can he chosen, each with a cost of $$$0$$$. Thus the total cost is $$$1 + 0 + 0 = 1$$$.

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.