A - Many A+B Problems

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N 個の整数の 2 つ組 (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N) が与えられます。 各 i = 1, 2, \ldots, N について、A_i + B_i を出力してください。

制約

  • 1 \leq N \leq 1000
  • -10^9 \leq A_i, B_i \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には A_i+B_i を出力せよ。


入力例 1

4
3 5
2 -6
-5 0
314159265 123456789

出力例 1

8
-4
-5
437616054
  • 1 行目には、A_1 + B_1 = 3 + 5 = 8 を、
  • 2 行目には、A_2 + B_2 = 2 + (-6) = -4 を、
  • 3 行目には、A_3 + B_3 = (-5) + 0 = -5 を、
  • 4 行目には、A_4 + B_4 = 314159265 + 123456789 = 437616054 を出力します。

Score : 100 points

Problem Statement

You are given N pairs of integers: (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N). For each i = 1, 2, \ldots, N, print A_i + B_i.

Constraints

  • 1 \leq N \leq 1000
  • -10^9 \leq A_i, B_i \leq 10^9
  • All values in the input are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print N lines. For i = 1, 2, \ldots, N, the i-th line should contain A_i+B_i.


Sample Input 1

4
3 5
2 -6
-5 0
314159265 123456789

Sample Output 1

8
-4
-5
437616054
  • The first line should contain A_1 + B_1 = 3 + 5 = 8.
  • The second line should contain A_2 + B_2 = 2 + (-6) = -4.
  • The third line should contain A_3 + B_3 = (-5) + 0 = -5.
  • The fourth line should contain A_4 + B_4 = 314159265 + 123456789 = 437616054.
B - Counting Passes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N 人の人 1,2,\dots,N がある試験を受け、人 i は A_i 点を取りました。
この試験では、 L 点以上を取った人のみが合格となります。
N 人のうち何人が合格したか求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 100
  • 1 \le L \le 1000
  • 0 \le A_i \le 1000

入力

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

N L
A_1 A_2 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

5 60
60 20 100 90 40

出力例 1

3

5 人が試験を受けました。 60 点以上取ると合格です。

  • 人 1 は 60 点を取ったので、合格です。
  • 人 2 は 20 点を取ったので、不合格です。
  • 人 3 は 100 点を取ったので、合格です。
  • 人 4 は 90 点を取ったので、合格です。
  • 人 5 は 40 点を取ったので、不合格です。

以上より、合格したのは 3 人だと分かります。


入力例 2

4 80
79 78 77 76

出力例 2

0

合格者がいない場合もあります。


入力例 3

10 50
31 41 59 26 53 58 97 93 23 84

出力例 3

6

Score : 100 points

Problem Statement

N people labeled 1,2,\dots,N took an exam, and person i scored A_i points.
Only those who scored at least L points pass this exam.
Determine how many people out of the N have passed the exam.

Constraints

  • All input values are integers.
  • 1 \le N \le 100
  • 1 \le L \le 1000
  • 0 \le A_i \le 1000

Input

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

N L
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5 60
60 20 100 90 40

Sample Output 1

3

Five people took the exam. You need to score at least 60 points to pass.

  • Person 1 scored 60 points, so they passed.
  • Person 2 scored 20 points, so they did not pass.
  • Person 3 scored 100 points, so they passed.
  • Person 4 scored 90 points, so they passed.
  • Person 5 scored 40 points, so they did not pass.

From the above, we can see that three people have passed.


Sample Input 2

4 80
79 78 77 76

Sample Output 2

0

There may be cases no one has passed.


Sample Input 3

10 50
31 41 59 26 53 58 97 93 23 84

Sample Output 3

6
C - Piano 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N) と、長さ M の数列 B=(B_1,B_2,\dots,B_M) が与えられます。ここで、A,B のすべての要素は互いに相異なります。A,B のすべての要素を昇順に並べた長さ N+M の数列 C=(C_1,C_2,\dots,C_{N+M}) において、A に現れる要素が2つ連続するかどうか判定してください。

