E - Exhibition Order Jumping Up and Down Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

N 個の展示品が番号 1,2,\ldots,N の順に並んでいます。展示品 i の高さは A_i、得点は B_i です。得点は負の場合もあります。

いくつかの展示品を選び、番号の小さい順に展示します。選んだ番号を p_1<p_2<\cdots<p_k としたとき、次の条件を満たす必要があります。

  • すべての 1\leq j<k について、L\leq |A_{p_{j+1}}-A_{p_j}|\leq R である。
  • 隣り合う展示品の高さが上がるか下がるかは、交互でなければならない。すなわち、すべての 1\leq j<k-1 について、A_{p_{j+1}}-A_{p_j} と A_{p_{j+2}}-A_{p_{j+1}} は異なる符号を持つ。

最初は上がっても下がってもかまいません。1 個だけ選ぶ場合は両方の条件を満たします。また、何も選ばないこともでき、その場合の得点の総和は 0 です。

条件を満たす選び方における、選んだ展示品の得点の総和の最大値を求めてください。

制約

  • 1\leq N\leq 200000
  • 1\leq L\leq R\leq 10^9
  • 1\leq A_i\leq 10^9 (1\leq i\leq N)
  • -10^9\leq B_i\leq 10^9 (1\leq i\leq N)
  • 入力はすべて整数である。

入力

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

N L R
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • 1 行目には展示品の個数 N と、高さの差の下限 L、上限 R が与えられる。
  • 続く N 行のうち i 行目には、展示品 i の高さ A_i と得点 B_i が与えられる。

出力

選んだ展示品の得点の総和の最大値を整数で出力せよ。


入力例 1

3 1 3
1 5
3 4
2 6

出力例 1

15

入力例 2

3 1 2
1 -2
2 -3
3 -4

出力例 2

0

入力例 3

5 2 4
1 4
3 6
5 7
2 3
4 8

出力例 3

24

入力例 4

7 1 2
1 1
2 2
3 3
2 4
1 5
3 6
2 7

出力例 4

23

入力例 5

1 1000000000 1000000000
1000000000 1000000000

出力例 5

1000000000

Score : 466 pts

Problem Statement

There are N exhibits lined up in the order of numbers 1, 2, \ldots, N. Exhibit i has a height of A_i and a score of B_i. Scores may be negative.

You choose some exhibits and display them in ascending order of their indices. If the chosen indices are p_1 < p_2 < \cdots < p_k, the following conditions must be satisfied:

  • For all 1 \leq j < k, L \leq |A_{p_{j+1}} - A_{p_j}| \leq R.
  • Whether the height of adjacent exhibits increases or decreases must alternate. That is, for all 1 \leq j < k - 1, A_{p_{j+1}} - A_{p_j} and A_{p_{j+2}} - A_{p_{j+1}} have different signs.

The height may either increase or decrease at the start. If only 1 exhibit is chosen, both conditions are satisfied. You may also choose to display no exhibits at all, in which case the total score is 0.

Find the maximum possible sum of scores of the chosen exhibits in a valid selection.

Constraints

  • 1\leq N\leq 200000
  • 1\leq L\leq R\leq 10^9
  • 1\leq A_i\leq 10^9 (1\leq i\leq N)
  • -10^9\leq B_i\leq 10^9 (1\leq i\leq N)
  • All input values are integers.

Input

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

N L R
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • The first line contains the number of exhibits N, the lower bound on the height difference L, and the upper bound R.
  • Of the following N lines, the i-th line contains the height A_i and the score B_i of exhibit i.

Output

Print the maximum possible sum of scores of the chosen exhibits as an integer.


Sample Input 1

3 1 3
1 5
3 4
2 6

Sample Output 1

15

Sample Input 2

3 1 2
1 -2
2 -3
3 -4

Sample Output 2

0

Sample Input 3

5 2 4
1 4
3 6
5 7
2 3
4 8

Sample Output 3

24

Sample Input 4

7 1 2
1 1
2 2
3 3
2 4
1 5
3 6
2 7

Sample Output 4

23

Sample Input 5

1 1000000000 1000000000
1000000000 1000000000

Sample Output 5

1000000000