Under construction

Still being built. Things may change

Biochips

Time limit2sMemory limit512 MB

Summary
Pick exactly M nodes from a rooted tree with given values so no picked node is an ancestor of another and the sum is maximal.
Level

Medium7 of 10

Topics
Dynamic programming, Tree
Solved
No attempts yet

Problem

Scientists found a biochip that splits itself into several new biochips. When a chip splits, the parent chip disappears. Every biochip has its own memory size, and that size does not depend on the memory size of its parent. A chip produced by a split is either used, which stops its division, or it keeps splitting in the same way.

The scientists wrote the whole division process as a tree with NN elements, and they know the structure of the tree together with the memory size of every biochip in it.

Write a program that picks exactly MM biochips from the tree so that the total memory size is as large as possible. Once you pick a biochip, you may pick none of its ancestors and none of its descendants.

Input

The first line contains two integers NN and MM, the number of elements of the tree and the number of biochips to pick (1≤N≤2000001 \le N \le 200000, 1≤M≤5001 \le M \le 500).

Each of the next NN lines contains two non-negative integers: the number of the parent in the tree, and the memory size xx of that chip (0≤x≤10000 \le x \le 1000). Biochips are numbered from 1 to NN, and the ii-th of these NN lines describes the biochip numbered ii. Exactly one biochip has no parent, and its parent is given as 0.

Picking MM biochips is always possible.

Output

Print one integer, the largest possible total memory size of the MM chosen biochips.

Examples2

  1. Example 1

    Input
    7 3
    2 100
    0 1000
    2 150
    3 100
    3 5
    5 100
    2 50
    
    Expected output
    300
    
  2. Example 2

    Input
    5 2
    0 0
    1 100
    2 99
    2 98
    1 50
    
    Expected output
    197