A - Refilling Water

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は、 N 個の植木鉢に毎朝水やりをしています。

各植木鉢 i ( 1 \leq i \leq N )には、ちょうど R_i リットルの水が必要です。高橋君は今朝、じょうろに S リットルの水を汲みました。

すべての植木鉢に必要な水の合計は R_1 + R_2 + \cdots + R_N リットルです。高橋君は、すべての植木鉢にちょうど必要な量の水を与えたいと考えています。まずじょうろの水を使いますが、必要な水の合計に対して不足する場合は、不足分を近所の水道から購入して補います。水道からは 1 リットルあたり C 円で購入できます。じょうろの水だけで足りる場合は、水道から購入する必要はありません(じょうろに水が余っても問題ありません)。

水道から購入する水の代金(円)を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 0 \leq S \leq 10^{12}
  • 1 \leq C \leq 10^4
  • 1 \leq R_i \leq 10^4 ( 1 \leq i \leq N )
  • 入力はすべて整数である。

入力

N S C
R_1 R_2 \ldots R_N
  • 1 行目には、植木鉢の数を表す整数 N 、じょうろに汲んだ水の量(リットル)を表す整数 S 、水道からの購入単価(円/リットル)を表す整数 C が、スペース区切りで与えられる。
  • 2 行目には、各植木鉢 i に必要な水の量(リットル)を表す整数 R_1, R_2, \ldots, R_N が、スペース区切りで与えられる。

出力

水道から購入する水の代金(円)を整数で 1 行に出力せよ。じょうろの水だけですべての植木鉢の水やりをまかなえる場合は 0 を出力せよ。


入力例 1

3 5 2
3 2 4

出力例 1

8

入力例 2

3 10 3
2 3 1

出力例 2

0

入力例 3

10 20 100
5 8 3 7 6 4 9 2 1 10

出力例 3

3500

入力例 4

20 50 500
100 200 300 400 500 600 700 800 900 1000 150 250 350 450 550 650 750 850 950 1050

出力例 4

5725000

入力例 5

1 0 1
1

出力例 5

1

Score : 233 pts

Problem Statement

Takahashi waters N flower pots every morning.

Each flower pot i (1 \leq i \leq N) requires exactly R_i liters of water. This morning, Takahashi filled his watering can with S liters of water.

The total amount of water needed for all flower pots is R_1 + R_2 + \cdots + R_N liters. Takahashi wants to give each flower pot exactly the required amount of water. He will use the water from his watering can first, but if the total water needed exceeds what is in the watering can, he will purchase the shortage from a nearby water supply. Water can be purchased from the supply at C yen per liter. If the watering can alone has enough water, there is no need to purchase from the water supply (it is fine if water remains in the watering can).

Determine the cost (in yen) of water purchased from the water supply.

Constraints

  • 1 \leq N \leq 10^6
  • 0 \leq S \leq 10^{12}
  • 1 \leq C \leq 10^4
  • 1 \leq R_i \leq 10^4 (1 \leq i \leq N)
  • All input values are integers.

Input

N S C
R_1 R_2 \ldots R_N
  • The first line contains the integer N representing the number of flower pots, the integer S representing the amount of water (in liters) filled in the watering can, and the integer C representing the unit price (yen per liter) of water from the supply, separated by spaces.
  • The second line contains the integers R_1, R_2, \ldots, R_N representing the amount of water (in liters) required for each flower pot i, separated by spaces.

Output

Output the cost (in yen) of water purchased from the water supply as an integer on a single line. If the watering can alone is sufficient to water all the flower pots, output 0.


Sample Input 1

3 5 2
3 2 4

Sample Output 1

8

Sample Input 2

3 10 3
2 3 1

Sample Output 2

0

Sample Input 3

10 20 100
5 8 3 7 6 4 9 2 1 10

Sample Output 3

3500

Sample Input 4

20 50 500
100 200 300 400 500 600 700 800 900 1000 150 250 350 450 550 650 750 850 950 1050

Sample Output 4

5725000

Sample Input 5

1 0 1
1

Sample Output 5

