F - Dungeon Conquest Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君はゲームの中でダンジョンを探索しています。

高橋君の体力には最大値 M が定められており、ダンジョンの入り口(初期状態)での体力は M、所持コイン数は 0 枚です。

ダンジョンには 1 から N までの番号が付けられた N 個の部屋と、1 から N - 1 までの番号が付けられた N - 1 個の休憩所があります。

高橋君は、部屋 1 から始めて以下の順番で交互に進み、部屋 N の攻略を目指します。

\text{部屋 } 1 \to \text{休憩所 } 1 \to \text{部屋 } 2 \to \text{休憩所 } 2 \to \dots \to \text{休憩所 } N-1 \to \text{部屋 } N

各部屋および各休憩所でのイベントは以下の通りです。

  • 部屋 i(1 \le i \le N)の攻略:

部屋 i を攻略すると、体力を A_i 消費します。攻略する直前の体力が A_i 未満である場合、その部屋を攻略できず探索は失敗となります(攻略直後の体力がちょうど 0 になることは許容されます)。

  • 休憩所 i(1 \le i \le N - 1)での行動:

高橋君は、以下の 2 つの行動のうちどちらかちょうど一方を選択して行わなければなりません。

  • 泉の水を飲む: 体力を B_i 回復する。ただし、回復後の体力が最大値 M を超える場合、体力は M になる。
  • 宝箱を開ける: コインを C_i 枚獲得する。体力は変化しない。

高橋君が適切に行動を選択して部屋 N まで攻略を完了させたとき、最終的に獲得できるコインの総数の最大値を求めてください。

なお、適切に行動を選択することで、部屋 N まで攻略を完了できる方法が少なくとも 1 つ存在することが保証されます。

制約

  • 2 \le N \le 3000
  • 1 \le M \le 3000
  • 1 \le A_i \le M(1 \le i \le N)
  • 1 \le B_i \le M(1 \le i \le N - 1)
  • 1 \le C_i \le 10^9(1 \le i \le N - 1)
  • 適切に行動を選択することで、部屋 N まで攻略を完了できる方法が少なくとも 1 つ存在する。
  • 入力はすべて整数である。

入力

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

N M
A_1 A_2 \dots A_N
B_1 C_1
B_2 C_2
\vdots
B_{N-1} C_{N-1}
  • 1 行目には、部屋の数を表す整数 N と、最大体力を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各部屋の攻略で消費する体力を表す N 個の整数 A_1, A_2, \dots, A_N が、スペース区切りで与えられる。
  • 続く N - 1 行のうち i 行目(1 \le i \le N - 1)には、休憩所 i における体力の回復量を表す整数 B_i と、獲得できるコインの枚数を表す整数 C_i が、スペース区切りで与えられる。

出力

高橋君が獲得できるコインの総数の最大値を 1 行で出力せよ。


入力例 1

4 10
4 5 4 3
5 10
6 30
4 15

出力例 1

30

入力例 2

3 10
2 2 2
1 50
1 70

出力例 2

120

入力例 3

8 15
7 8 4 10 5 6 9 3
8 50
6 120
10 30
5 200
7 80
9 150
4 90

出力例 3

210

入力例 4

20 50
25 30 15 40 20 35 10 25 30 20 45 15 30 25 35 20 10 40 30 15
20 1000000000
35 850000000
15 500000000
40 999999999
30 750000000
25 300000000
45 900000000
20 600000000
35 450000000
50 1000000000
15 200000000
30 800000000
40 650000000
25 400000000
35 950000000
20 350000000
45 880000000
30 550000000
50 1000000000

出力例 4

650000000

入力例 5

2 1
1 1
1 1

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi is exploring a dungeon in a game.

Takahashi has a maximum health of M. At the entrance of the dungeon (initial state), his health is M, and he has 0 coins.

The dungeon has N rooms numbered 1 to N, and N - 1 rest areas numbered 1 to N - 1.

Takahashi starts from Room 1 and alternates between rooms and rest areas in the following order, aiming to clear Room N:

\text{Room } 1 \to \text{Rest Area } 1 \to \text{Room } 2 \to \text{Rest Area } 2 \to \dots \to \text{Rest Area } N-1 \to \text{Room } N

The events at each room and rest area are as follows:

  • Clearing Room i (1 \le i \le N):

Clearing Room i consumes A_i health. If his health immediately before clearing the room is strictly less than A_i, he cannot clear the room and the exploration fails (having exactly 0 health immediately after clearing is allowed).

  • Action at Rest Area i (1 \le i \le N - 1):

Takahashi must choose and perform exactly one of the following two actions:

  • Drink water from the spring: Restores B_i health. However, if the health after recovery exceeds the maximum value M, it becomes M.
  • Open the treasure chest: Obtains C_i coins. Health does not change.

Find the maximum total number of coins Takahashi can ultimately obtain when he chooses his actions appropriately and successfully clears up to Room N.

It is guaranteed that there is at least one way to choose actions to clear up to Room N.

Constraints

  • 2 \le N \le 3000
  • 1 \le M \le 3000
  • 1 \le A_i \le M (1 \le i \le N)
  • 1 \le B_i \le M (1 \le i \le N - 1)
  • 1 \le C_i \le 10^9 (1 \le i \le N - 1)
  • There exists at least one way to choose actions to clear up to Room N.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N
B_1 C_1
B_2 C_2
\vdots
B_{N-1} C_{N-1}
  • The first line contains two space-separated integers N and M, representing the number of rooms and the maximum health, respectively.
  • The second line contains N space-separated integers A_1, A_2, \dots, A_N, representing the health consumed to clear each room.
  • The i-th of the following N - 1 lines (1 \le i \le N - 1) contains two space-separated integers B_i and C_i, representing the amount of health restored and the number of coins obtained at Rest Area i, respectively.

Output

Print the maximum total number of coins Takahashi can obtain in a single line.


Sample Input 1

4 10
4 5 4 3
5 10
6 30
4 15

Sample Output 1

30

Sample Input 2

3 10
2 2 2
1 50
1 70

Sample Output 2

120

Sample Input 3

8 15
7 8 4 10 5 6 9 3
8 50
6 120
10 30
5 200
7 80
9 150
4 90

Sample Output 3

210

Sample Input 4

20 50
25 30 15 40 20 35 10 25 30 20 45 15 30 25 35 20 10 40 30 15
20 1000000000
35 850000000
15 500000000
40 999999999
30 750000000
25 300000000
45 900000000
20 600000000
35 450000000
50 1000000000
15 200000000
30 800000000
40 650000000
25 400000000
35 950000000
20 350000000
45 880000000
30 550000000
50 1000000000

Sample Output 4

650000000

Sample Input 5

2 1
1 1
1 1

Sample Output 5

0