A - o-padding

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

整数 N および、英小文字からなる長さが N 未満 の文字列 S が与えられます。

長さが N になるまで S の先頭に英小文字 o を追加し続けることで得られる文字列を出力してください。

制約

  • 2\leq N \leq 100
  • N は整数
  • S は長さ 1 以上 N 未満の英小文字からなる文字列

入力

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

N
S

出力

答えを出力せよ。


入力例 1

5
abc

出力例 1

ooabc

N=5、S の長さは 3 であるため、S の先頭に o を 5-3=2 個追加した文字列が答えとなります。


入力例 2

2
o

出力例 2

oo

入力例 3

12
vgxgpuam

出力例 3

oooovgxgpuam

Score : 100 points

Problem Statement

You are given an integer N and a string S consisting of lowercase English letters with length less than N.

Print the string obtained by repeatedly adding the lowercase English letter o to the beginning of S until its length becomes N.

Constraints

  • 2\leq N \leq 100
  • N is an integer.
  • S is a string consisting of lowercase English letters with length between 1 and N - 1, inclusive.

Input

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

N
S

Output

Print the answer.


Sample Input 1

5
abc

Sample Output 1

ooabc

Since N=5 and the length of S is 3, the answer is the string obtained by adding 5-3=2 os to the beginning of S.


Sample Input 2

2
o

Sample Output 2

oo

Sample Input 3

12
vgxgpuam

Sample Output 3

oooovgxgpuam
B - First Contest of the Year

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

ある星では 1 年が D 日あり、7 日ごとにコンテストが開催されています。コンテストが日をまたいで開催されることはありません。

ある年の最初のコンテストは 1 年のうち F 日目に開催されました。その次の年の最初のコンテストは 1 年のうち何日目に開催されますか?

なお、この問題の制約のもとでは、次の年にコンテストが 1 回以上開催されることが保証されます。

制約

  • 10 \leq D \leq 366
  • 1 \leq F \leq 7
  • 入力される値はすべて整数

入力

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

D F

出力

答えを出力せよ。


入力例 1

365 4

出力例 1

3

ある年の最初のコンテストの開催が 1 年のうち 4 日目だったとき、次の年の最初のコンテストの開催は 1 年のうち 3 日目になります。


入力例 2

10 5

出力例 2

2

Score : 100 points

Problem Statement

On a certain planet, a year has D days, and contests are held every 7 days. Contests never span across days.

The first contest of a certain year was held on the F-th day of the year. On what day of the next year will the first contest of the next year be held?

The constraints of this problem guarantee that at least one contest will be held in the next year.

Constraints

  • 10 \leq D \leq 366
  • 1 \leq F \leq 7
  • All input values are integers.

Input

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

D F

Output

Print the integer N such that the next contest is held on the N-th day of the year.


Sample Input 1

365 4

Sample Output 1

3

If the first contest of a certain year was held on the 4th day of the year, the first contest of the next year will be held on the 3rd day of the year.


Sample Input 2

10 5

Sample Output 2

2
C - Practical Computing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

以下のような N 個の整数列 A_0,\ldots,A_{N-1} を求めてください。

  • 各 i (0\leq i \leq N-1) について、A_i の長さは i+1 である。
  • 各 i,j (0\leq i \leq N-1, 0 \leq j \leq i) について、A_i の j+1 番目の値 a_{i,j} は次のように定められる。

    • j=0 または j=i の時、a_{i,j}=1
    • それ以外の時、a_{i,j} = a_{i-1,j-1} + a_{i-1,j}

制約

  • 1 \leq N \leq 30
  • N は整数

入力

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

N

出力

N 行出力せよ。 i 行目には A_{i-1} の値を順に空白区切りで出力せよ。


入力例 1

3

出力例 1

1
1 1
1 2 1

入力例 2

10

出力例 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Score : 200 points

Problem Statement

Find the N integer sequences A_0,\ldots,A_{N-1} defined as follows.

  • For each i (0\leq i \leq N-1), the length of A_i is i+1.
  • For each i and j (0\leq i \leq N-1, 0 \leq j \leq i), the (j+1)-th term of A_i, denoted by a_{i,j}, is defined as follows.
    • a_{i,j}=1, if j=0 or j=i.
    • a_{i,j} = a_{i-1,j-1} + a_{i-1,j}, otherwise.

