Biochips
Time limit2sMemory limit512 MB
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 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 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 and , the number of elements of the tree and the number of biochips to pick (, ).
Each of the next lines contains two non-negative integers: the number of the parent in the tree, and the memory size of that chip (). Biochips are numbered from 1 to , and the -th of these lines describes the biochip numbered . Exactly one biochip has no parent, and its parent is given as 0.
Picking biochips is always possible.
Output
Print one integer, the largest possible total memory size of the chosen biochips.