1
B - Maximum Danger Interval

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は、ある直線道路沿いに設置された N 個の計測地点の騒音レベルを調査しています。計測地点には道路に沿って 1 から N までの番号が付けられており、計測地点 i (1 \leq i \leq N) の騒音レベルは T_i です。

計測地点 i は、その騒音レベル T_i が L \leq T_i \leq R を満たすとき「注意地点」と呼ばれます。注意地点は、住民の健康被害が懸念される騒音レベルにあたります。T_i < L または T_i > R である計測地点は注意地点ではありません。

高橋君は、1 \leq l \leq r \leq N を満たす整数の組 (l, r) を一つ選び、計測地点 l, l+1, \ldots, r からなる区間を考えます。この区間に含まれる計測地点のうち、注意地点の個数を A、注意地点でない地点の個数を B とします。このとき、A - B をこの区間の「危険度」と定義します。なお、A + B = r - l + 1(区間内の計測地点の総数)が成り立ちます。

1 \leq l \leq r \leq N を満たすすべての整数の組 (l, r) に対する危険度の最大値を求めてください。なお、注意地点が一つも存在しない場合など、危険度の最大値が負になることもあります。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq R \leq 10^9
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N L R
T_1 T_2 \ldots T_N
  • 1 行目には、計測地点の数を表す整数 N、注意地点を判定する騒音レベルの下限(以上)を表す整数 L、上限(以下)を表す整数 R が、スペース区切りで与えられる。
  • 2 行目には、各計測地点の騒音レベルを表す整数 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。

出力

1 \leq l \leq r \leq N を満たすすべての整数の組 (l, r) における危険度の最大値を 1 行で出力してください。


入力例 1

5 4 6
3 5 7 5 3

出力例 1

1

入力例 2

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

出力例 2

4

入力例 3

20 10 50
5 15 20 60 30 25 10 70 40 35 8 12 45 3 50 10 55 20 30 100

出力例 3

8

Score : 333 pts

Problem Statement

Takahashi is surveying the noise levels at N measurement points set up along a certain straight road. The measurement points are numbered from 1 to N along the road, and the noise level at measurement point i (1 \leq i \leq N) is T_i.

A measurement point i is called a "caution point" if its noise level T_i satisfies L \leq T_i \leq R. Caution points correspond to noise levels that may cause health concerns for residents. A measurement point where T_i < L or T_i > R is not a caution point.

Takahashi chooses a pair of integers (l, r) satisfying 1 \leq l \leq r \leq N and considers the interval consisting of measurement points l, l+1, \ldots, r. Among the measurement points in this interval, let A be the number of caution points and B be the number of non-caution points. The "danger level" of this interval is defined as A - B. Note that A + B = r - l + 1 (the total number of measurement points in the interval).

Find the maximum danger level over all pairs of integers (l, r) satisfying 1 \leq l \leq r \leq N. Note that the maximum danger level may be negative, such as when no caution points exist at all.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq R \leq 10^9
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers

Input

N L R
T_1 T_2 \ldots T_N
  • The first line contains the integer N representing the number of measurement points, the integer L representing the lower bound (inclusive) of the noise level for determining caution points, and the integer R representing the upper bound (inclusive), separated by spaces.
  • The second line contains the integers T_1, T_2, \ldots, T_N representing the noise levels at each measurement point, separated by spaces.

Output

Print the maximum danger level over all pairs of integers (l, r) satisfying 1 \leq l \leq r \leq N on a single line.


Sample Input 1

5 4 6
3 5 7 5 3

Sample Output 1

1

Sample Input 2

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

Sample Output 2

4

Sample Input 3

20 10 50
5 15 20 60 30 25 10 70 40 35 8 12 45 3 50 10 55 20 30 100

Sample Output 3

8
C - A Boat Going Down the River

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君はまっすぐな川でボートに乗っています。川は東西方向に流れており、東を正の方向とする数直線上で高橋君の位置を表します。

時刻 0 において、高橋君は座標 0 の位置にいます。