Constraints

  • 1 \leq N \leq 30
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the terms of A_{i-1} separated by spaces.


Sample Input 1

3

Sample Output 1

1
1 1
1 2 1

Sample Input 2

10

Sample Output 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
D - Dentist Aoki

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君には、穴 1,2,\dots,N に 1 本ずつ、全部で N 本の歯が生えています。
歯医者の青木君は、これらの歯と穴に対して、 Q 回の治療を行います。
i 回目の治療では、穴 T_i を治療します。治療内容は次の通りです。

  • 穴 T_i に歯が生えている場合、穴 T_i から歯を抜く。
  • そうでない ( 穴 T_i に歯が生えていない) 場合、穴 T_i に歯を生やす。

全ての治療が終わった後、高橋君に生えている歯の本数は何本ですか?

制約

  • 入力は全て整数
  • 1 \le N,Q \le 1000
  • 1 \le T_i \le N

入力

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

N Q
T_1 T_2 \dots T_Q

出力

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


入力例 1

30 6
2 9 18 27 18 9

出力例 1

28

高橋君には最初 30 本の歯が生えており、青木君は 6 回の治療を行います。

  • 1 回目の治療では穴 2 を治療します。 穴 2 に歯が生えているため、歯を抜きます。
  • 2 回目の治療では穴 9 を治療します。 穴 9 に歯が生えているため、歯を抜きます。
  • 3 回目の治療では穴 18 を治療します。 穴 18 に歯が生えているため、歯を抜きます。
  • 4 回目の治療では穴 27 を治療します。 穴 27 に歯が生えているため、歯を抜きます。
  • 5 回目の治療では穴 18 を治療します。 穴 18 に歯が生えていないため、歯を生やします。
  • 6 回目の治療では穴 9 を治療します。 穴 9 に歯が生えていないため、歯を生やします。

最終的な歯の本数は 28 本です。


入力例 2

1 7
1 1 1 1 1 1 1

出力例 2

0

入力例 3

9 20
9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8

出力例 3

5

Score: 200 points

Problem Statement

Takahashi has N teeth, one in each of the holes numbered 1, 2, \dots, N.
Dentist Aoki will perform Q treatments on these teeth and holes.
In the i-th treatment, hole T_i is treated as follows:

  • If there is a tooth in hole T_i, remove the tooth from hole T_i.
  • If there is no tooth in hole T_i (i.e., the hole is empty), grow a tooth in hole T_i.

After all treatments are completed, how many teeth does Takahashi have?

Constraints

  • All input values are integers.
  • 1 \le N, Q \le 1000
  • 1 \le T_i \le N

Input

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

N Q
T_1 T_2 \dots T_Q

Output

Print the number of teeth as an integer.


Sample Input 1

30 6
2 9 18 27 18 9

Sample Output 1

28

Initially, Takahashi has 30 teeth, and Aoki performs six treatments.

  • In the first treatment, hole 2 is treated. There is a tooth in hole 2, so it is removed.
  • In the second treatment, hole 9 is treated. There is a tooth in hole 9, so it is removed.
  • In the third treatment, hole 18 is treated. There is a tooth in hole 18, so it is removed.
  • In the fourth treatment, hole 27 is treated. There is a tooth in hole 27, so it is removed.
  • In the fifth treatment, hole 18 is treated. There is no tooth in hole 18, so a tooth is grown.
  • In the sixth treatment, hole 9 is treated. There is no tooth in hole 9, so a tooth is grown.

The final count of teeth is 28.


Sample Input 2

1 7
1 1 1 1 1 1 1

Sample Output 2

0

Sample Input 3

9 20
9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8

Sample Output 3

5
E - Bipartize

Time Limit: 2 sec / Memory Limit: 1024 MiB



配点 : 350 点

問題文

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

あなたは、次の操作を 0 回以上行います。

  • まだ削除されていない辺をひとつ選び、削除する。

あなたの目的はグラフを二部グラフにすることです。 操作を行ったあとのグラフを二部グラフにするために、最低でも何回操作を行う必要があるか求めてください。

グラフが単純であるとは

