P19569 [HackerEarth] View Russian Translation
普及
题目描述

View Russian Translation

You are given a tree T of n nodes. The nodes in the tree are numbered 1 through n. Let's consider some set of different colors numbered 1 through n - 1. Each color costs differently. You are given a sequence cost_1, cost_2, ..., cost_{n-1} of the color costs.

For each edge, you should choose exactly one color in such a way that no two incident edges have the same color and the sum of the chosen color costs is as minimal as possible.

More formally, let's assign some unique integer from 1 to n - 1 to each of the edges of T. A sequence c_1, c_2, ..., c_{n-1} (1 ≤ c_k ≤ n - 1) is called an edge coloring of the tree; c_k is called the color of the k'th edge. An edge coloring is called valid, if there is no such a pair of edges, that share a common node and have the same color.

The cost of an edge coloring is the sum of the color costs for all the edges of the tree. Your task is to find the cost of the cheapest valid edge coloring.

Input format
The first line contains one integer t denoting the number of test cases in the input.

The first line of each test case description contains an integer n.

The second line contains n - 1 positive integers denoting cost_1, cost_2, ..., cost_{n-1}.

Each of the next n - 1 lines contains two integers u and v denoting an edge of the tree.

Output format
For each test case, output a single integer: the cost of the cheapest valid edge coloring.

Constraints

1 ≤ t ≤ 16
1 ≤ n ≤ 100
1 ≤ cost_{color} ≤ 1000

SAMPLE INPUT
2
5
1 2 3 4
1 2
2 3
2 4
4 5
6
16 4 1 8 2
1 3
3 5
6 3
3 2
4 3

SAMPLE OUTPUT
7
31

Explanation

Let's number the edges of the sample trees as they go in the input.

The first test case of the sample input: one of the optimal edge colorings is (1, 2, 3, 1).

The second test case of the sample input: one of the optimal edge colorings is (1, 2, 3, 4, 5).

输入格式

见题面

输出格式

见题面

样例输入
16 25 595 587 873 133 732 286 921 148 317 813 688 956 171 53 211 177 538 438 611 408 862 717 266 314 1 4 1 8 1 14 1 20 1 21 1 18 1 17 1 3 1 7 1 11 1 25 1 12 1 10 1 13 1 23 1 9 1 2 1 15 1 19 1 22 1 6 1 5 1 24 1 16 25 137 499 143 10 87 177 680 114 70 430 589 663 24 68 512 757 888 10 201 813 746 572 292 600 2 3 1 4 1 8 1 20 2 21 2 19 2 9 1 12 2 5 1 18 2 11 2 7 1 2 2 17 1 16 1 22 2 25 2 23 1 6 1 14 1 24 2 15 1 10 2 13 25 290 405 484 565 393 382 656 422 667 814 786 763 32 3 399 375 437 622 491 799 911 561 735 896 2 3 15 16 23 24 19 20 5 6 4 5 9 10 24 25 3 4 17 18 10 11 6 7 22 23 18 19 7 8 16 17 14 15 1 2 20 21 13 14 11 12 12 13 8 9 21 22 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 14 15 17 18 9 10 10 11 8 9 22 23 12 13 3 4 4 5 16 17 23 24 15 16 2 3 20 21 6 7 24 25 5 6 21 22 19 20 18 19 1 2 11 12 13 14 7 8 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 1 2 1 6 13 25 1 15 5 19 3 5 12 14 3 18 7 9 11 16 6 21 1 3 7 13 10 11 2 4 2 7 19 22 5 10 1 23 11 24 1 8 3 20 1 12 9 17 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 8 12 15 17 1 5 11 14 4 8 3 6 8 11 9 24 1 3 4 15 20 21 1 25 5 10 3 22 7 23 1 2 16 18 16 19 14 20 10 13 4 9 1 7 10 16 1 4 25 90 8 528 245 139 45 726 645 979 278 230 599 831 198 417 274 194 578 779 374 431 144 486 169 4 5 19 20 16 17 7 8 2 3 8 9 1 2 20 21 15 16 13 14 3 4 6 7 17 18 21 22 22 23 14 15 11 12 12 13 5 6 24 25 18 19 9 10 10 11 23 24 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 1 6 2 21 2 3 2 15 1 22 1 24 2 17 1 4 1 14 2 13 1 2 2 11 2 25 2 7 1 20 2 19 1 10 1 12 2 5 2 9 1 18 2 23 1 16 1 8 25 26 686 602 10 392 254 719 309 857 731 921 870 133 342 833 163 218 627 983 970 852 125 957 988 2 23 1 10 1 22 1 6 1 24 2 15 2 3 1 2 1 16 2 21 1 20 1 4 2 5 2 17 2 19 1 18 2 11 2 13 2 7 2 9 1 12 2 25 1 8 1 14 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 1 14 1 18 1 17 1 2 1 15 1 9 1 16 1 22 1 21 1 19 1 10 1 6 1 8 1 12 1 3 1 4 1 7 1 24 1 20 1 25 1 11 1 23 1 5 1 13 25 606 290 998 651 593 899 709 205 434 498 745 803 982 26 350 101 653 304 745 798 843 657 706 499 6 9 9 15 1 23 14 21 1 14 13 22 9 10 12 20 16 18 1 5 1 3 13 25 3 11 4 6 5 16 2 7 5 24 4 13 8 19 4 8 16 17 1 4 8 12 1 2 25 140 914 1 49 867 883 976 200 723 708 197 712 374 714 765 862 765 421 645 139 728 295 316 402 1 7 1 13 1 19 1 8 1 12 1 25 1 11 1 5 1 17 1 4 1 24 1 16 1 20 1 10 1 15 1 23 1 3 1 14 1 18 1 2 1 6 1 22 1 9 1 21 25 218 628 391 496 535 459 637 737 333 540 823 372 125 140 380 952 847 837 870 604 214 215 753 242 5 9 12 25 4 11 4 14 22 24 19 23 15 17 5 12 8 21 14 20 11 13 4 5 17 19 12 18 7 22 1 7 2 3 5 6 2 16 3 4 1 2 2 15 6 8 9 10 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 11 1 10 1 18 2 21 2 23 1 16 1 4 1 12 2 13 2 3 2 19 1 22 1 6 2 7 2 9 2 5 1 2 1 24 2 15 2 25 1 20 1 14 2 17 1 8 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 1 17 1 18 1 10 1 16 1 24 1 3 1 14 1 21 1 11 1 9 1 4 1 7 1 20 1 22 1 8 1 15 1 6 1 12 1 25 1 2 1 23 1 13 1 19 1 5 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 19 20 10 11 16 17 17 18 15 16 21 22 9 10 14 15 13 14 20 21 6 7 1 2 12 13 8 9 4 5 7 8 23 24 24 25 2 3 18 19 3 4 5 6 11 12 22 23
样例输出
25 45 3220 4324 3440 13 45 398 13 1989 1453 3389 1932 25 25 20