Willow

Two players choose starting cities on a tree with coins and alternately collect cities, each road usable once, and Hanaa maximizes the final score difference.

Hard9Game theoryTreeDynamic programmingNo attempts yetTime limit5sMemory limit512 MB

Problem

Hanaa and Sherine play a game called Willow. The board has NN cities, city ii holds CiC_i coins, and N1N - 1 two-way roads connect the cities so that every city can be reached from every other city.

Hanaa first picks a city to start from. Sherine then sees that choice and picks her own starting city, which may be the city Hanaa picked. After that the two take turns, Hanaa first.

On her turn a player takes every coin left in the city she is standing on. There is nothing to take if the city started empty, or if one of the players has already started a turn there. She then has to travel to a neighboring city along a road that has not been used yet. If no unused road leads out of her city, she stays where she is. Each road can be used at most once, so once one player has driven a road, the other cannot use it either. The game ends when neither player has a coin to take and neither can move.

When the game ends, each player's score is the number of coins she collected minus the number of coins her opponent collected. A player who collects fewer coins ends with a negative score. Both players maximize their own score. If both play as well as possible, what is the highest score Hanaa can get?

Input

The first line has the number of test cases TT. TT test cases follow.

The first line of each test case has the number of cities NN. The ii-th of the next NN lines has CiC_i, the number of coins in city ii. The ii-th of the following N1N - 1 lines (ii starts at 1) has a single integer jj, which means that a road joins city ii and city jj. Always 1i<jN1 \le i < j \le N, and every city can be reached from every other city when the game starts.

The limits are as follows.

  • 1T501 \le T \le 50
  • 2N802 \le N \le 80
  • 0Ci100000 \le C_i \le 10000

Output

For each test case, print one line Case #x: y, where xx is the test case number starting at 1 and yy is the highest score Hanaa can get.