グラフが単純であるとは、自己ループ(u _ i=v _ i となる辺)や多重辺(u _ i=u _ j かつ v _ i=v _ j となる辺のペア)が存在しないことをいいます。

二部グラフとは

二部グラフとは、頂点をそれぞれ黒か白のどちらか一色で塗り、次の条件を満たすことができるグラフのことをいいます。

  • どの辺についても、その辺が結んでいるふたつの頂点に塗られた色は異なる。

制約

  • 2\le N\le10
  • 1\le M\le\dfrac{N(N-1)}2
  • 1\le u _ i\lt v _ i\le N\ (1\le i\le M)
  • 与えられるグラフは単純
  • 入力はすべて整数

入力

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

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

出力

操作を行ったあとのグラフを二部グラフにするために、行う必要がある操作の回数を出力せよ。


入力例 1

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

出力例 1

2

例えば、頂点 1 と頂点 3 を結ぶ辺、頂点 3 と頂点 5 を結ぶ辺の 2 つを削除することでグラフを二部グラフにすることができます。

操作を 1 回以下行うことでグラフを二部グラフにすることはできないため、2 を出力してください。


入力例 2

2 1
1 2

出力例 2

0

グラフははじめから二部グラフです。 よって、行う必要がある操作の回数は 0 回です。


入力例 3

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

出力例 3

5

Score : 350 points

Problem Statement

There is a simple undirected graph with N vertices and M edges. The graph consists of vertex 1, vertex 2,\ldots, vertex N, and the i-th edge (1\le i\le M) connects vertices u _ i and v _ i.

You will perform the following operation zero or more times:

  • Choose one edge that has not been deleted yet, and delete it.

Your goal is to make the graph bipartite. Find the minimum number of operations needed to make the graph after the operations bipartite.

What it means for a graph to be simple?

A graph is simple if and only if it has no self-loops (edges where u _ i=v _ i) or multi-edges (pairs of edges where u _ i=u _ j and v _ i=v _ j).

What is a bipartite graph?

A bipartite graph is a graph where it is possible to color each vertex black or white satisfying the following condition:

  • For every edge, the two vertices connected by that edge have different colors.

Constraints

  • 2\le N\le10
  • 1\le M\le\dfrac{N(N-1)}2
  • 1\le u _ i\lt v _ i\le N\ (1\le i\le M)
  • The given graph is simple.
  • 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 the number of operations that need to be performed to make the graph bipartite.


Sample Input 1

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

Sample Output 1

2

You can make the graph bipartite by deleting two edges: for example, the edge connecting vertices 1 and 3, and the edge connecting vertices 3 and 5.

It is impossible to make the graph bipartite by performing one or less operations, so print 2.


Sample Input 2

2 1
1 2

Sample Output 2

0

The graph is bipartite from the beginning. Thus, the number of operations that need to be performed is 0.


Sample Input 3

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

Sample Output 3

5
F - Find it!

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

N 頂点 N 辺の有向グラフがあります。
i 番目の辺は頂点 i から 頂点 A_i に伸びます。 ( i \neq A_i であることは制約より保証されます )
同一頂点を複数回含まない有向閉路をひとつ求めてください。
なお、この問題の制約下で答えが存在することが示せます。

注釈

この問題では、有向閉路とは以下の条件を全て満たす頂点の列 B=(B_1,B_2,\dots,B_M) であるとします。

  • M \ge 2
  • B_i から B_{i+1} に辺が伸びている。 ( 1 \le i \le M-1 )
  • B_M から B_1 に辺が伸びている。
  • i \neq j ならば B_i \neq B_j

制約

  • 入力は全て整数
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

入力

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

N
A_1 A_2 \dots A_N

出力

以下の形式で出力せよ。

M
B_1 B_2 \dots B_M

M は出力する有向閉路の頂点数であり、 B_i は有向閉路の i 番目の頂点である。
出力は以下の条件を満たす必要がある。

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

答えとして考えられるものが複数ある場合、どれを出力しても正解となる。


入力例 1

7
6 7 2 1 3 4 5

出力例 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。

他の正解となる出力の例は以下の通りです。

4
2 7 5 3
3
4 1 6

グラフが連結であるとは限らないことに注意してください。


入力例 2

2
2 1

出力例 2

2
1 2

