This page is still under construction.

Parts of this page are still being built. What you see may change.

Festive Baobab

Time limit2sMemory limit512 MB

Summary
Place t unit-weight decorations on nodes of a rooted tree; each node i allows total weight in its subtree up to w_i, and each decoration on node i gives d_i joy. Maximize total joy.
Level

Hard8 of 10

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

Problem

Do you know where the baobab is in the Math&Mech building? If not, ask someone after the contest to show you...

Everyone knows the baobab is a tree. Like any tree, it has a trunk and branches. Branches grow from the trunk or from other branches.

On September 1, the baobab is usually decorated to make new students feel comfortable. For this purpose, a large chest with small colorful decorations sits deep in the building's dungeons. Every decoration weighs exactly one gram.

Unfortunately, the baobab is very old, so it breaks if overloaded with decorations. For each branch, you know the maximum weight of decorations it can carry. If a branch, together with all branches growing from it (directly or through other branches), carries decorations whose total weight exceeds this limit, the branch breaks. This is unacceptable. But you can put several decorations on a single branch, as long as it does not cause any branch to break.

Some branches are near the entrance and clearly visible, while others are hidden in the depths of the baobab. So each decoration brings more or less joy depending on its position. The total joy of the baobab is the sum of the joys of the branches where decorations are located. If several decorations are on a single branch, that branch's joy is multiplied by their quantity.

What is the maximum possible total joy the baobab can have?

Input

The first line contains two integers nn and tt: the number of branches on the baobab and the number of decorations (1≤n≤100 0001 \le n \le 100\,000, 1≤t≤1091 \le t \le 10^9). Each of the next nn lines contains three integers did_i, pip_i, and wiw_i: the joy a single decoration on the ii-th branch gives, the branch that the ii-th branch grows from (or 00 if the ii-th branch grows directly from the trunk), and the maximum weight of decorations the ii-th branch can carry (1≤di,wi≤1091 \le d_i, w_i \le 10^9, 0≤pi≤n0 \le p_i \le n).

Every branch grows from the trunk, directly or through other branches. It is also guaranteed that it is possible to use all decorations.

Output

Output one integer: the maximum total joy the decorated baobab gives.

Examples1

  1. Example 1

    Input
    9 6
    30 0 4
    40 9 2
    80 8 3
    20 9 2
    10 4 3
    70 5 8
    90 2 4
    50 0 6
    60 1 3
    
    Expected output
    490