Ninja Assignment
Time limit1sMemory limit256 MB
Choose a manager and up to budget many ninjas from the manager's subtree, possibly passing through unassigned ninjas, to maximize assigned count times the manager's leadership.
Problem
A ninja organization assigns ninjas to a client and is paid for the work its ninjas do for that client.
The organization has a single ninja called the master. Every ninja other than the master serves exactly one boss. To keep the ninjas' secrets and preserve the chain of command, only a boss may give orders to their own subordinates; nobody else may relay an order.
You want to gather a group of ninjas from the organization and assign them to one client. You pay each assigned ninja a fixed monthly salary, and the total salary of all assigned ninjas must not exceed a given budget. To relay orders, you also designate one ninja as the manager, and that manager must be able to pass an order down to every assigned ninja. Because orders travel down the chain of command, every assigned ninja must be the manager itself or one of the manager's direct or indirect subordinates; orders may pass through ninjas who are not assigned. The manager itself may or may not be assigned; a ninja that is not assigned earns no salary.
You want to maximize the client's satisfaction within the budget. The satisfaction equals the total number of assigned ninjas multiplied by the manager's leadership level. Each ninja's leadership level is fixed.
Given, for each ninja (), its boss , salary , and leadership level , together with the salary budget , output the maximum possible client satisfaction over all valid choices of a manager and a set of assigned ninjas.
Input
The first line contains two positive integers and separated by a space, where is the number of ninjas and is the total budget.
Each of the next lines describes one ninja. Line contains three integers , , and separated by spaces: is ninja 's boss, is ninja 's salary, and is ninja 's leadership level. If , ninja is the master. Because always holds, every ninja's boss has a smaller number than the ninja itself.
Output
Output the maximum possible client satisfaction.
Constraints
Note
In the sample, choose ninja as the manager and assign ninjas and . Their salaries total , which does not exceed the budget of . Two ninjas are assigned and the manager's leadership level is , so the client's satisfaction is , which is the maximum.