川の流れは以下のように変化します。流れが変化する時刻 T_1, T_2, \ldots, T_N(T_1 < T_2 < \cdots < T_N)と、それぞれの変化後の川の流れの速度 W_1, W_2, \ldots, W_N が与えられます。W_i > 0 は東向き、W_i < 0 は西向きの流れを意味します。すべての i について W_i \neq 0 が保証されます。

各時間帯における川の流れの速度は次の通りです:

  • 時刻 0 以上 T_1 未満の間:川の流れはありません(速度 0)。
  • i = 1, 2, \ldots, N-1 について、時刻 T_i 以上 T_{i+1} 未満の間:川の流れの速度は W_i です。
  • 時刻 T_N 以降:川の流れの速度は W_N です。

高橋君のボートの漕ぎ方には次のルールがあります:

  • 川の流れがないとき、高橋君はボートを漕がず、その場に留まります。
  • 川の流れがあるとき、高橋君は常に流れと同じ方向に、水面に対して速さ V でボートを漕ぎます。すなわち、川の流れの速度を W(W \neq 0)とすると、高橋君は流れの方向に速さ |W| + V で移動します。東を正とした地面に対する速度で表すと、流れが東向き(W > 0)のとき W + V、流れが西向き(W < 0)のとき W - V です。

高橋君の座標は時間の経過とともに連続的に変化します(流れの速度が切り替わる瞬間に座標が飛ぶことはありません)。

Q 個の質問が与えられます。j 番目の質問では、時刻 X_j における高橋君の座標を求めてください。

なお、制約の範囲において、各質問の答えは必ず整数であり、符号付き 64 bit 整数に収まります。

制約

  • 1 \leq N
  • 1 \leq Q \leq 10^5
  • N + Q \leq 2 \times 10^5
  • 1 \leq V \leq 10^9
  • 1 \leq T_1 < T_2 < \cdots < T_N \leq 10^9
  • -10^9 \leq W_i \leq 10^9
  • W_i \neq 0 (1 \leq i \leq N)
  • 0 \leq X_j \leq 10^9 (1 \leq j \leq Q)
  • N, Q, T_i, W_i, V, X_j はすべて整数

入力

N V Q
T_1 W_1
T_2 W_2
\vdots
T_N W_N
X_1
X_2
\vdots
X_Q
  • 1 行目には、川の流れの変化の回数を表す整数 N、高橋君が水面に対してボートを漕ぐ速さを表す整数 V、質問の数を表す整数 Q が、空白区切りで与えられる。
  • 2 行目から N + 1 行目では、川の流れの変化の情報が N 行で与えられる。
  • 1 + i 行目(1 \leq i \leq N)では、i 回目の変化が起こる時刻 T_i と、その時刻以降の川の流れの速度 W_i が、空白区切りで与えられる。
  • N + 2 行目から N + 1 + Q 行目では、質問の時刻が Q 行で与えられる。
  • N + 1 + j 行目(1 \leq j \leq Q)では、j 番目の質問の時刻 X_j が与えられる。

出力

Q 行出力せよ。j 行目には、時刻 X_j における高橋君の座標を整数で出力せよ。


入力例 1

3 2 6
3 1
7 -2
10 3
0
3
5
7
9
12

出力例 1

0
0
6
12
4
10

入力例 2

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

出力例 2

0
0
-16
-32
-14

入力例 3

7 4 10
5 2
12 -6
20 1
35 -3
50 8
65 -1
80 5
0
5
10
12
25
35
49
50
70
100

出力例 3

0
0
30
42
-13
37
-61
-68
87
217

入力例 4

18 12345 22
1 1000000000
10 -999999999
100 500
1000 -400
10000 30000
50000 -20000
100000 1
200000 -1
500000 700000
1000000 -800000
5000000 123456789
10000000 -987654321
50000000 42
100000000 -42
250000000 31415926
500000000 -27182818
750000000 999
900000000 -1000
0
1
9
10
55
999
1000
12345
50000
150000
499999
500000
999999
1000000
7500000
50000000
123456789
250000000
600000000
900000000
950000000
1000000000

出力例 4

