E - Jump Action Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

高橋君は横スクロールアクションゲームをプレイしています。

ゲームのステージには、一直線上に 1 から N までの番号が付けられた N 個の足場があり、足場 i の座標は X_i です。

高橋君は足場 1 から出発し、ジャンプを繰り返して足場 N へ移動しようとしています。高橋君が一度のジャンプで移動できる最大距離は K です。

現在いる足場 i から別の足場 j へ直接ジャンプして移動するには、以下の条件をすべて満たす必要があります。

  • i < j
  • X_j - X_i \leq K

高橋君は、スタート地点である足場 1、ゴール地点である足場 N、および途中で着地した足場 i ごとに、それぞれ得点 A_i を獲得します。

足場 1 から足場 N に到達するまでに獲得できる得点の合計の最大値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq X_1 < X_2 < \dots < X_N \leq 10^9
  • -10^9 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

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

N K
X_1 A_1
X_2 A_2
\vdots
X_N A_N

出力

高橋君が獲得できる得点の合計の最大値を出力してください。

ただし、足場 N に到達できない場合は、代わりに -1 を出力してください(到達可能で得点の合計の最大値が -1 である場合も -1 を出力します)。


入力例 1

5 3
1 2
3 5
4 -1
6 4
8 3

出力例 1

14

入力例 2

4 2
1 10
3 10
6 10
8 10

出力例 2

-1

入力例 3

8 5
2 10
4 -5
7 20
10 -10
12 15
16 5
17 -20
20 10

出力例 3

60

入力例 4

15 7
1 100
4 -50
7 80
11 -20
14 60
18 40
21 -30
25 90
29 -10
33 50
37 70
40 -40
44 30
48 60
52 100

出力例 4

670

入力例 5

2 1000000000
1 -1000000000
1000000000 -1000000000

出力例 5

-2000000000

Score : 466 pts

Problem Statement

Takahashi is playing a side-scrolling action game.

On the game stage, there are N platforms numbered 1 to N along a straight line, and the coordinate of platform i is X_i.

Starting from platform 1, Takahashi wants to reach platform N by repeatedly jumping. The maximum distance he can move in a single jump is K.

To jump directly from his current platform i to another platform j, all of the following conditions must be satisfied:

  • i < j
  • X_j - X_i \leq K

Takahashi earns a score of A_i for platform 1 (the starting point), platform N (the goal), and each platform i he lands on along the way.

Find the maximum possible total score he can earn by traveling from platform 1 to platform N.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq X_1 < X_2 < \dots < X_N \leq 10^9
  • -10^9 \leq A_i \leq 10^9
  • All input values are integers.

Input

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

N K
X_1 A_1
X_2 A_2
\vdots
X_N A_N

Output

Print the maximum total score Takahashi can earn.

However, if it is impossible to reach platform N, print -1 instead (if it is reachable and the maximum total score is -1, print -1 as well).


Sample Input 1

5 3
1 2
3 5
4 -1
6 4
8 3

Sample Output 1

14

Sample Input 2

4 2
1 10
3 10
6 10
8 10

Sample Output 2

-1

Sample Input 3

8 5
2 10
4 -5
7 20
10 -10
12 15
16 5
17 -20
20 10

Sample Output 3

60

Sample Input 4

15 7
1 100
4 -50
7 80
11 -20
14 60
18 40
21 -30
25 90
29 -10
33 50
37 70
40 -40
44 30
48 60
52 100

Sample Output 4

670

Sample Input 5

2 1000000000
1 -1000000000
1000000000 -1000000000

Sample Output 5

-2000000000