制約

  • 1\leq N,M \leq 100
  • 1\leq A_i,B_j \leq 200
  • A_1, A_2, \dots, A_N, B_1, B_2, \dots, B_M は相異なる
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

A に現れる要素が C において2つ連続するならば Yes を、そうでないなら No を出力せよ。


入力例 1

3 2
3 2 5
4 1

出力例 1

Yes

C=(1,2,3,4,5) です。A に現れる 2,3 が C で連続しているため、Yes を出力します。


入力例 2

3 2
3 1 5
4 2

出力例 2

No

C=(1,2,3,4,5) です。A に現れる要素が C で連続している箇所はないため、No を出力します。


入力例 3

1 1
1
2

出力例 3

No

Score : 200 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_N) of length N and a sequence B=(B_1,B_2,\dots,B_M) of length M. Here, all elements of A and B are pairwise distinct. Determine whether the sequence C=(C_1,C_2,\dots,C_{N+M}) formed by sorting all elements of A and B in ascending order contains two consecutive elements appearing in A.

Constraints

  • 1 \leq N, M \leq 100
  • 1 \leq A_i, B_j \leq 200
  • A_1, A_2, \dots, A_N, B_1, B_2, \dots, B_M are distinct.
  • 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 B_2 \dots B_M

Output

If C contains two consecutive elements appearing in A, print Yes; otherwise, print No.


Sample Input 1

3 2
3 2 5
4 1

Sample Output 1

Yes

C=(1,2,3,4,5). Since 2 and 3 from A occur consecutively in C, print Yes.


Sample Input 2

3 2
3 1 5
4 2

Sample Output 2

No

C=(1,2,3,4,5). Since no two elements from A occur consecutively in C, print No.


Sample Input 3

1 1
1
2

Sample Output 3

No
D - Geometric Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

A が等比数列であるか判定してください。

制約

  • 2\leq N\leq 100
  • 1\leq A_i\leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A が 等比数列ならば Yes を、そうでないならば No を出力せよ。


入力例 1

5
3 6 12 24 48

出力例 1

Yes

A=(3,6,12,24,48) です。
A は初項 3, 公比 2, 項数 5 の等比数列となっています。
よって、Yes を出力します。


入力例 2

3
1 2 3

出力例 2

No

A=(1,2,3) です。
A_1:A_2=1:2\neq 2:3=A_2:A_3 より、A は等比数列ではありません。
よって、No を出力します。


入力例 3

2
10 8

出力例 3

Yes

A は初項 10, 公比 0.8, 項数 2 の等比数列となっています。
よって、Yes を出力します。

Score : 200 points

Problem Statement

You are given a length-N sequence A=(A_1,A_2,\ldots,A_N) of positive integers.

Determine whether A is a geometric progression.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

If A is a geometric progression, print Yes; otherwise, print No.


Sample Input 1

5
3 6 12 24 48

Sample Output 1

Yes

A=(3,6,12,24,48).
A is a geometric progression with first term 3, common ratio 2, and five terms.
Therefore, print Yes.


Sample Input 2

3
1 2 3

Sample Output 2

No

A=(1,2,3).
Since A_1 : A_2 = 1 : 2 \neq 2 : 3 = A_2 : A_3, A is not a geometric progression.
Therefore, print No.


Sample Input 3

2
10 8

Sample Output 3

Yes

A is a geometric progression with first term 10, common ratio 0.8, and two terms.
Therefore, print Yes.

E - Cards Query Problem

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。

  • 1 i j \colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる
  • 2 i \colon 箱 i に入っているカードに書かれた数を昇順ですべて答える
  • 3 i \colon 数 i が書かれたカードが入っている箱の番号を昇順ですべて答える

ただし、以下の点に注意してください。

  • 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
  • 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • 2 番目の形式のクエリについて、
    • 1 \leq i \leq N
    • このクエリが与えられる時点で箱 i にカードが入っている
  • 3 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
  • 出力するべき数は合計で 2 \times 10^5 個以下
  • 入力はすべて整数

入力

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ただし、\mathrm{query}_q は q 個目のクエリを表しており、次のいずれかの形式で与えられる。

1 i j
2 i
3 i