0
0
8000098760
9000111105
-36000444375
-80989452200
-80989439355
-81004845330
-79410344355
-80410294355
-83496782009
-83496794355
272674993300
272675705645
305696130705645
-38892297674294355
-38891968883539698
-38893536374294355
-33755984924294355
-37833257774294355
-37833925024294355
-37834592274294355

入力例 5

1 1000000000 1
1000000000 -1000000000
1000000000

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi is riding a boat on a straight river. The river flows in the east-west direction, and Takahashi's position is represented on a number line where east is the positive direction.

At time 0, Takahashi is at coordinate 0.

The flow of the river changes over time. You are given the times T_1, T_2, \ldots, T_N (T_1 < T_2 < \cdots < T_N) when the flow changes, and the velocity of the river's flow after each change, W_1, W_2, \ldots, W_N. W_i > 0 represents an eastward flow, and W_i < 0 represents a westward flow. It is guaranteed that W_i \neq 0 for all i.

The velocity of the river's flow during each time interval is as follows:

  • From time 0 to less than T_1: There is no river flow (velocity 0).
  • For i = 1, 2, \ldots, N-1, from time T_i to less than T_{i+1}: The velocity of the river flow is W_i.
  • From time T_N onwards: The velocity of the river flow is W_N.

Takahashi rows his boat according to the following rules:

  • When there is no river flow, Takahashi does not row his boat and stays in place.
  • When there is river flow, Takahashi always rows his boat in the same direction as the flow at a speed of V relative to the water surface. That is, if the river flow velocity is W (W \neq 0), Takahashi moves in the direction of the flow at a speed of |W| + V. Expressed as velocity relative to the ground (where east is positive), this is W + V when the flow is eastward (W > 0), and W - V when the flow is westward (W < 0).

Takahashi's coordinate changes continuously over time (his coordinate does not jump at the exact moment the flow velocity changes).

You are given Q queries. In the j-th query, find Takahashi's coordinate at time X_j.

Under the constraints, the answer to each query is guaranteed to be an integer and fit within a signed 64-bit integer.

Constraints

  • 1 \leq N
  • 1 \leq Q \leq 10^5
  • N + Q \leq 2 \times 10^5
  • 1 \leq V \leq 10^9
  • 1 \leq T_1 < T_2 < \cdots < T_N \leq 10^9
  • -10^9 \leq W_i \leq 10^9
  • W_i \neq 0 (1 \leq i \leq N)
  • 0 \leq X_j \leq 10^9 (1 \leq j \leq Q)
  • N, Q, T_i, W_i, V, X_j are all integers.

Input

N V Q
T_1 W_1
T_2 W_2
\vdots
T_N W_N
X_1
X_2
\vdots
X_Q
  • The first line contains three space-separated integers: N, the number of times the river flow changes; V, Takahashi's rowing speed relative to the water surface; and Q, the number of queries.
  • The next N lines, from the 2nd line to the (N + 1)-th line, provide information about the changes in the river flow.
  • The (1 + i)-th line (1 \leq i \leq N) contains two space-separated integers: T_i, the time of the i-th change, and W_i, the velocity of the river flow from that time onwards.
  • The next Q lines, from the (N + 2)-th line to the (N + 1 + Q)-th line, provide the query times.
  • The (N + 1 + j)-th line (1 \leq j \leq Q) contains X_j, the time for the j-th query.

Output

Print Q lines. The j-th line should contain Takahashi's coordinate at time X_j as an integer.


Sample Input 1

3 2 6
3 1
7 -2
10 3
0
3
5
7
9
12

Sample Output 1

0
0
6
12
4
10

Sample Input 2

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

Sample Output 2

0
0
-16
-32
-14

Sample Input 3

7 4 10
5 2
12 -6
20 1
35 -3
50 8
65 -1
80 5
0
5
10
12
25
35
49
50
70
100

Sample Output 3

0
0
30
42
-13
37
-61
-68
87
217

Sample Input 4

18 12345 22
1 1000000000
10 -999999999
100 500
1000 -400
10000 30000
50000 -20000
100000 1
200000 -1
500000 700000
1000000 -800000
5000000 123456789
10000000 -987654321
50000000 42
100000000 -42
250000000 31415926
500000000 -27182818
750000000 999
900000000 -1000
0
1
9
10
55
999
1000
12345
50000
150000
499999
500000
999999
1000000
7500000
50000000
123456789
250000000
600000000
900000000
950000000
1000000000

