#P1552. [APIO2012] 派遣

    ID: 540 远端评测题 500ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2012平衡树APIO左偏树

[APIO2012] 派遣

Description

In this gang, there is one ninja called the Master. Every ninja except the Master has exactly one superior. For secrecy and to strengthen leadership, all work-related commands are always sent from a superior to their direct subordinate, and cannot be sent in any other way.

You are now to recruit a group of ninjas and dispatch them to customers. You must pay a salary for each dispatched ninja, and the total salary must not exceed your budget. In addition, to send commands, you must choose one ninja as the manager, and this manager must be able to send commands to all dispatched ninjas. When sending commands, any ninja (dispatched or not) may act as a relay. The manager themself may or may not be dispatched. If the manager is not dispatched, you do not need to pay the manager’s salary.

Your goal is to maximize customer satisfaction within the budget. Customer satisfaction is defined as the number of dispatched ninjas multiplied by the manager’s leadership level, where each ninja has a fixed leadership level.

Write a program that, given each ninja ii’s superior BiB_i, salary CiC_i, leadership level LiL_i, and the total budget MM, outputs the maximum possible customer satisfaction under the above requirements.

Input Format

The first line contains two integers NN and MM, where NN is the number of ninjas and MM is the total salary budget.

The next NN lines describe each ninja’s superior, salary, and leadership level. The ii-th line contains three integers Bi,Ci,LiB_i, C_i, L_i, denoting ninja ii’s superior, salary, and leadership level, respectively. The Master has Bi=0B_i = 0, and every ninja’s superior index is strictly less than their own, i.e., Bi<iB_i \lt i.

Output Format

Output a single integer, the maximum customer satisfaction within the budget.

5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1

6

Hint

1N1051 \le N \le 10^51M1091 \le M \le 10^90Bi<i0 \le B_i \lt i1CiM1 \le C_i \le M1Li1091 \le L_i \le 10^9

For 30%30\% of the testdata, N3000N \le 3000.

Translated by ChatGPT 5