1 \rightarrow 2 と 2 \rightarrow 1 の辺が双方存在するケースです。
この場合、 1 \rightarrow 2 \rightarrow 1 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。
図中 1 \leftrightarrow 2 で 1 \rightarrow 2 と 2 \rightarrow 1 の辺が双方あることを表現しています。


入力例 3

8
3 7 4 7 3 3 8 2

出力例 3

3
2 7 8

この入力に対応するグラフは以下の通りです。

Score : 350 points

Problem Statement

There is a directed graph with N vertices and N edges.
The i-th edge goes from vertex i to vertex A_i. (The constraints guarantee that i \neq A_i.)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.

Notes

The sequence of vertices B = (B_1, B_2, \dots, B_M) is called a directed cycle when all of the following conditions are satisfied:

  • M \geq 2
  • The edge from vertex B_i to vertex B_{i+1} exists. (1 \leq i \leq M-1)
  • The edge from vertex B_M to vertex B_1 exists.
  • If i \neq j, then B_i \neq B_j.

Constraints

  • All input values are integers.
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

Input

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

N
A_1 A_2 \dots A_N

Output

Print a solution in the following format:

M
B_1 B_2 \dots B_M

M is the number of vertices, and B_i is the i-th vertex in the directed cycle.
The following conditions must be satisfied:

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

If multiple solutions exist, any of them will be accepted.


Sample Input 1

7
6 7 2 1 3 4 5

Sample Output 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 is indeed a directed cycle.

Here is the graph corresponding to this input:

Here are other acceptable outputs:

4
2 7 5 3
3
4 1 6

Note that the graph may not be connected.


Sample Input 2

2
2 1

Sample Output 2

2
1 2

This case contains both of the edges 1 \rightarrow 2 and 2 \rightarrow 1.
In this case, 1 \rightarrow 2 \rightarrow 1 is indeed a directed cycle.

Here is the graph corresponding to this input, where 1 \leftrightarrow 2 represents the existence of both 1 \rightarrow 2 and 2 \rightarrow 1:


Sample Input 3

8
3 7 4 7 3 3 8 2

Sample Output 3

3
2 7 8

Here is the graph corresponding to this input:

G - AtCoder Janken 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋くんと青木くんが N 回のじゃんけんを行いました。

青木くんが出した手は R, P, S からなる長さ N の文字列 S で表されます。 青木くんが i 回目 (1\leq i\leq N) のじゃんけんに出した手は、S の i 文字目が R のときグー、P のときパー、S のときチョキです。

高橋くんが出した手について、次の条件を満たすことがわかっています。

  • 高橋くんは青木くんに 1 度も負けなかった。
  • i=1,2,\ldots,N-1 について、高橋くんが i 回目のじゃんけんに出した手と i+1 回目のじゃんけんに出した手は異なる。

高橋くんが勝った回数としてありえる最大値を求めてください。

ここで、条件を満たすような高橋くんの手が存在することが証明できます。

制約

  • 1\leq N\leq2\times10 ^ 5
  • S は R, P, S からなる長さ N の文字列
  • N は整数

入力

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

N
S

出力

答えを出力せよ。


入力例 1

6
PRSSRS

出力例 1

5

青木くんは、6 回のじゃんけんでそれぞれパー、グー、チョキ、チョキ、グー、チョキを出しました。

高橋くんは、それぞれチョキ、パー、グー、チョキ、パー、グーを出すことで、1,2,3,5,6 回目のじゃんけんで勝つことができます。

条件を満たす高橋くんの手のうち 6 回のじゃんけんすべてで勝つものは存在しないため、5 を出力してください。


入力例 2

10
SSSSSSSSSS

出力例 2

5

入力例 3

24
SPRPSRRRRRPPRPRPSSRSPRSS

出力例 3

18

Score : 400 points

Problem Statement

Takahashi and Aoki played rock-paper-scissors N times. [Note: In this game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.]

Aoki's moves are represented by a string S of length N consisting of the characters R, P, and S. The i-th character of S indicates Aoki's move in the i-th game: R for Rock, P for Paper, and S for Scissors.

Takahashi's moves satisfy the following conditions:

  • Takahashi never lost to Aoki.
  • For i=1,2,\ldots,N-1, Takahashi's move in the i-th game is different from his move in the (i+1)-th game.

