#P3780. [SDOI2017] 苹果树

    ID: 2723 远端评测题 5000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2017各省省选单调队列山东O2优化枚举,暴力背包

[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 nn nodes, numbered 11 to nn. Node 11 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 ii-th node has ai(ai>0)a_i (a_i > 0) apples, and taking one apple from this node gives vi(vi>0)v_i (v_i > 0) happiness (if you take kaik \leq a_i apples from this node, you gain kvikv_i 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 kk. Pick some apples from the tree. If in total you pick tt apples, and among all nodes from which at least one apple is picked, the maximum depth is hh (the root has depth 11), then you must satisfy thkt-h \leq k. Find the maximum total happiness you can obtain.

Input Format

There are multiple testcases. The first line contains an integer QQ, the number of testcases.

For each testcase, the first line contains two integers nn and kk.

Then nn lines follow, each describing one node. On the ii-th line, the first integer is the index of the parent of node ii (if i=1i = 1, its parent is 00), the second integer is aia_i, and the third integer is viv_i.

Output Format

Output QQ 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 10%10\% of the testdata, nk3000000nk \leq 3000000 and the height of the tree is 22.
  • For 20%20\% of the testdata, nk25000000nk \leq 25000000 and the height of the tree is 22.
  • For 20%20\% of the testdata, nk25000000nk \leq 25000000 and all aia_i are 11.
  • For another 20%20\% of the testdata, nk3000000nk \leq 3000000 with no additional restrictions.
  • Constraints for 100%100\% of the testdata: 1Q51 \leq Q \leq 5; 1n200001 \leq n \leq 20000; 1k5000001 \leq k \leq 500000; 1nk250000001 \leq nk \leq 25000000; 1ai1081 \leq a_i \leq 10^8; 1vi1001 \leq v_i \leq 100.

Translated by ChatGPT 5