A - Happy Birthday! 4

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

正整数 X,Y,Z が与えられます。

高橋君と青木君は現在それぞれ X 歳、Y 歳です。

高橋君と青木君は毎年 1 月 1 日を迎えると同時に 1 歳ずつ歳を取ります。

(今年も含めて)これから高橋君の年齢が青木君の年齢のちょうど Z 倍となるタイミングが存在するか判定してください。

制約

  • 1\le X,Y \le 100
  • 2\le Z\le 10
  • 入力される値は全て整数

入力

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

X Y Z

出力

高橋君の年齢が青木君の年齢のちょうど Z 倍となるタイミングが存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

44 20 2

出力例 1

Yes

今から 4 年後に高橋君は 48 歳、青木君は 24 歳になり、高橋君の年齢は青木君の年齢のちょうど 2 倍になります。したがって、 Yes を出力してください。


入力例 2

28 10 3

出力例 2

No

これから高橋君の年齢が青木君の年齢のちょうど 3 倍となるタイミングは存在しません。したがって、No を出力してください。


入力例 3

50 5 10

出力例 3

Yes

現在の高橋君の年齢は青木君の年齢のちょうど 10 倍です。

今年も含めてこれから高橋君の年齢が青木君の年齢のちょうど Z 倍となるタイミングが存在するか判定することに注意してください。


入力例 4

1 100 2

出力例 4

No

Score : 100 points

Problem Statement

You are given positive integers X,Y,Z.

Takahashi and Aoki are currently X years old and Y years old, respectively.

Takahashi and Aoki age by 1 year simultaneously on every January 1st.

Determine whether there is a moment in the future (including this year) when Takahashi's age becomes exactly Z times Aoki's age.

Constraints

  • 1\le X,Y \le 100
  • 2\le Z\le 10
  • All input values are integers.

Input

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

X Y Z

Output

Output Yes if there is a moment in the future when Takahashi's age becomes exactly Z times Aoki's age, and No otherwise.


Sample Input 1

44 20 2

Sample Output 1

Yes

Four years from now, Takahashi will be 48 years old and Aoki will be 24 years old, so Takahashi's age will be exactly twice Aoki's age. Thus, output Yes.


Sample Input 2

28 10 3

Sample Output 2

No

There will never be a moment when Takahashi's age becomes exactly three times Aoki's age. Thus, output No.


Sample Input 3

50 5 10

Sample Output 3

Yes

Currently, Takahashi's age is exactly 10 times Aoki's age.

Note that you need to determine whether there will be such a moment in the future including this year.


Sample Input 4

1 100 2

Sample Output 4

No
B - Train Car

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

N 両編成の電車があります。 この電車の前から K 両目の車両は後ろから何両目ですか?

制約

  • 1 \leq K \leq N \leq 100
  • 入力される値はすべて整数

入力

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

N K  

出力

答えを 1 行で出力せよ。


入力例 1

5 2

出力例 1

4

5 両編成の電車で前から 2 両目の車両は後ろから 4 両目です。


入力例 2

1 1

出力例 2

1

入力例 3

99 50

出力例 3

50

Score : 100 points

Problem Statement

There is a train consisting of N cars. What is the position, counted from the back, of the car that is K-th from the front of this train?

Constraints

  • 1 \leq K \leq N \leq 100
  • All input values are integers.

Input

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

N K  

Output

If the car is X-th from the back, output the integer X in one line.


Sample Input 1

5 2

Sample Output 1

4

For a train consisting of five cars, the car that is second from the front is fourth from the back.


Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

99 50

Sample Output 3

50
C - 9x9 Sum

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 150 点

問題文

九九の表に現れる 81 個の整数のうち、X でないものの総和を求めてください。

縦 9 マス、横 9 マスのグリッドがあります。
グリッドの各マスには整数が書きこまれていて、グリッドの上から i 行目、左から j 列目のマスには i \times j が書きこまれています。
整数 X が与えられます。グリッドに書きこまれた 81 個の整数のうち X でないものの総和を求めてください。値の等しい整数が異なるマスに書きこまれている場合は別々に加算する点に注意してください。

制約

  • X は 1 以上 81 以下の整数

入力

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

X

出力

グリッドに書きこまれた 81 個の整数のうち X でないものの総和を出力せよ。


入力例 1

1

出力例 1

2024