Determine the maximum number of games Takahashi could have won.

It is guaranteed that there exists a sequence of moves for Takahashi that satisfies these conditions.

Constraints

  • 1\leq N\leq2\times10 ^ 5
  • S is a string of length N consisting of R, P, and S.
  • N is an integer.

Input

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

N
S

Output

Print the maximum number of games Takahashi could have won.


Sample Input 1

6
PRSSRS

Sample Output 1

5

In the six games of rock-paper-scissors, Aoki played Paper, Rock, Scissors, Scissors, Rock, and Scissors.

Takahashi can play Scissors, Paper, Rock, Scissors, Paper, and Rock to win the 1st, 2nd, 3rd, 5th, and 6th games.

There is no sequence of moves for Takahashi that satisfies the conditions and wins all six games, so print 5.


Sample Input 2

10
SSSSSSSSSS

Sample Output 2

5

Sample Input 3

24
SPRPSRRRRRPPRPRPSSRSPRSS

Sample Output 3

18
H - K-Divisible Subarrays

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

非負整数からなる長さ N の列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。

非負整数列からなる長さ 1 以上の列 S=(S _ 1,S _ 2,\ldots,S _ k) のスコアを、列 S _ i\ (1\le i\le k) のうち要素の総和が K で割り切れるものの個数として定めます。

A を 1 つ以上の連続する部分列に分割して得られる非負整数列の列に対するスコアの最大値を求めてください。 ここで、長さ N の列 A を 1 つ以上の連続する部分列に分割するとは、整数からなる長さ 1 以上の列 l=(l _ 1,l _ 2,\ldots,l _ k)\ (1=l _ 1\lt l _ 2\lt\cdots\lt l _ k\le N) をとり、以下のような k 個の列を作ることをいいます。

  • (A _ {l _ i},A _ {l _ i+1},\ldots,A _ {l _ {i+1}-1})\ (1\le i\le k) (ここで、l _ {k+1}=N+1 とする。)

制約

  • 1\le N\le2\times10 ^ 5
  • 1\le K\le10 ^ 9
  • 0\le A _ i\lt K
  • 入力はすべて整数

入力

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

N K
A _ 1 A _ 2 \ldots A _ N

出力

答えを出力せよ。


入力例 1

6 10
6 8 2 2 6 4

出力例 1

2

例えば、A を (6),(8,2),(2),(6,4) のように 4 つの連続する部分列に分割することができます。 2 番目と 4 番目の列の要素の総和が 10 の倍数になるので、((6),(8,2),(2),(6,4)) のスコアは 2 です。

スコアが 3 以上になるように A を分割することはできないので、2 を出力してください。


入力例 2

8 1
0 0 0 0 0 0 0 0

出力例 2

8

入力例 3

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

出力例 3

8

Score : 450 points

Problem Statement

You are given a sequence of length N consisting of non-negative integers: A=(A _ 1,A _ 2,\ldots,A _ N) .

Define the score of a sequence of length 1 or more consisting of sequences of non-negative integers, S=(S _ 1,S _ 2,\ldots,S _ k), as the number of sequences S _ i\ (1\le i\le k) whose sum of elements is divisible by K.

Find the maximum possible score of a sequence of non-negative integer sequences obtained by dividing A into one or more contiguous subsequences. Here, dividing a sequence A of length N into one or more contiguous subsequences means choosing a sequence of integers l=(l _ 1,l _ 2,\ldots,l _ k)\ (1=l _ 1\lt l _ 2\lt\cdots\lt l _ k\le N) of length 1 or more, and forming the following k sequences.

  • (A _ {l _ i},A _ {l _ i+1},\ldots,A _ {l _ {i+1}-1})\ (1\le i\le k) (Here, let l _ {k+1}=N+1.)

Constraints

  • 1\le N\le2\times10 ^ 5
  • 1\le K\le10 ^ 9
  • 0\le A _ i\lt K
  • All input values are integers.

Input

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

N K
A _ 1 A _ 2 \ldots A _ N

Output

Output the answer.


Sample Input 1

6 10
6 8 2 2 6 4

Sample Output 1

2

For example, A can be divided into four contiguous subsequences as (6),(8,2),(2),(6,4). The sums of the elements of the second and fourth sequences are multiples of 10, so the score of ((6),(8,2),(2),(6,4)) is 2.