出力

2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。


入力例 1

5
8
1 1 1
1 2 4
1 1 4
2 4
1 1 4
2 4
3 1
3 2

出力例 1

1 2
1 1 2
1 4
4

クエリを順に処理していきます。

  • カードに 1 を書き込み、箱 1 に入れます。
  • カードに 2 を書き込み、箱 4 に入れます。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 箱 4 に入っているカードに書かれた数は 1, 2 です。
    • 1, 2 の順に出力してください。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 箱 4 に入っているカードに書かれた数は 1, 1, 2 です。
    • 1 を 2 度出力することに注意してください。
  • 数 1 が書かれたカードが入っている箱は箱 1, 4 です。
    • 箱 4 には数 1 が書かれたカードが 2 枚入っていますが、4 は 1 回しか出力しないことに注意してください。
  • 数 2 が書かれたカードが入っている箱は箱 4 です。

入力例 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

出力例 2

1 2 200000
1

Score : 300 points

Problem Statement

We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.

  • 1 i j \colon Write the number i on a blank card and put it into box j.
  • 2 i \colon Report all numbers written on the cards in box i, in ascending order.
  • 3 i \colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.

Here, note the following.

  • In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
  • In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • For a query of the first kind:
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • For a query of the second kind:
    • 1 \leq i \leq N
    • Box i contains some cards when this query is given.
  • For a query of the third kind:
    • 1 \leq i \leq 2 \times 10^5
    • Some boxes contain a card with the number i when this query is given.
  • At most 2 \times 10^5 numbers are to be reported.
  • All values in the input are integers.

Input

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:

1 i j
2 i
3 i

Output

Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.


Sample Input 1

5
8
1 1 1
1 2 4
1 1 4
2 4
1 1 4
2 4
3 1
3 2

Sample Output 1

1 2
1 1 2
1 4
4

Let us process the queries in order.

  • Write 1 on a card and put it into box 1.
  • Write 2 on a card and put it into box 4.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1 and 2.
    • Print 1 and 2 in this order.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1, 1, and 2.
    • Note that you should print 1 twice.
  • Boxes 1 and 4 contain a card with the number 1.
    • Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
  • Boxes 4 contains a card with the number 2.

Sample Input 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

Sample Output 2

1 2 200000
1
F - Cheese

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

ピザ屋で働く高橋くんは、まかないとして美味しいチーズピザを作ることにしました。
今、高橋くんの目の前に N 種類のチーズがあります。
i 種類目のチーズは 1 [g] あたりのおいしさが A_i で、 B_i [g] あります。
ピザのおいしさは、ピザに乗せたチーズのおいしさの総和で決まります。
但し、チーズを使いすぎると怒られてしまうため、乗せたチーズの重さは合計で W [g] 以下である必要があります。
この条件のもとで、可能なピザのおいしさの最大値を求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 1 \le W \le 3 \times 10^8
  • 1 \le A_i \le 10^9
  • 1 \le B_i \le 1000

入力

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

N W
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを整数として出力せよ。


入力例 1

3 5
3 1
4 2
2 3

出力例 1

15

1 種類目のチーズを 1 [g] 、 2 種類目のチーズを 2 [g] 、 3 種類目のチーズを 2 [g] 乗せるのが最適です。
このとき、ピザのおいしさは 15 となります。


入力例 2

4 100
6 2
1 5
3 9
8 7

出力例 2

100

チーズの重量の総和が W [g] に満たないケースもあります。


入力例 3

10 3141
314944731 649
140276783 228
578012421 809
878510647 519
925326537 943
337666726 611
879137070 306
87808915 39
756059990 244
228622672 291

出力例 3

2357689932073

Score : 300 points

Problem Statement

Takahashi, who works for a pizza restaurant, is making a delicious cheese pizza for staff meals.
There are N kinds of cheese in front of him.
The deliciousness of the i-th kind of cheese is A_i per gram, and B_i grams of this cheese are available.
The deliciousness of the pizza will be the total deliciousness of cheese he puts on top of the pizza.
However, using too much cheese would make his boss angry, so the pizza can have at most W grams of cheese on top of it.
Under this condition, find the maximum possible deliciousness of the pizza.