グリッドに 1 が書きこまれたマスは上から 1 行目、左から 1 列目のマスのみです。1 でない整数を全て足し合わせると総和は 2024 になります。


入力例 2

11

出力例 2

2025

グリッドに 11 が書きこまれたマスは存在しません。よって 81 個の整数全てを足し合わせた総和である 2025 が答えとなります。


入力例 3

24

出力例 3

1929

Score : 150 points

Problem Statement

Among the 81 integers that appear in the 9-by-9 multiplication table, find the sum of those that are not X.

There is a grid of size 9 by 9.
Each cell of the grid contains an integer: the cell at the i-th row from the top and the j-th column from the left contains i \times j.
You are given an integer X. Among the 81 integers written in this grid, find the sum of those that are not X. If the same value appears in multiple cells, add it for each cell.

Constraints

  • X is an integer between 1 and 81, inclusive.

Input

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

X

Output

Print the sum of the integers that are not X among the 81 integers written in the grid.


Sample Input 1

1

Sample Output 1

2024

The only cell with 1 in the grid is the cell at the 1st row from the top and 1st column from the left. Summing all integers that are not 1 yields 2024.


Sample Input 2

11

Sample Output 2

2025

There is no cell containing 11 in the grid. Thus, the answer is 2025, the sum of all 81 integers.


Sample Input 3

24

Sample Output 3

1929
D - Matrix Transposition

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

H 行 W 列の行列 A が与えられます。
A の上から i 行目、左から j 列目の要素は A_{i,j} です。

ここで、W 行 H 列の行列 B を、上から i 行目、左から j 列目の要素が A_{j,i} と一致するような行列として定めます。
すなわち、B は A の転置行列です。

B を出力してください。

制約

  • 1\leq H,W \leq 10^5
  • H \times W \leq 10^5
  • 1 \leq A_{i,j} \leq 10^9
  • 入力は全て整数である

入力

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

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

出力

B を以下の形式で出力せよ。

B_{1,1} B_{1,2} \ldots B_{1,H}
B_{2,1} B_{2,2} \ldots B_{2,H}
\vdots
B_{W,1} B_{W,2} \ldots B_{W,H}

入力例 1

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

出力例 1

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

たとえば A_{2,1}=4 なので、転置行列 B の上から 1 行目、左から 2 列目の要素は 4 になります。


入力例 2

2 2
1000000000 1000000000
1000000000 1000000000

出力例 2

1000000000 1000000000
1000000000 1000000000

Score : 200 points

Problem Statement

You are given an H-by-W matrix A.
The element at the i-th row from the top and j-th column from the left of A is A_{i,j}.

Let B be a W-by-H matrix whose element at the i-th row from the top and j-th column from the left equals A_{j, i}.
That is, B is the transpose of A.

Print B.

Constraints

  • 1\leq H,W \leq 10^5
  • H \times W \leq 10^5
  • 1 \leq A_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

Output

Print B in the following format:

B_{1,1} B_{1,2} \ldots B_{1,H}
B_{2,1} B_{2,2} \ldots B_{2,H}
\vdots
B_{W,1} B_{W,2} \ldots B_{W,H}

Sample Input 1

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

Sample Output 1

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

For example, we have A_{2,1}=4, so the element at the 1-st row from the top and 2-nd column from the left of the transpose B is 4.


Sample Input 2

2 2
1000000000 1000000000
1000000000 1000000000

Sample Output 2

1000000000 1000000000
1000000000 1000000000
E - Count ABC Again

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 350 点

問題文

長さ N の文字列 S が与えられます。クエリが Q 個与えられるので、順番に処理してください。

i 番目のクエリは以下の通りです。

  • 整数 X_i と文字 C_i が与えられるので、 S の X_i 番目の文字を C_i に置き換える。その後、 S に文字列 ABC が部分文字列として何箇所含まれるかを出力する。

ここで、 S の 部分文字列 とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ab は abc の部分文字列ですが、ac は abc の部分文字列ではありません。

制約

  • 3\le N\le 2\times 10^5
  • 1\le Q\le 2\times 10^5
  • S は英大文字からなる長さ N の文字列
  • 1\le X_i\le N
  • C_i は英大文字

入力

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

N Q
S
X_1 C_1
X_2 C_2
\vdots
X_Q C_Q

出力

Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。


入力例 1

7 4
ABCDABC
4 B
3 A
5 C
4 G

出力例 1

2
1
1
0

