I. Tree Matching
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tree with $$$N$$$ vertices numbered $$$1, 2, \ldots N$$$. The $$$i-$$$th vertex has a color $$$C_i$$$. Each edge of the tree has some positive weight. Let's define the value of a pair $$$(x, y)$$$ of distinct vertices $$$x$$$ and $$$y$$$ as the sum of the weights of the edges that appear on the unique simple path between $$$x$$$ and $$$y$$$. Let's call such a pair valid if $$$x$$$ and $$$y$$$ have different colors. You want to select some ($$$\geq 0$$$) valid pairs of vertices, such that each vertex is contained in at max one pair, and the sum of values of all the selected pairs is maximized. Print this maximum value.

Input
  • The first line contains $$$T$$$, the number of testcases. Then the testcases follow:
    • The first line of each testcase contains a single integer $$$N$$$
    • The next line contains the colors of the nodes $$$C_1, C_2, \ldots C_N$$$
    • Each of the next $$$N - 1$$$ lines contains $$$3$$$ integers, $$$u, v$$$ and $$$w$$$, denoting an edge between $$$u$$$ and $$$v$$$ of weight $$$w$$$.

Constraints

  • $$$1 \le T \le 500$$$
  • $$$1 \le N \le 2000$$$
  • $$$1 \le C_i \le N$$$
  • $$$1 \le w \le 10^5$$$
  • The sum of $$$N$$$ over all testcases doesn't exceed $$$2000$$$
Output

For each testcase, print the answer on a new line.

Example
Input
2
4
1 1 2 2
1 2 3
2 3 1
2 4 5
2
1 1
1 2 1
Output
9
0