#P2015. 二叉苹果树
二叉苹果树
Description
There is an apple tree. If a branch splits, it always splits into two (that is, there is no node with exactly one child).
The tree has nodes (leaves or branching points), numbered to , and the root is always node .
We describe a branch by the pair of node indices it connects. Below is a tree with branches:
2 5
\ /
3 4
\ /
1
Now the tree has too many branches and needs pruning. However, some branches have apples on them.
Given the number of branches to keep, find the maximum number of apples that can be retained.
Input Format
The first line contains integers and , representing the number of nodes in the tree and the number of branches to keep, respectively.
Each of the next lines contains integers describing one branch: the first numbers are the indices of the nodes it connects, and the third number is the number of apples on that branch.
Output Format
Output a single integer, the maximum number of apples that can be retained.
5 2
1 3 1
1 4 10
2 3 20
3 5 20
21
Hint
, and the number of apples on each branch .
Translated by ChatGPT 5
京公网安备 11011102002149号