各クエリを処理した後の S は次のようになります。

  • 1 つ目のクエリを処理後: S= ABCBABC となる。この中に ABC は部分文字列として 2 回登場する。
  • 2 つ目のクエリを処理後: S= ABABABC となる。この中に ABC は部分文字列として 1 回登場する。
  • 3 つ目のクエリを処理後: S= ABABCBC となる。この中に ABC は部分文字列として 1 回登場する。
  • 4 つ目のクエリを処理後: S= ABAGCBC となる。この中に ABC は部分文字列として 0 回登場する。

入力例 2

3 3
ABC
1 A
2 B
3 C

出力例 2

1
1
1

クエリの処理前と処理後で S が変わらない場合もあります。


入力例 3

15 10
BBCCBCACCBACACA
9 C
11 B
5 B
11 B
4 A
8 C
8 B
5 B
7 B
14 B

出力例 3

0
0
0
0
1
1
2
2
1
1

Score : 350 points

Problem Statement

You are given a string S of length N. You are also given Q queries, which you should process in order.

The i-th query is as follows:

  • Given an integer X_i and a character C_i, replace the X_i-th character of S with C_i. Then, print the number of times the string ABC appears as a substring in S.

Here, a substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

  • 3 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • S is a string of length N consisting of uppercase English letters.
  • 1 \le X_i \le N
  • C_i is an uppercase English letter.

Input

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

N Q
S
X_1 C_1
X_2 C_2
\vdots
X_Q C_Q

Output

Print Q lines. The i-th line (1 \le i \le Q) should contain the answer to the i-th query.


Sample Input 1

7 4
ABCDABC
4 B
3 A
5 C
4 G

Sample Output 1

2
1
1
0

After processing each query, S becomes as follows.

  • After the first query: S= ABCBABC. In this string, ABC appears twice as a substring.
  • After the second query: S= ABABABC. In this string, ABC appears once as a substring.
  • After the third query: S= ABABCBC. In this string, ABC appears once as a substring.
  • After the fourth query: S= ABAGCBC. In this string, ABC appears zero times as a substring.

Sample Input 2

3 3
ABC
1 A
2 B
3 C

Sample Output 2

1
1
1

There are cases where S does not change through processing a query.


Sample Input 3

15 10
BBCCBCACCBACACA
9 C
11 B
5 B
11 B
4 A
8 C
8 B
5 B
7 B
14 B

Sample Output 3

0
0
0
0
1
1
2
2
1
1
F - Remove and Append

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) が与えられます。
q=1,2,\dots,Q の順に、以下の操作を行います。

  • P から値が a_q である要素を削除し、それを P の末尾に追加する。

Q 個の操作を行った後の P の各要素の値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • (P_1,P_2,\dots,P_N) は (1,2,\dots,N) の順列
  • 1 \leq a_q \leq N
  • 入力される値はすべて整数

入力

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

N Q  
P_1 P_2 \ldots P_N
a_1  
a_2  
\vdots  
a_Q  

出力

Q 個の操作を行った後の P_1,P_2,\dots,P_N をこの順に空白区切りで 1 行で出力せよ。


入力例 1

4 2
2 4 3 1
3
2

出力例 1

4 1 3 2

最初、P=(2,4,3,1) です。
1 回目の操作を行うと、P=(2,4,1,3) となります。
2 回目の操作を行うと、P=(4,1,3,2) となります。


入力例 2

3 3
1 2 3
1
1
1

出力例 2

2 3 1

入力例 3

2 5
2 1
1
1
2
2
1

出力例 3

2 1

Score : 300 points

Problem Statement

You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N).
For q=1,2,\dots,Q in this order, perform the following operation.

  • Remove the element with value a_q from P, and append it to the end of P.

Find the value of each element of P after performing the Q operations.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • (P_1,P_2,\dots,P_N) is a permutation of (1,2,\dots,N).
  • 1 \leq a_q \leq N
  • All input values are integers.

Input

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

N Q  
P_1 P_2 \ldots P_N
a_1  
a_2  
\vdots  
a_Q  

Output

Output P_1,P_2,\dots,P_N after performing the Q operations, in this order, separated by spaces, in one line.


Sample Input 1

4 2
2 4 3 1
3
2

Sample Output 1

4 1 3 2

Initially, P=(2,4,3,1).
After the first operation, P=(2,4,1,3).
After the second operation, P=(4,1,3,2).


Sample Input 2

