CF BUDDY
← Problems·

1988D · The Omnipotent Monster Killer

2000 · brute force, dfs and similar, dp

Problem: You, the monster killer, want to kill a group of monsters. The monsters are on a tree with nn vertices. On vertex with number ii (1in1\le i\le n), there is a monster with aia_i attack points. You want to battle with monsters for 1010010^{100} rounds.

In each round, the following happens in order:

  1. All living monsters attack you. Your health decreases by the sum of attack points of all living monsters.
  2. You select some (possibly all or none) monsters and kill them. After being killed, the monster will not be able to do any attacks in the future.

There is a restriction: in one round, you cannot kill two monsters that are directly connected by an edge.

If you choose what monsters to attack optimally, what is the smallest health decrement you can have after all rounds?

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). Description of the test cases follows.

The first line of each test case contains an integer nn (1n31051\le n\le 3\cdot 10^5).

The second line of each test case contains nn integers a1,,ana_1,\ldots,a_n (1ai10121\le a_i\le 10^{12}).

The following n1n-1 lines each contain two integers x,yx,y (1x,yn1\le x,y\le n), denoting an edge on the tree connecting vertex xx and yy.

It is guaranteed that the sum of nn over all test cases does not exceed 31053\cdot 10^5.

Output Format: For each test case, print one integer: the minimum possible health decrement.

Note: In the first test case, an optimal sequence of operations would be:

  • In the first round: first, receive the attack from the monster on vertex 11, so your health decreases by 101210^{12}. Then kill the monster on vertex 11.
  • In the second round to the 1010010^{100}-th round: all monsters have been killed, so nothing happens.

The total health decrement is 101210^{12}.

In the second test case, an optimal sequence of operations would be:

  • In the first round: first, receive the attack from the monster on vertex 1,2,3,4,51,2,3,4,5, so your health decreases by 47+15+32+29+23=14647+15+32+29+23=146. Then kill the monsters on vertex 1,4,51,4,5.
  • In the second round: first, receive the attack from the monster on vertex 2,32,3, so your health decreases by 15+32=4715+32=47. Then kill the monsters on vertex 2,32,3.
  • In the third round to the 1010010^{100}-th round: all monsters have been killed, so nothing happens.

The total health decrement is 193193.

In the third test case, an optimal sequence of operations would be:

  • In the first round: first, receive the attack from the monster on vertex 1,2,3,4,5,6,71,2,3,4,5,6,7, so your health decreases by 8+10+2+3+5+7+4=398+10+2+3+5+7+4=39. Then kill the monsters on vertex 1,3,6,71,3,6,7.
  • In the second round: first, receive the attack from the monster on vertex 2,4,52,4,5, so your health decreases by 10+3+5=1810+3+5=18. Then kill the monsters on vertex 2,4,52,4,5.
  • In the third round to the 1010010^{100}-th round: all monsters have been killed, so nothing happens.

The total health decrement is 5757.

Sample Cases

Case 1

Input

3
1
1000000000000
5
47 15 32 29 23
1 2
1 3
2 4
2 5
7
8 10 2 3 5 7 4
1 2
1 4
3 2
5 3
6 2
7 5

Output

1000000000000
193
57

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback