Family Fortune

Time limit10sMemory limit128 MB

Summary
Choose K nodes in a rooted tree, no one an ancestor of another, maximizing the sum of weights; print 0 if impossible.
Level

Medium7 of 10

Topics
Dynamic programming, Tree, DFS
Solved
No attempts yet

Problem

While studying the history of wealthy families, researchers want to know how large a fortune each family has actually amassed. History records several "net worth" figures for each individual, but simply adding them up is inaccurate because of the double counting caused by inheritance. One way to estimate a family's wealth is to choose a set of KK people, none of whom is an ancestor or a descendant of any other in the set, and add up their net worth. The family's wealth is then defined as the maximum such sum over all valid sets of KK people.

Because the records contain only the net worth of the male family members, the family tree is a simple tree in which every male has exactly one father and any number (possibly zero) of sons. There is also exactly one person who is an ancestor of every other member.

Given the family tree, what is the family's wealth under this definition?

Input

The input contains several test cases. Each test case begins with two integers:

N K

where NN (1≤N≤100,000)(1 \le N \le 100{,}000) is the total number of recorded family members and KK (1≤K≤1,000)(1 \le K \le 1{,}000) is the size of the desired set.

Each of the next NN lines contains two integers:

P W

where PP (0≤P≤N)(0 \le P \le N) is the parent of that member. Members are numbered from 11 to NN, and the ii-th of these lines describes the parent and fortune of member ii. There is a single root, whose member has P=0P = 0. The tree is at most 1,0001{,}000 deep and, of course, contains no cycles. WW (1≤W≤1,000)(1 \le W \le 1{,}000) is that member's wealth (in millions).

The input ends with a line containing two zeros.

Output

For each test case, print on its own line a single integer: the maximum sum (in millions) of the fortunes of a set of KK family members in which no member is an ancestor or descendant of any other. If no such set of KK members exists, print 00. Do not print extra spaces, and do not separate answers with blank lines.

Examples4

  1. Example 1

    Input
    11 5
    0 1
    1 1
    1 1
    2 1
    2 1
    3 1
    3 1
    3 1
    5 1
    7 1
    7 1
    11 5
    11 3
    1 1
    4 1
    1 2
    10 2
    10 2
    6 2
    6 1
    10 2
    11 3
    0 4
    7 3
    0 18
    1 20
    1 15
    2 12
    2 6
    3 8
    3 8
    0 0
    
    Expected output
    5
    10
    36
    
  2. Example 2

    Input
    1 1
    0 42
    1 2
    0 42
    0 0
    
    Expected output
    42
    0
    
  3. Example 3

    Input
    4 1
    0 5
    1 10
    2 1
    3 7
    4 2
    0 5
    1 10
    2 1
    3 7
    0 0
    
    Expected output
    10
    0
    
  4. Example 4

    Input
    5 1
    0 100
    1 1
    1 2
    1 3
    1 4
    5 2
    0 100
    1 1
    1 2
    1 3
    1 4
    5 3
    0 100
    1 1
    1 2
    1 3
    1 4
    5 4
    0 100
    1 1
    1 2
    1 3
    1 4
    5 5
    0 100
    1 1
    1 2
    1 3
    1 4
    0 0
    
    Expected output
    100
    7
    9
    10
    0