3 3
1 2 3
1
1
1

Sample Output 2

2 3 1

Sample Input 3

2 5
2 1
1
1
2
2
1

Sample Output 3

2 1
G - Doubles

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

N 個のサイコロがあります。 i 番目のサイコロは K_i 個の面をもち、各面にはそれぞれ A_{i,1},A_{i,2},\ldots,A_{i,K_i} が書かれています。このサイコロを振ると、各面がそれぞれ \frac{1}{K_i} の確率で出ます。

あなたは N 個のサイコロから 2 個を選んで振ります。サイコロを適切に選んだときの、2 つのサイコロの出目が等しくなる確率の最大値を求めてください。

制約

  • 2 \leq N \leq 100
  • 1 \leq K_i
  • K_1+K_2+\dots+K_N \leq 10^5
  • 1 \leq A_{i,j} \leq 10^5
  • 入力は全て整数である

入力

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

N
K_1 A_{1,1} A_{1,2} \dots A_{1,K_1}
\vdots
K_N A_{N,1} A_{N,2} \dots A_{N,K_N}

出力

答えを出力せよ。真の解との相対誤差または絶対誤差が 10^{-8} 以下のとき正解とみなされる。


入力例 1

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

出力例 1

0.333333333333333
  • 1 番目のサイコロと 2 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{3} です。
  • 1 番目のサイコロと 3 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{6} です。
  • 2 番目のサイコロと 3 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{6} です。

よって最大値は \frac{1}{3}=0.3333333333\ldots となります。


入力例 2

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

出力例 2

0.666666666666667

Score : 400 points

Problem Statement

There are N dice. The i-th die has K_i faces, with the numbers A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} written on them. When you roll this die, each face appears with probability \frac{1}{K_i}.

You choose two dice from the N dice and roll them. Determine the maximum probability that the two dice show the same number, when the dice are chosen optimally.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq K_i
  • K_1 + K_2 + \dots + K_N \leq 10^5
  • 1 \leq A_{i,j} \leq 10^5
  • All input values are integers.

Input

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

N
K_1 A_{1,1} A_{1,2} \dots A_{1,K_1}
\vdots
K_N A_{N,1} A_{N,2} \dots A_{N,K_N}

Output

Print the answer. Your answer is considered correct if the absolute or relative error from the true solution does not exceed 10^{-8}.


Sample Input 1

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

Sample Output 1

0.333333333333333
  • When choosing the 1st and 2nd dice, the probability that the outcomes are the same is \frac{1}{3}.
  • When choosing the 1st and 3rd dice, the probability is \frac{1}{6}.
  • When choosing the 2nd and 3rd dice, the probability is \frac{1}{6}.

Therefore, the maximum probability is \frac{1}{3} = 0.3333333333\ldots.


Sample Input 2

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

Sample Output 2

0.666666666666667
H - Notebook

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 500 点

問題文

整数列 A とノートがあります。ノートには 10^9 枚のページがあります。

Q 個のクエリが与えられます。各クエリは下記の 4 種類のいずれかです。

ADD x : 整数 x を A の末尾に追加する。
DELETE : A の末尾の要素を削除する。ただし、A が空である場合は何もしない。
SAVE y : ノートの y ページ目に書かれている数列を消し、代わりに現在の A を y ページ目に書き込む。
LOAD z : A をノートの z ページ目に書かれている数列で置き換える。

はじめ、A は空列であり、ノートのすべてのページには空列の情報が書かれています。 その初期状態から、Q 個のクエリを与えられる順に実行し、各クエリの実行直後における A の末尾の要素を出力してください。

なお、入出力の量が多くなる場合があるので、高速な方法で入出力を行うことを推奨します。

制約

  • 1 \leq Q \leq 5 \times 10^5
  • 1 \leq x, y, z \leq 10^9
  • Q, x, y, z は整数
  • 与えられるクエリは問題文中の 4 種類のいずれか

入力

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

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

出力

下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までのクエリを実行した直後の A の末尾の要素 X_i を( A が空の場合は X_i := -1 とする)出力せよ。

X_1 X_2 \ldots X_Q

入力例 1

11
ADD 3
SAVE 1
ADD 4
SAVE 2
LOAD 1
DELETE
DELETE
LOAD 2
SAVE 1
LOAD 3
LOAD 1

出力例 1

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

