Ninja Assignment

Time limit1sMemory limit256 MB

Summary
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.
Level

Hard8 of 10

Topics
Tree, DFS, Heap, Greedy
Solved
No attempts yet

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 ii (1≤i≤N1 \le i \le N), its boss BiB_i, salary CiC_i, and leadership level LiL_i, together with the salary budget MM, 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 NN and MM separated by a space, where NN is the number of ninjas and MM is the total budget.

Each of the next NN lines describes one ninja. Line i+1i+1 contains three integers BiB_i, CiC_i, and LiL_i separated by spaces: BiB_i is ninja ii's boss, CiC_i is ninja ii's salary, and LiL_i is ninja ii's leadership level. If Bi=0B_i = 0, ninja ii is the master. Because Bi<iB_i < i always holds, every ninja's boss has a smaller number than the ninja itself.

Output

Output the maximum possible client satisfaction.

Constraints

  • 1≤N≤100,0001 \le N \le 100{,}000
  • 1≤M≤1,000,000,0001 \le M \le 1{,}000{,}000{,}000
  • 0≤Bi<i0 \le B_i < i
  • 1≤Ci≤M1 \le C_i \le M
  • 1≤Li≤1,000,000,0001 \le L_i \le 1{,}000{,}000{,}000

Note

In the sample, choose ninja 11 as the manager and assign ninjas 33 and 44. Their salaries total 2+2=42 + 2 = 4, which does not exceed the budget of 44. Two ninjas are assigned and the manager's leadership level is 33, so the client's satisfaction is 2×3=62 \times 3 = 6, which is the maximum.

Examples1

  1. Example 1

    Input
    5 4
    0 3 3
    1 3 5
    2 2 2
    1 2 4
    2 3 1
    
    Expected output
    6