A cannot be divided so that the score is 3 or more, so output 2.


Sample Input 2

8 1
0 0 0 0 0 0 0 0

Sample Output 2

8

Sample Input 3

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

Sample Output 3

8
I - Shortcuts

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

座標平面上でチェックポイント 1,2,\dots,N をこの順に通るレースが行われます。
チェックポイント i の座標は (X_i,Y_i) であり、すべてのチェックポイントの座標は異なります。

チェックポイント 1,N 以外のチェックポイントは、通過を省略することもできます。
ただし、通らなかったチェックポイントの個数を C として、以下の通りペナルティが課せられます。

  • C>0 なら \displaystyle 2^{C−1}
  • C=0 なら 0

チェックポイント 1 からチェックポイント N までの総移動距離(ユークリッド距離)とペナルティの和を s とします。
このとき、 s として達成可能な最小の値を求めてください。

制約

  • 入力は全て整数
  • 2 \le N \le 10^4
  • 0 \le X_i,Y_i \le 10^4
  • i \neq j ならば (X_i,Y_i) \neq (X_j,Y_j)

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

答えを出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{−5} 以下のとき正解と判定される。


入力例 1

6
0 0
1 1
2 0
0 1
1 0
2 1

出力例 1

5.82842712474619009753

チェックポイント 1,2,5,6 を通過し、 3,4 の通過を省略することを考えます。

  • チェックポイント 1 \rightarrow 2 に移動する。 2 点間の距離は \sqrt{2} である。
  • チェックポイント 2 \rightarrow 5 に移動する。 2 点間の距離は 1 である。
  • チェックポイント 5 \rightarrow 6 に移動する。 2 点間の距離は \sqrt{2} である。
  • 通らなかったチェックポイントは 2 つであり、このとき科せられるペナルティは 2 である。

以上のようにして、 s = 3 + 2\sqrt{2} \approx 5.828427 を達成できます。
s をこの値より小さくすることはできません。


入力例 2

10
1 8
3 7
9 4
4 9
6 1
7 5
0 0
1 3
6 8
6 4

出力例 2

24.63441361516795872523

入力例 3

10
34 24
47 60
30 31
12 97
87 93
64 46
82 50
14 7
17 24
3 78

出力例 3

110.61238353245736230207

Score : 500 points

Problem Statement

There is a race through checkpoints 1,2,\dots,N in this order on a coordinate plane.
The coordinates of checkpoint i are (X_i,Y_i), and all checkpoints have different coordinates.

Checkpoints other than checkpoints 1 and N can be skipped.
However, let C be the number of checkpoints skipped, and the following penalty will be imposed:

  • \displaystyle 2^{C−1} if C>0, and
  • 0 if C=0.

Let s be the total distance traveled (Euclidean distance) from checkpoint 1 to checkpoint N plus the penalty.
Find the minimum achievable value as s.

Constraints

  • All input values are integers.
  • 2 \le N \le 10^4
  • 0 \le X_i,Y_i \le 10^4
  • (X_i,Y_i) \neq (X_j,Y_j) if i \neq j.

Input

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the answer. Your output is considered correct if the absolute or relative error from the true value is at most 10^{-5}.


Sample Input 1

6
0 0
1 1
2 0
0 1
1 0
2 1

Sample Output 1

5.82842712474619009753

Consider passing through checkpoints 1,2,5,6 and skip checkpoints 3,4.

  • Move from checkpoint 1 to 2. The distance between them is \sqrt{2}.
  • Move from checkpoint 2 to 5. The distance between them is 1.
  • Move from checkpoint 5 to 6. The distance between them is \sqrt{2}.
  • Two checkpoints are skipped, so the penalty of 2 is imposed.

In this way, you can achieve s = 3 + 2\sqrt{2} \approx 5.828427.
You cannot make s smaller than this value.


Sample Input 2

10
1 8
3 7
9 4
4 9
6 1
7 5
0 0
1 3
6 8
6 4

Sample Output 2

24.63441361516795872523

Sample Input 3

10
34 24
47 60
30 31
12 97
87 93
64 46
82 50
14 7
17 24
3 78

Sample Output 3

110.61238353245736230207