はじめ、A は空列、すなわち A = () であり、ノートのすべてのページには空列の情報が書かれています。

  • 1 番目のクエリによって、 A の末尾に 3 が追加され、A = (3) となります。
  • 2 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3) になります。A は変わらず A = (3) です。
  • 3 番目のクエリによって、 A の末尾に 4 が追加され、A = (3, 4) となります。
  • 4 番目のクエリによって、ノートの 2 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
  • 5 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3) で置き換えられ、A = (3) となります。
  • 6 番目のクエリによって、 A の末尾の要素が削除され、A = () となります。
  • 7 番目のクエリでは、A がすでに空であるので何もしません。A は変わらず A = () です。
  • 8 番目のクエリによって、 A がノートの 2 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
  • 9 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
  • 10 番目のクエリによって、 A がノートの 3 ページ目に書かれた数列 () で置き換えられ、A = () となります。
  • 11 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。

入力例 2

21
ADD 4
ADD 3
DELETE
ADD 10
LOAD 7
SAVE 5
SAVE 5
ADD 4
ADD 4
ADD 5
SAVE 5
ADD 2
DELETE
ADD 1
SAVE 5
ADD 7
ADD 8
DELETE
ADD 4
DELETE
LOAD 5

出力例 2

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

Score : 500 points

Problem Statement

We have an integer sequence A and a notebook. The notebook has 10^9 pages.

You are given Q queries. Each query is of one of the following four kinds:

ADD x: append an integer x to the tail of A.
DELETE: remove the last term of A if A is not empty; do nothing otherwise.
SAVE y: erase the sequence recorded on the y-th page of the notebook, and record the current A onto the y-th page.
LOAD z: replace A with the sequence recorded on the z-th page of the notebook.

Initially, A is an empty sequence, and an empty sequence is recorded on each page of the notebook. Process Q queries successively in the given order and print the last term of A after processing each query.

The use of fast input and output methods is recommended because of potentially large input and output.

Constraints

  • 1 \leq Q \leq 5 \times 10^5
  • 1 \leq x, y, z \leq 10^9
  • Q, x, y, and z are integers.
  • Each of the given queries is of one of the four kinds in the Problem Statement.

Input

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

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

Output

For each i = 1, 2, \ldots, Q, let X_i be the last element of A after processing up to the i-th query, or let X_i := -1 if A is empty, and print them in the following format:

X_1 X_2 \ldots X_Q

Sample Input 1

11
ADD 3
SAVE 1
ADD 4
SAVE 2
LOAD 1
DELETE
DELETE
LOAD 2
SAVE 1
LOAD 3
LOAD 1

Sample Output 1

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

Initially, A is an empty sequence, so A = (), and an empty sequence is recorded on each page of the notebook.

  • By the 1-st query, 3 is appended to the tail of A, resulting in A = (3).
  • By the 2-nd query, the sequence recorded on the 1-st page of the notebook becomes (3). It remains that A = (3).
  • By the 3-rd query, 4 is appended to the tail of A, resulting in A = (3, 4).
  • By the 4-th query, the sequence recorded on the 2-nd page of the notebook becomes (3, 4). It remains that A = (3, 4).
  • By the 5-th query, A is replaced by (3), which is recorded on the 1-st page of the notebook, resulting in A = (3).
  • By the 6-th query, the last term of A is removed, resulting in A = ().
  • By the 7-th query, nothing happens because A is already empty. It remains that A = ().
  • By the 8-th query, A is replaced by (3,4), which is recorded on the 2-nd page of the notebook, resulting in A = (3, 4).
  • By the 9-th query, the sequence recorded on the 1-st page of the notebook becomes (3, 4). It remains that A = (3, 4).
  • By the 10-th query, A is replaced by (), which is recorded on the 3-rd page of the notebook, resulting in A = ().
  • By the 11-th query, A is replaced by (3, 4), which is recorded on the 1-st page of the notebook, resulting in A = (3, 4).

Sample Input 2

21
ADD 4
ADD 3
DELETE
ADD 10
LOAD 7
SAVE 5
SAVE 5
ADD 4
ADD 4
ADD 5
SAVE 5
ADD 2
DELETE
ADD 1
SAVE 5
ADD 7
ADD 8
DELETE
ADD 4
DELETE
LOAD 5

Sample Output 2

4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
I - Christmas Present 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 550 点

問題文

xy 平面として表される町があり、サンタさんと、1 から N までの番号が付けられた N 人の子供が住んでいます。 サンタさんの家の座標は (S_X,S_Y) であり、子供 i\ (1\leq i\leq N) の家の座標は (X_i,Y_i) です。

サンタさんは、N 人の子供たちに番号順にプレゼントを 1 個ずつ配りたいです。 子供 i にプレゼントを配るためには、プレゼントを 1 個以上持った状態で子供 i の家に行く必要があります。 しかし、サンタさんは同時に K 個までしかプレゼントを持つことができず、プレゼントを補充するためには一旦自分の家に戻る必要があります(サンタさんの家には十分な数のプレゼントがあります)。

サンタさんが自分の家を出発し、N 人の子供たち全員にプレゼントを配って、自分の家まで戻ってくるために移動する距離の最小値を求めてください。

制約

  • 1\leq K\leq N \leq 2\times 10^5
  • -10^9\leq S_X,S_Y,X_i,Y_i \leq 10^9
  • (S_X,S_Y)\neq (X_i,Y_i)
  • (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
  • 入力は全て整数

入力

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

N K
S_X S_Y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

サンタさんが移動する距離の最小値を出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{−6} 以下のとき正解と判定される。


入力例 1

3 2
1 1
3 1
1 2
3 2

出力例 1

9.236067977499790

上図において、赤い丸はサンタさんの家、中に数字が書かれた丸はその番号の子供の家を表しています。

サンタさんが以下のように行動することを考えます。

  1. プレゼントを 2 個持ってサンタさんの家を出る。
  2. 子供 1 の家に行ってプレゼントを 1 個配る。
  3. サンタさんの家に戻ってプレゼントを 1 個補充する。
  4. 子供 2 の家に行ってプレゼントを 1 個配る。
  5. 子供 3 の家に行ってプレゼントを 1 個配る。
  6. サンタさんの家に戻る。

このとき、サンタさんが移動する距離は 2+2+1+2+\sqrt{5}=7+\sqrt{5}=9.236\dots となり、これが最小です。


入力例 2

2 1
0 1
-1 1
1 1

出力例 2

4.000000000000000

入力例 3

8 3
735867677 193944314
586260100 -192321079
95834122 802780784
418379342 -790013317
-445130206 189801569
-354684803 -49687658
-204491568 -840249197
853829789 470958158
-751917965 762048217

出力例 3

11347715738.116592407226562

Score : 550 points

Problem Statement

There is a town represented as an xy-plane, where Santa lives, along with N children numbered 1 to N. Santa's house is at coordinates (S_X,S_Y), and the house of child i\ (1\leq i\leq N) is at (X_i,Y_i).

Santa wants to deliver one present to each of the N children in numerical order. To deliver a present to child i, Santa must visit the house of child i with at least one present in hand. However, Santa can only carry up to K presents at a time, and he must return to his own house to replenish presents (there are enough presents at Santa's house).

Find the minimum distance Santa must travel to leave his house, deliver presents to all N children, and return to his house.

Constraints

  • 1\leq K\leq N \leq 2\times 10^5
  • -10^9\leq S_X,S_Y,X_i,Y_i \leq 10^9
  • (S_X,S_Y)\neq (X_i,Y_i)
  • (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
  • All input values are integers.

Input

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

N K
S_X S_Y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the minimum distance Santa must travel. The output will be considered correct if the absolute or relative error from the true value is at most 10^{−6}.


Sample Input 1

3 2
1 1
3 1
1 2
3 2

Sample Output 1

9.236067977499790

In the figure above, the red circle represents Santa's house, and the circles with numbers represent the houses of the children with those numbers.

Consider Santa acting as follows:

  1. Leave his house with two presents.
  2. Go to child 1's house and deliver a present.
  3. Return to his house and replenish one present.
  4. Go to child 2's house and deliver a present.
  5. Go to child 3's house and deliver a present.
  6. Return to his house.

In this case, Santa travels the distance of 2+2+1+2+\sqrt{5}=7+\sqrt{5}=9.236\dots, which is the minimum.


Sample Input 2

2 1
0 1
-1 1
1 1

Sample Output 2

4.000000000000000

Sample Input 3

8 3
735867677 193944314
586260100 -192321079
95834122 802780784
418379342 -790013317
-445130206 189801569
-354684803 -49687658
-204491568 -840249197
853829789 470958158
-751917965 762048217

Sample Output 3

11347715738.116592407226562