F - Sweet Alchemy 解説 /

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 900

問題文

パティシエの赤木さんは、「お菓子の素」という粉のみを材料として N 種類のドーナツを作ることができます。これらのドーナツはドーナツ 1、ドーナツ 2...、ドーナツ N と呼ばれます。ドーナツ i (1 ≤ i ≤ N)1 個作るには、お菓子の素 m_i グラムを消費する必要があります。なお、0.5 個など整数でない個数のドーナツを作ることはできません。

これらのドーナツのレシピは、ドーナツ 1 のレシピから改変を繰り返して開発されたものです。 具体的には、ドーナツ i (2 ≤ i ≤ N) のレシピはドーナツ p_i (1 ≤ p_i < i) のレシピを直接改変したものです。

いま、赤木さんはお菓子の素を X グラム持っています。これを使って、今夜のパーティーに向けて可能な限りたくさんのドーナツを作ることにしました。ただし、来客の好みは様々なので、次の条件を守ることにしました。

  • ドーナツ i (1 ≤ i ≤ N) を作る個数を c_i とする。2 ≤ i ≤ N であるようなどの整数 i に対しても、c_{p_i} ≤ c_i ≤ c_{p_i} + D となるように作る。ここで、D はあらかじめ決まった値である。

このとき、最大で何個のドーナツを作ることができるでしょうか?お菓子の素を使い切る必要はありません。

制約

  • 2 ≤ N ≤ 50
  • 1 ≤ X ≤ 10^9
  • 0 ≤ D ≤ 10^9
  • 1 ≤ m_i ≤ 10^9 (1 ≤ i ≤ N)
  • 1 ≤ p_i < i (2 ≤ i ≤ N)
  • 入力中の値はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N X D
m_1
m_2 p_2
:
m_N p_N

出力

条件を守って作ることのできるドーナツの最大の個数を出力せよ。


入力例 1

3 100 1
15
10 1
20 1

出力例 1

7

100 グラムのお菓子の素があり、赤木さんは 3 種類のドーナツを作ることができ、守るべき条件は c_1 ≤ c_2 ≤ c_1 + 1c_1 ≤ c_3 ≤ c_1 + 1 です。ドーナツ 12 個、ドーナツ 23 個、ドーナツ 32 個作るのが最適です。


入力例 2

3 100 10
15
10 1
20 1

出力例 2

10

入力例 1 からお菓子の素の量やドーナツのレシピそのものは変わっていませんが、最後の条件が緩くなっています。この場合、ドーナツ 2 だけを 10 個作るのが最適です。このように、必ずしもすべての種類のドーナツを作らなくても構いません。


入力例 3

5 1000000000 1000000
123
159 1
111 1
135 3
147 3

出力例 3

7496296

Score : 900 points

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

The recipes of these doughnuts are developed by repeated modifications from the recipe of Doughnut 1. Specifically, the recipe of Doughnut i (2 ≤ i ≤ N) is a direct modification of the recipe of Doughnut p_i (1 ≤ p_i < i).

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

  • Let c_i be the number of Doughnut i (1 ≤ i ≤ N) that she makes. For each integer i such that 2 ≤ i ≤ N, c_{p_i} ≤ c_i ≤ c_{p_i} + D must hold. Here, D is a predetermined value.

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto.

Constraints

  • 2 ≤ N ≤ 50
  • 1 ≤ X ≤ 10^9
  • 0 ≤ D ≤ 10^9
  • 1 ≤ m_i ≤ 10^9 (1 ≤ i ≤ N)
  • 1 ≤ p_i < i (2 ≤ i ≤ N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X D
m_1
m_2 p_2
:
m_N p_N

Output

Print the maximum number of doughnuts that can be made under the condition.


Sample Input 1

3 100 1
15
10 1
20 1

Sample Output 1

7

She has 100 grams of Moto, can make three kinds of doughnuts, and the conditions that must hold are c_1 ≤ c_2 ≤ c_1 + 1 and c_1 ≤ c_3 ≤ c_1 + 1. It is optimal to make two Doughnuts 1, three Doughnuts 2 and two Doughnuts 3.


Sample Input 2

3 100 10
15
10 1
20 1

Sample Output 2

10

The amount of Moto and the recipes of the doughnuts are not changed from Sample Input 1, but the last conditions are relaxed. In this case, it is optimal to make just ten Doughnuts 2. As seen here, she does not necessarily need to make all kinds of doughnuts.


Sample Input 3

5 1000000000 1000000
123
159 1
111 1
135 3
147 3

Sample Output 3

7496296