Sample Output 4

0
0
8000098760
9000111105
-36000444375
-80989452200
-80989439355
-81004845330
-79410344355
-80410294355
-83496782009
-83496794355
272674993300
272675705645
305696130705645
-38892297674294355
-38891968883539698
-38893536374294355
-33755984924294355
-37833257774294355
-37833925024294355
-37834592274294355

Sample Input 5

1 1000000000 1
1000000000 -1000000000
1000000000

Sample Output 5

0
D - A Walk Through the Flower Garden

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は花の収集家です。彼は様々な花を集めて花束を作ることを趣味としています。

高橋君の家の前には、東西にまっすぐ無限に伸びる一本の散歩道があります。散歩道は数直線として表され、高橋君の自宅は座標 0 の地点にあります。高橋君は散歩道上を移動しますが、座標 a の地点から座標 b の地点へ移動する際には、a と b の間のすべての座標の地点を通ります(すなわち、途中の座標を飛ばすことはできません)。

この散歩道沿いには、M 種類の花が咲いています。

種類 j(1 \leq j \leq M)の花は、散歩道上の座標 L_j の地点と座標 R_j の地点の間に咲いています。種類 j の花束を完成させるには、散歩の開始から終了までの間に、座標 L_j の地点と座標 R_j の地点の両方にそれぞれ少なくとも 1 回ずつ到達する必要があります。2 つの地点に到達する順序や時点は問いません(同時に訪れる必要もありません)。なお、移動の途中である座標を通過した場合も、その座標に到達したものとみなします。条件を満たした花束からは美しさ V_j を得られます。各種類の花束は高々 1 回しか完成できません。

高橋君は座標 0(自宅)から散歩を開始し、散歩道上を正の方向(東)にも負の方向(西)にも自由に移動できます。何度でも方向を変えることができ、同じ場所を繰り返し通ることもできます。散歩の終了時にどの座標にいても構いません(自宅に戻る必要はありません)。全く移動しないことも許されます。

ただし、散歩に使える時間には限りがあり、散歩における総移動距離(移動した道のりの合計)が D 以下でなければなりません。例えば、座標 0 から座標 3 まで移動し、その後座標 -1 まで移動した場合、総移動距離は 3 + 4 = 7 です。

花束を 1 つも完成させない場合、美しさの合計は 0 です。

高橋君が完成させた花束の美しさの合計の最大値を求めてください。

制約

  • 1 \leq M \leq 5000
  • 1 \leq D \leq 10^9
  • -10^9 \leq L_j < R_j \leq 10^9
  • 1 \leq V_j \leq 10^9
  • 入力はすべて整数である

入力

M D
L_1 R_1 V_1
L_2 R_2 V_2
\vdots
L_M R_M V_M
  • 1 行目には、花の種類数を表す整数 M と、移動可能な総距離を表す整数 D が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目では、各種類の花の情報が与えられる。
  • 1 + j 行目では、種類 j の花が咲いている区間の左端 L_j 、右端 R_j 、および花束の美しさ V_j が、スペース区切りで与えられる。

出力

完成させた花束の美しさの合計の最大値を 1 行で出力せよ。


入力例 1

3 5
1 2 10
-2 -1 20
-1 3 50

出力例 1

60

入力例 2

5 6
1 4 30
-3 -1 40
-2 2 100
5 6 70
-6 -4 80

出力例 2

120

入力例 3

10 18
-8 -3 15
-5 2 40
1 6 25
-10 -7 60
3 9 70
-2 4 30
7 12 45
-12 1 80
-1 1 20
-6 8 100

出力例 3

215

入力例 4

25 120
-50 -40 300
-45 -10 500
-30 5 450
-20 20 800
-10 35 600
5 25 250
15 60 700
40 90 900
-80 -60 650
-70 -5 1200
-5 5 150
25 55 400
55 100 750
-100 -90 1000
-95 10 1300
10 15 90
-15 -14 80
70 110 500
-60 30 1100
-1 1 60
35 36 50
-35 65 1400
80 130 1000
-120 -110 2000
-25 75 1600

