This page is still under construction.

Parts of this page are still being built. What you see may change.

Parents

Time limit5sMemory limit128 MB

Summary
Pick from 1 to K nodes of a valued tree with no parent-child pair so the sum of picked values is as large as possible.
Level

Medium7 of 10

Topics
Dynamic programming, Tree
Solved
No attempts yet

Problem

You are given a rooted tree whose nodes each carry an integer value. The value of a set of nodes is the sum of the values of the nodes it contains.

For a positive integer KK, a KK-anti-parental subset is a set of nodes that satisfies both of the following:

  1. it contains between 11 and KK nodes, inclusive;
  2. no node in the set is the parent of another node in the set (equivalently, the set contains no parent-child pair).

Given a node-valued tree and KK, compute the maximum possible value of a KK-anti-parental subset.

Input

The first line contains the number of test cases TT. Each test case is given as follows.

The first line of a test case contains two integers NN and KK: the number of nodes and the parameter KK, with N≤100000N \le 100000 and 1≤K≤1001 \le K \le 100. Nodes are indexed from 00 to N−1N-1, and node 00 is always the root.

The second line contains NN integers, the values of nodes 00 through N−1N-1 in order, each in the range [−1000,1000][-1000, 1000].

The third line contains N−1N-1 integers: the parent index of each node from 11 to N−1N-1, in increasing order of index. The first of these integers is the parent of node 11. The root (node 00) has no parent entry. When N=1N = 1 this line is empty.

Output

For each test case, print a single line containing the maximum possible value of a KK-anti-parental subset of that tree.

Examples1

  1. Example 1

    Input
    3
    7 3
    6 2 -5 5 -3 4 1
    0 0 2 2 0 5
    3 2
    4 -3 5
    0 0
    7 6
    -1 -1 -1 -1 -1 -1 -1
    0 1 2 3 4 5
    
    Expected output
    12
    5
    -1