#P1746D. Paths on the Tree
Paths on the Tree
Description
You are given a rooted tree consisting of vertices. The vertices are numbered from to , and the root is the vertex . You are also given a score array .
A multiset of simple paths is called valid if the following two conditions are both true.
- Each path starts from .
- Let be the number of paths covering vertex . For each pair of vertices () that have the same parent, holds.
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 () — the number of test cases. The description of the test cases follows.
The first line of each test case contains two space-separated integers () and () — the size of the tree and the required number of paths.
The second line contains space-separated integers (), where is the parent of the -th vertex. It is guaranteed that this value describe a valid tree with root .
The third line contains space-separated integers () — the scores of the vertices.
It is guaranteed that the sum of over all test cases does not exceed .
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 () — the number of test cases. The description of the test cases follows.
The first line of each test case contains two space-separated integers () and () — the size of the tree and the required number of paths.
The second line contains space-separated integers (), where is the parent of the -th vertex. It is guaranteed that this value describe a valid tree with root .
The third line contains space-separated integers () — the scores of the vertices.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print a single integer — the maximum value of a path multiset.
Note
In the first test case, one of optimal solutions is four paths , , , , here . The value equals to .
In the second test case, one of optimal solution is three paths , , , here . The value equals to .