Constraints

  • All values in input are integers.
  • 1 \le N \le 3 \times 10^5
  • 1 \le W \le 3 \times 10^8
  • 1 \le A_i \le 10^9
  • 1 \le B_i \le 1000

Input

Input is given from Standard Input in the following format:

N W
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answer as an integer.


Sample Input 1

3 5
3 1
4 2
2 3

Sample Output 1

15

The optimal choice is to use 1 gram of cheese of the first kind, 2 grams of the second kind, and 2 grams of the third kind.
The pizza will have a deliciousness of 15.


Sample Input 2

4 100
6 2
1 5
3 9
8 7

Sample Output 2

100

There may be less than W grams of cheese in total.


Sample Input 3

10 3141
314944731 649
140276783 228
578012421 809
878510647 519
925326537 943
337666726 611
879137070 306
87808915 39
756059990 244
228622672 291

Sample Output 3

2357689932073
G - Bitmask

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

0, 1, ? からなる文字列 S および整数 N が与えられます。 S に含まれる ? をそれぞれ 0 または 1 に置き換えて 2 進整数とみなしたときに得られる値の集合を T とします。 たとえば、S= ?0? のとき、 T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace です。

T に含まれる N 以下の値のうち最大のものを (10 進整数として) 出力してください。 N 以下の値が T に含まれない場合は、代わりに -1 を出力してください。

制約

  • S は 0, 1, ? からなる文字列
  • S の長さは 1 以上 60 以下
  • 1\leq N \leq 10^{18}
  • N は整数

入力

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

S
N

出力

答えを出力せよ。


入力例 1

?0?
2

出力例 1

1

問題文中で例示したとおり、T=\lbrace 0,1,4,5\rbrace です。 T に含まれる N 以下の値は 0 と 1 なので、そのうちの最大である 1 を出力します。


入力例 2

101
4

出力例 2

-1

T=\lbrace 5\rbrace であるため、N 以下の値は T に含まれません。


入力例 3

?0?
1000000000000000000

出力例 3

5

Score : 400 points

Problem Statement

You are given an integer N and a string S consisting of 0, 1, and ?. Let T be the set of values that can be obtained by replacing each ? in S with 0 or 1 and interpreting the result as a binary integer. For instance, if S= ?0?, we have T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace.

Print (as a decimal integer) the greatest value in T less than or equal to N. If T does not contain a value less than or equal to N, print -1 instead.

Constraints

  • S is a string consisting of 0, 1, and ?.
  • The length of S is between 1 and 60, inclusive.
  • 1\leq N \leq 10^{18}
  • N is an integer.

Input

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

S
N

Output

Print the answer.


Sample Input 1

?0?
2

Sample Output 1

1

As shown in the problem statement, T=\lbrace 0,1,4,5\rbrace. Among them, 0 and 1 are less than or equal to N, so you should print the greatest of them, 1.


Sample Input 2

101
4

Sample Output 2

-1

We have T=\lbrace 5\rbrace, which does not contain a value less than or equal to N.


Sample Input 3

?0?
1000000000000000000

Sample Output 3

5
H - Reachable Set

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

N 頂点 M 辺からなる無向グラフがあります。 頂点には 1,2,\ldots,N の番号がついており、i 番目 (1\leq i\leq M) の辺は頂点 u _ i と頂点 v _ i を結んでいます。

k=1,2,\ldots,N について、次の問題を解いてください。

次の操作について考える。

  • 頂点を一つ選ぶ。選んだ頂点と、選んだ頂点に接続する全ての辺をグラフから削除する。

上の操作を繰り返すことで、次の条件を満たすことができるか判定せよ。

  • 頂点 1 から辺をたどって到達できる頂点の集合が、頂点 1, 頂点 2,\ldots, 頂点 k のちょうど k 個からなる。

可能な場合、条件を満たすために少なくとも何回操作を行う必要があるか求めよ。

