#P7636. [2018年杭电多校]Rikka with Treasure

[2018年杭电多校]Rikka with Treasure

Rikka with Treasure

Problem Description

By coincidentally, Rikka found a treasure map. She wants to employ several expedition teams to help her find the treasure. There are nn caves in the treasure maps and n1n-1 undirected roads between the caves. For each cave pair (i,j)(ij)(i,j)(i \neq j), it is guaranteed that there is exactly one simple path between them, and the distance d(i,j)d(i,j) between these two caves is defined by the number of caves in this path. For example, in the following treasure map, d(1,3)=3,d(2,5)=2d(1,3) = 3, d(2, 5) = 2 and d(1,4)=4d(1,4)=4.

Some of the caves have depots near them. Each expedition team can explore all the caves on the path (sis_i, tit_i)(sis_i may be equal to tit_i) which satisfies there are depots near sis_i, tit_i, and si,tis_i,t_i are stipulated by Rikka. After the exploration, Rikka needs to pay d(si,ti)×Cd(s_i,t_i) \times C dollars to this team. Each cave has treasure in it and the treasure in the iith cave worths aia_i dollars. Rikka can get iith treasure if and only if there are at least one expedition teams which have explored iith cave. For example, in the previous treasure map, if ai=i,C=1a_i=i,C=1, all caves have depots near them, Rikka employs two expedition teams and the first team explores path (1,5)(1,5), the second team explores (2,3)(2,3), then Rikka need pay 33 dollars to the first team, 22 dollars to the second team, and Rikka can get treasure 1,2,3,51,2,3,5. So the Rikka's income will be 1+2+3+532=61+2+3+5-3-2=6 dollars. Now, for each KK from 11 to nn, Rikka wants to calculate the maximum possible income she can get if she employs at most KK expedition teams.

Input

The first line contains a single integer t(1t103)t(1 \leq t \leq 10^3), the number of the testcases. For each testcase, the first line contains three integers n,C(1n3000,1C107)n,C(1 \leq n \leq 3000, 1 \leq C \leq 10^7). The second line contains nn 01 integers pip_i. pi=1p_i=1 if and only if there is a depot near the iith cave. The third line contains nn integers ai(1ai107)a_i(1 \leq a_i \leq 10^7), which describes the value of the treasure. Then n1n-1 lines follow, each line contains two integers ui,viu_i,v_i, which describes one road in the map. The input guarantees that there are at least one depots and there are at most 55 testcases with n>200n > 200.

Output

For each testcase, output a single line with a nn integers, the maximum possible income Rikka can get if she employs at most ii expedition teams.

Sample Input

5

5 1

1 0 1 0 1

1 2 3 4 5

1 2

2 3

2 5

3 4

5 1

1 0 1 1 1

1 2 3 4 5

1 2

2 3

2 5

3 4

5 1

1 1 1 1 1

1 2 3 4 5

1 2

2 3

2 5

3 4

5 2

1 0 1 0 1

1 2 3 4 5

1 2

2 3

2 5

3 4

5 1

1 1 1 1 1

1 2 3 4 5

1 2

1 3

1 4

1 5

Sample Output

7 7 7 7 7

10 10 10 10 10

10 10 10 10 10

4 4 4 4 4

7 9 10 10 10

Source

2018 Multi-University Training Contest 9