#P1746D. Paths on the Tree

    ID: 673 Type: RemoteJudge 3000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>dfs and similardpgreedysortingstrees*1900

Paths on the Tree

Description

You are given a rooted tree consisting of nn vertices. The vertices are numbered from 11 to nn, and the root is the vertex 11. You are also given a score array s1,s2,,sns_1, s_2, \ldots, s_n.

A multiset of kk simple paths is called valid if the following two conditions are both true.

  • Each path starts from 11.
  • Let cic_i be the number of paths covering vertex ii. For each pair of vertices (u,v)(u,v) (2u,vn2\le u,v\le n) that have the same parent, cucv1|c_u-c_v|\le 1 holds.
The value of the path multiset is defined as i=1ncisi\sum\limits_{i=1}^n c_i s_i.

It can be shown that it is always possible to find at least one valid multiset. Find the maximum value among all valid multisets.

Each test contains multiple test cases. The first line contains a single integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two space-separated integers nn (2n21052 \le n \le 2 \cdot 10^5) and kk (1k1091 \le k \le 10^9) — the size of the tree and the required number of paths.

The second line contains n1n - 1 space-separated integers p2,p3,,pnp_2,p_3,\ldots,p_n (1pin1\le p_i\le n), where pip_i is the parent of the ii-th vertex. It is guaranteed that this value describe a valid tree with root 11.

The third line contains nn space-separated integers s1,s2,,sns_1,s_2,\ldots,s_n (0si1040 \le s_i \le 10^4) — the scores of the vertices.

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

For each test case, print a single integer — the maximum value of a path multiset.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two space-separated integers nn (2n21052 \le n \le 2 \cdot 10^5) and kk (1k1091 \le k \le 10^9) — the size of the tree and the required number of paths.

The second line contains n1n - 1 space-separated integers p2,p3,,pnp_2,p_3,\ldots,p_n (1pin1\le p_i\le n), where pip_i is the parent of the ii-th vertex. It is guaranteed that this value describe a valid tree with root 11.

The third line contains nn space-separated integers s1,s2,,sns_1,s_2,\ldots,s_n (0si1040 \le s_i \le 10^4) — the scores of the vertices.

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

Output

For each test case, print a single integer — the maximum value of a path multiset.

Sample Input 1

2
5 4
1 2 1 3
6 2 1 5 7
5 3
1 2 1 3
6 6 1 4 10

Sample Output 1

54
56

Note

In the first test case, one of optimal solutions is four paths 12351 \to 2 \to 3 \to 5, 12351 \to 2 \to 3 \to 5, 141 \to 4, 141 \to 4, here c=[4,2,2,2,2]c=[4,2,2,2,2]. The value equals to 46+22+21+25+27=544\cdot 6+ 2\cdot 2+2\cdot 1+2\cdot 5+2\cdot 7=54.

In the second test case, one of optimal solution is three paths 12351 \to 2 \to 3 \to 5, 12351 \to 2 \to 3 \to 5, 141 \to 4, here c=[3,2,2,1,2]c=[3,2,2,1,2]. The value equals to 36+26+21+14+210=563\cdot 6+ 2\cdot 6+2\cdot 1+1\cdot 4+2\cdot 10=56.