Under construction

Still being built. Things may change

Backpack

Interview

Time limit1sMemory limit128 MB

Summary
Choose a set of items whose total mass is at most p, where each chosen item requires its named lower-indexed prerequisite to be chosen too.
Level

Medium6 of 10

Topics
Dynamic programming, Tree, Backtracking
Solved
No attempts yet

Problem

Hansel and Gretel are setting off on a trip. Hansel, wanting to impress Gretel, decided to pack all of their belongings into a single backpack. What is more, the heavier the backpack is, the better the impression it will make. Of course, Hansel cannot simply cram in everything he finds at home without thinking, because the backpack could tear apart, which would be quite the embarrassment.

On top of that, Hansel must not end up in a situation where, say, he takes the radio but leaves the batteries behind, or takes the tripod but forgets the camera. For each item ii, Hansel has either named another item jj without which item ii would be useless, or simply marked that item ii is useful on its own.

Help our hero and compute the heaviest backpack contents he can put together, without exceeding the backpack's maximum load and without taking any useless item.

Input

The first line contains two integers nn and pp (1≤n≤2001 \le n \le 200, 1≤p≤1061 \le p \le 10^6): the number of items Hansel is considering taking, and the load capacity of the backpack in kilograms. If the packed items have a total mass exceeding pp, the backpack tears.

Assume the items are numbered from 11 to nn. Each of the next nn lines describes one item. The description of item ii consists of two integers jij_i and mim_i (0≤ji<i0 \le j_i < i, 1≤mi≤p1 \le m_i \le p): jij_i is the number of the item that must be in the backpack in order to pack item ii (if ji=0j_i = 0, then item ii may be packed unconditionally), and mim_i is the mass of item ii in kilograms.

Output

Print a single integer: the maximum mass of the backpack contents (in kilograms) that Hansel can achieve.

Examples3

  1. Example 1

    Input
    7 11
    0 3
    0 1
    2 3
    2 2
    4 4
    5 3
    5 2
    
    Expected output
    10
    
  2. Example 2

    Input
    1 5
    0 3
    
    Expected output
    3
    
  3. Example 3

    Input
    4 12
    0 3
    0 3
    0 3
    0 3
    
    Expected output
    12