制約

  • 1\leq N\leq2\times10 ^ 5
  • 0\leq M\leq3\times10 ^ 5
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M)
  • (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M)
  • 入力はすべて整数

入力

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

N M
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ M v _ M

出力

N 行にわたって出力せよ。 i 行目 (1\leq i\leq N) には、k=i としたときの条件を満たすことができないなら -1 を、満たすことができるなら条件を満たすために行う必要がある操作の回数を出力せよ。


入力例 1

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

出力例 1

2
3
3
2
1
0

たとえば、k=2 としたときの問題では、頂点 3, 頂点 4, 頂点 5 の 3 つの頂点を選んで削除することで、頂点 1 から到達できる頂点を頂点 1, 頂点 2 の 2 つだけにすることができます。 頂点を 2 つ以下だけ削除して条件を満たすことはできないため、2 行目には 3 を出力してください。

また、k=6 としたときの問題では、頂点をひとつも削除しないことで頂点 1 から到達できる頂点を頂点 1, 頂点 2,\ldots, 頂点 6 の 6 つにすることができます。 なので、6 行目には 0 を出力してください。


入力例 2

5 4
1 5
2 3
3 4
4 5

出力例 2

1
-1
-1
-1
0

入力例 3

2 0

出力例 3

0
-1

辺が 1 つもない場合もあります。


入力例 4

11 25
6 9
5 9
2 3
1 9
10 11
4 5
9 10
8 9
7 8
3 5
1 7
6 10
4 7
7 9
1 10
4 11
3 8
2 7
3 4
1 8
2 8
3 7
2 10
1 6
6 11

出力例 4

5
-1
-1
-1
-1
-1
4
3
2
1
0

Score : 450 points

Problem Statement

You are given an undirected graph with N vertices and M edges. The vertices are numbered 1,2,\ldots,N, and the i‑th edge (1 \le i \le M) connects vertices u_i and v_i.

For each k = 1,2,\ldots,N, solve the following problem:

Consider the following operation.

  • Choose one vertex, and delete that vertex together with all edges incident to it.

Determine whether one can repeat this operation to satisfy the following condition:

  • The set of vertices reachable from vertex 1 by traversing edges consists exactly of the k vertices 1,2,\ldots,k.

If it is possible, find the minimum number of operations required to do so.

Constraints

  • 1 \le N \le 2 \times 10^{5}
  • 0 \le M \le 3 \times 10^{5}
  • 1 \le u_i < v_i \le N\ (1 \le i \le M)
  • (u_i,v_i) \ne (u_j,v_j)\ (1 \le i < j \le M)
  • All input values are integers.

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print N lines. On the i-th line (1 \le i \le N), if one cannot satisfy the condition for k=i, print -1; otherwise, print the minimum number of operations required to satisfy the condition.


Sample Input 1

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

Sample Output 1

2
3
3
2
1
0

For example, for k = 2, deleting the three vertices 3, 4, 5 makes the set of vertices reachable from vertex 1 equal to the two vertices 1, 2. It is impossible with two or fewer deletions, so print 3 on the 2nd line.

For k = 6, deleting zero vertices makes the set of vertices reachable from vertex 1 equal to the six vertices 1,2,\ldots,6, so print 0 on the 6th line.


Sample Input 2

5 4
1 5
2 3
3 4
4 5

Sample Output 2

1
-1
-1
-1
0

Sample Input 3

2 0

Sample Output 3

0
-1

There may be no edges.


Sample Input 4

11 25
6 9
5 9
2 3
1 9
10 11
4 5
9 10
8 9
7 8
3 5
1 7
6 10
4 7
7 9
1 10
4 11
3 8
2 7
3 4
1 8
2 8
3 7
2 10
1 6
6 11

Sample Output 4

5
-1
-1
-1
-1
-1
4
3
2
1
0
I - Smooth Occlusion

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

高橋くんには歯が 2N 本あり、N 本は上の歯、残りの N 本は下の歯です。

左から i 本目 (1\leq i\leq N) の上の歯の長さは U _ i で、左から i 本目 (1\leq i\leq N) の下の歯の長さは D _ i です。