出力例 4

5730

入力例 5

1 1
-1000000000 1000000000 1000000000

出力例 5

0

Score : 400 pts

Problem Statement

Takahashi is a flower collector. His hobby is gathering various flowers to create bouquets.

In front of Takahashi's house, there is a straight walking path that extends infinitely to the east and west. The path is represented as a number line, and Takahashi's home is located at coordinate 0. Takahashi moves along the path, and when moving from coordinate a to coordinate b, he passes through all coordinates between a and b (that is, he cannot skip over any coordinates along the way).

Along this path, M types of flowers are blooming.

Type j (1 \leq j \leq M) flowers bloom between coordinate L_j and coordinate R_j on the path. To complete a bouquet of type j, Takahashi must reach both coordinate L_j and coordinate R_j at least once each during the walk (from start to finish). The order and timing of reaching the two points does not matter (they do not need to be visited simultaneously). Note that passing through a coordinate during movement also counts as reaching that coordinate. A completed bouquet yields a beauty value of V_j. Each type of bouquet can be completed at most once.

Takahashi starts his walk at coordinate 0 (his home) and can freely move in the positive direction (east) or negative direction (west) along the path. He can change direction any number of times and can pass through the same place repeatedly. He may end his walk at any coordinate (he does not need to return home). It is also allowed to not move at all.

However, since his time for the walk is limited, the total distance traveled (the sum of all distances moved) must be at most D. For example, if he moves from coordinate 0 to coordinate 3, and then moves to coordinate -1, the total distance traveled is 3 + 4 = 7.

If no bouquets are completed, the total beauty is 0.

Find the maximum possible total beauty of the bouquets that Takahashi can complete.

Constraints

  • 1 \leq M \leq 5000
  • 1 \leq D \leq 10^9
  • -10^9 \leq L_j < R_j \leq 10^9
  • 1 \leq V_j \leq 10^9
  • All input values are integers

Input

M D
L_1 R_1 V_1
L_2 R_2 V_2
\vdots
L_M R_M V_M
  • The first line contains an integer M representing the number of flower types and an integer D representing the maximum total distance, separated by a space.
  • Lines 2 through M + 1 give the information for each flower type.
  • Line 1 + j contains the left endpoint L_j, right endpoint R_j of the interval where type j flowers bloom, and the beauty value V_j of the bouquet, separated by spaces.

Output

Output the maximum possible total beauty of the completed bouquets in a single line.


Sample Input 1

3 5
1 2 10
-2 -1 20
-1 3 50

Sample Output 1

60

Sample Input 2

5 6
1 4 30
-3 -1 40
-2 2 100
5 6 70
-6 -4 80

Sample Output 2

120

Sample Input 3

10 18
-8 -3 15
-5 2 40
1 6 25
-10 -7 60
3 9 70
-2 4 30
7 12 45
-12 1 80
-1 1 20
-6 8 100

Sample Output 3

215

Sample Input 4

25 120
-50 -40 300
-45 -10 500
-30 5 450
-20 20 800
-10 35 600
5 25 250
15 60 700
40 90 900
-80 -60 650
-70 -5 1200
-5 5 150
25 55 400
55 100 750
-100 -90 1000
-95 10 1300
10 15 90
-15 -14 80
70 110 500
-60 30 1100
-1 1 60
35 36 50
-35 65 1400
80 130 1000
-120 -110 2000
-25 75 1600

Sample Output 4

5730

Sample Input 5

1 1
-1000000000 1000000000 1000000000

Sample Output 5

0
E - Chain of Felling

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は森林管理の仕事をしています。管理区域には N 本の木が一列に並んでおり、左から順に木 1 , 木 2 , \ldots , 木 N と番号が付けられています。

各木 i には、収穫量 R_i と 日照影響値 D_i が設定されています。高橋君が木 i を伐採すると、木材として R_i の利益を得ます。しかし同時に、その木が倒れることで日照環境が変化し、まだ伐採されていない木のうち、木 i に隣接しているもの(番号が 1 差のもの)すべての収穫量が D_i だけ減少します。収穫量が負になることもあり得ます。

