QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 128 MB Puntuación total: 100

#10654. Knapsack

Estadísticas

Jaś and Małgosia are going on a trip. The boy, wanting to impress his friend, decided to pack their belongings into a single backpack. Moreover, the heavier the backpack, the better the impression he will make. Of course, Jaś cannot pack everything he finds at home without thinking—the backpack might tear (what a shame that would be!).

Furthermore, Jaś cannot allow a situation where, for example, he takes a radio but leaves the batteries behind, or takes a tripod but forgets the camera. For each item $i$, Jaś has either specified another item $j$ without which item $i$ would be useless, or simply noted that $i$ is an item that is useful on its own.

Help our hero calculate the maximum weight of the backpack's contents he can achieve without exceeding its maximum capacity and without taking any useless items.

Input

The first line of input contains two integers $n$ and $p$ ($1 \le n \le 200$, $1 \le p \le 10^{6}$), representing the number of items Jaś is considering taking and the backpack's capacity in kilograms—if we pack items with a total mass exceeding $p$, the backpack will tear.

Assume that the items are numbered from 1 to $n$. The next $n$ lines contain descriptions of the items—the description of item $i$ consists of two integers $j_{i}$ and $m_{i}$ ($0 \le j_{i} < i$, $1 \le m_{i} \le p$), representing the number of the item that must be in the backpack for item $i$ to be packed (if $j_{i} = 0$, then item $i$ can be packed unconditionally), and the mass of item $i$ in kilograms.

Output

Your program should output a single integer: the maximum mass of the backpack's contents (in kilograms) that Jaś can achieve.

Examples

Input 1

7 11
0 3
0 1
2 3
2 2
4 4
5 3
5 2

Output 1

10

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.