高橋くんの歯が次の 2 つの条件をどちらも満たしているとき、歯が「うまく噛み合っている」ということにします。

  • ある整数 H が存在し、1\leq i\leq N を満たすすべての整数 i について、U _ i+D _ i=H が成り立つ。
  • 1\leq i\lt N を満たすすべての整数 i について、\vert U _ i-U _ {i+1}\vert\leq X が成り立つ。

高橋くんは次の操作を好きなだけ繰り返すことができます。

  • 1 円を支払って歯削り機を使い、長さが正であるような歯を 1 つ選んでその歯の長さを 1 減らす。

この操作以外の方法で歯の長さを変えることはできません。

高橋くんが歯をうまく噛み合わせるために支払うことになる最小の金額を求めてください。

制約

  • 2\leq N\leq2\times10 ^ 5
  • 1\leq U _ i\leq10 ^ 9\ (1\leq i\leq N)
  • 1\leq D _ i\leq10 ^ 9\ (1\leq i\leq N)
  • 1\leq X\leq10 ^ 9
  • 入力はすべて整数

入力

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

N X
U _ 1 D _ 1
U _ 2 D _ 2
\vdots
U _ N D _ N

出力

答えを出力せよ。


入力例 1

4 3
3 1
4 1
5 9
2 6

出力例 1

15

はじめ、高橋くんのそれぞれの歯の高さは以下のようになっています。

たとえば、以下のようにして高橋くんの歯をうまく噛み合わせることができます。

15 円を支払うことでそれぞれの歯をこの長さにすることができます。

14 円以下を支払って高橋くんの歯をうまく噛み合わせることはできないため、15 を出力してください。


入力例 2

4 1000000000
3 3
3 3
3 3
3 3

出力例 2

0

歯の長さを変えなくてもうまく噛み合っている場合があります。


入力例 3

4 1
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1 1

出力例 3

5999999994

答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。


入力例 4

15 128
748 169
586 329
972 529
432 519
408 587
138 249
656 114
632 299
984 755
404 772
155 506
832 854
353 465
387 374
567 385

出力例 4

9460

Score : 500 points

Problem Statement

Takahashi has 2N teeth: N upper teeth and N lower teeth.

The length of the i-th upper tooth from the left (1 \leq i \leq N) is U _ i, and the length of the i-th lower tooth from the left (1 \leq i \leq N) is D _ i.

His teeth are said to “fit together well” if both of the following conditions are satisfied:

  • There exists an integer H such that U _ i + D _ i = H for every integer i with 1 \leq i \leq N.
  • \lvert U _ i - U _ {i+1} \rvert \leq X for every integer i with 1 \leq i < N.

He can perform the following operation any number of times:

  • Pay 1 yen to use a tooth-grinding machine, choose exactly one tooth whose length is positive, and reduce its length by 1.

No other method may be used to change the lengths of the teeth.

Find the minimum total amount of money he needs to pay to make his teeth fit together well.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq U _ i \leq 10^9 \ (1 \leq i \leq N)
  • 1 \leq D _ i \leq 10^9 \ (1 \leq i \leq N)
  • 1 \leq X \leq 10^9
  • All input values are integers.

Input

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

N X
U _ 1 D _ 1
U _ 2 D _ 2
\vdots
U _ N D _ N

Output

Print the answer.


Sample Input 1

4 3
3 1
4 1
5 9
2 6

Sample Output 1

15

Initially, Takahashi’s teeth have the following lengths:

For example, you can make them fit together well in the following way:

It costs 15 yen to achieve these lengths.

It is impossible to make them fit together well with 14 yen or less, so print 15.


Sample Input 2

4 1000000000
3 3
3 3
3 3
3 3

Sample Output 2

0

It is possible that the teeth already fit together well without any changes.


Sample Input 3

4 1
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1 1

Sample Output 3

5999999994

Note that the answer may exceed the 32-bit integer range.


Sample Input 4

15 128
748 169
586 329
972 529
432 519
408 587
138 249
656 114
632 299
984 755
404 772
155 506
832 854
353 465
387 374
567 385

Sample Output 4

9460