高橋君は N 本の木のうち、ちょうど K 本を選んで伐採します。伐採は 1 本ずつ順番に行い、伐採する順序は自由に決められます。ある木を伐採した瞬間に得られる利益は、その時点でのその木の収穫量です(それまでに他の木の伐採による影響で変化している場合があります)。

高橋君が得られる合計利益の最大値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq N
  • N \times K \leq 2 \times 10^7
  • -10^6 \leq R_i \leq 10^6
  • 0 \leq D_i \leq 10^6
  • 入力はすべて整数である

入力

N K
R_1 D_1
R_2 D_2
:
R_N D_N
  • 1 行目には、木の総数を表す N と、伐採する木の数を表す K が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各木の収穫量と日照影響値が与えられる。
  • 1 + i 行目では、木 i の収穫量 R_i と日照影響値 D_i が、スペース区切りで与えられる。

出力

高橋君が得られる合計利益の最大値を 1 行で出力せよ。


入力例 1

3 2
10 3
5 2
8 4

出力例 1

18

入力例 2

4 2
7 10
20 0
6 5
15 1

出力例 2

35

入力例 3

10 5
12 4
-3 7
25 0
8 6
17 2
-10 5
30 3
4 8
19 1
11 9

出力例 3

103

入力例 4

30 12
45 3
-20 15
100 0
35 7
60 12
-50 20
80 5
25 1
-10 30
90 4
15 9
70 6
-35 11
55 2
40 18
-5 0
110 8
20 14
65 3
-25 16
95 10
30 5
75 7
-40 13
50 4
85 6
10 19
-15 2
120 1
5 17

出力例 4

1005

入力例 5

1 1
-1000000 1000000

出力例 5

-1000000

Score : 433 pts

Problem Statement

Takahashi works in forest management. In his management area, N trees are lined up in a row, numbered 1, 2, \ldots, N from left to right.

Each tree i has a yield R_i and a sunlight impact value D_i. When Takahashi cuts down tree i, he gains a profit of R_i from the timber. However, at the same time, the falling of the tree changes the sunlight environment, and the yield of all currently uncut trees adjacent to tree i (i.e., those whose indices differ by exactly 1) decreases by D_i. The yield of a tree can become negative.

Takahashi will choose exactly K trees out of the N trees to cut down. He cuts them down one by one, and he can choose the order of cutting freely. The profit obtained at the moment he cuts down a tree is its yield at that moment (which may have already changed due to the effects of previously cut trees).

Find the maximum total profit Takahashi can obtain.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq N
  • N \times K \leq 2 \times 10^7
  • -10^6 \leq R_i \leq 10^6
  • 0 \leq D_i \leq 10^6
  • All input values are integers.

Input

N K
R_1 D_1
R_2 D_2
:
R_N D_N
  • The first line contains N, the total number of trees, and K, the number of trees to cut down, separated by a space.
  • The next N lines (from the 2nd to the (N + 1)-th line) provide the yield and sunlight impact value of each tree.
  • Specifically, the (1 + i)-th line contains the yield R_i and the sunlight impact value D_i of tree i, separated by a space.

Output

Print the maximum total profit Takahashi can obtain in a single line.


Sample Input 1

3 2
10 3
5 2
8 4

Sample Output 1

18

Sample Input 2

4 2
7 10
20 0
6 5
15 1

Sample Output 2

35

Sample Input 3

10 5
12 4
-3 7
25 0
8 6
17 2
-10 5
30 3
4 8
19 1
11 9

Sample Output 3

103

Sample Input 4

30 12
45 3
-20 15
100 0
35 7
60 12
-50 20
80 5
25 1
-10 30
90 4
15 9
70 6
-35 11
55 2
40 18
-5 0
110 8
20 14
65 3
-25 16
95 10
30 5
75 7
-40 13
50 4
85 6
10 19
-15 2
120 1
5 17

Sample Output 4

1005

Sample Input 5

1 1
-1000000 1000000

Sample Output 5

-1000000