#P3780. [SDOI2017] 苹果树
[SDOI2017] 苹果树
Description
Summer is coming, and it is the season of love. The apple tree in front of Little Q’s house is full of bright red round apples.
This apple tree is a rooted tree with nodes, numbered to . Node is the root. For every other node, its parent is guaranteed to be a node with a smaller index. Each node has some apples. The -th node has apples, and taking one apple from this node gives happiness (if you take apples from this node, you gain happiness). If you take at least one apple from a node, then you must also take at least one apple from its parent.
You are given a positive integer . Pick some apples from the tree. If in total you pick apples, and among all nodes from which at least one apple is picked, the maximum depth is (the root has depth ), then you must satisfy . Find the maximum total happiness you can obtain.
Input Format
There are multiple testcases. The first line contains an integer , the number of testcases.
For each testcase, the first line contains two integers and .
Then lines follow, each describing one node. On the -th line, the first integer is the index of the parent of node (if , its parent is ), the second integer is , and the third integer is .
Output Format
Output lines, one per testcase.
For each testcase, output a single integer, the maximum total happiness that can be obtained.
2
5 1
0 1 1
1 1 1
1 1 3
2 1 10
3 1 4
9 15
0 1 1
1 7 2
2 5 10
1 3 1
4 3 17
4 3 18
4 4 19
1 1 1
8 1 100
15
316
Hint
- For of the testdata, and the height of the tree is .
- For of the testdata, and the height of the tree is .
- For of the testdata, and all are .
- For another of the testdata, with no additional restrictions.
- Constraints for of the testdata: ; ; ; ; ; .
Translated by ChatGPT 5
京公网安备 11011102002149号