A - Penalty Kick

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

配点 : 100 点

問題文

髙橋君はサッカーの試合で N 回ペナルティキックを蹴ります。

髙橋君は i 回目のペナルティキックでは、i が 3 の倍数の場合は失敗しそれ以外の場合は成功します。

髙橋君がペナルティキックを蹴ったときの結果を出力してください。

制約

  • 1 \leq N \leq 100
  • 入力は全て整数である。

入力

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

N

出力

髙橋君のペナルティキックの結果を表す長さ N の文字列を出力せよ。結果を表す文字列の i (1 \leq i \leq N) 文字目は髙橋君が i 回目のペナルティキックで成功した場合は o 、失敗した場合は x とする。


入力例 1

7

出力例 1

ooxooxo

髙橋君は 3 回目と 6 回目のペナルティキックに失敗するので、3 文字目と 6 文字目が x となります。


入力例 2

9

出力例 2

ooxooxoox

Score: 100 points

Problem Statement

Takahashi will have N penalty kicks in a soccer match.

For the i-th penalty kick, he will fail if i is a multiple of 3, and succeed otherwise.

Print the results of his penalty kicks.

Constraints

  • 1 \leq N \leq 100
  • All inputs are integers.

Input

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

N

Output

Print a string of length N representing the results of Takahashi's penalty kicks. The i-th character (1 \leq i \leq N) should be o if Takahashi succeeds in the i-th penalty kick, and x if he fails.


Sample Input 1

7

Sample Output 1

ooxooxo

Takahashi fails the third and sixth penalty kicks, so the third and sixth characters will be x.


Sample Input 2

9

Sample Output 2

ooxooxoox
B - 3,2,1,GO

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

配点 : 100 点

問題文

正の整数 N が与えられます。

N,N-1,\dots,1 をこの順にカンマ , で区切って出力してください。

制約

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

入力

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

N

出力

N,N-1,\dots,1 をこの順にカンマ区切りで出力せよ。


入力例 1

9

出力例 1

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

入力例 2

5

出力例 2

5,4,3,2,1

入力例 3

1

出力例 3

1

Score : 100 points

Problem Statement

You are given a positive integer N.

Output N, N-1, \dots, 1 in this order, separated by commas ,.

Constraints

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

Input

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

N

Output

Output N, N-1, \dots, 1 in this order, separated by commas.


Sample Input 1

9

Sample Output 1

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

Sample Input 2

5

Sample Output 2

5,4,3,2,1

Sample Input 3

1

Sample Output 3

1
C - Traveling Takahashi Problem

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

配点 : 150 点

問題文

2 次元座標平面の原点に高橋くんがいます。

高橋くんが座標平面上の点 (a,b) から点 (c,d) に移動するには \sqrt{(a-c)^2+(b-d)^2} のコストがかかります。

高橋くんが原点からスタートし N 個の点 (X_1,Y_1),\ldots,(X_N,Y_N) へこの順に移動したのち原点に戻るときの、コストの総和を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • -10^9 \leq X_i,Y_i \leq 10^9
  • 入力は全て整数である

入力

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

N
X_1 Y_1
\vdots
X_N Y_N

出力

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


入力例 1

2
1 2
-1 0

出力例 1

6.06449510224597979401

移動は次の 3 行程からなります。

  • (0,0) から (1,2) に移動する。\sqrt{(0-1)^2+(0-2)^2}=\sqrt{5}=2.236067977... のコストがかかる
  • (1,2) から (-1,0) に移動する。\sqrt{(1-(-1))^2+(2-0)^2}=\sqrt{8}=2.828427124... のコストがかかる
  • (-1,0) から (0,0) に移動する。\sqrt{(-1-0)^2+(0-0)^2}=\sqrt{1}=1 のコストがかかる

コストの総和は 6.064495102... となります。


入力例 2

7
-14142 13562
-17320 50807
-22360 67977
24494 89742
-26457 51311
28284 27124
31622 77660

出力例 2

384694.57587932075868509383

入力例 3

5
-100000 100000
100000 -100000
-100000 100000
100000 -100000
-100000 100000

出力例 3

1414213.56237309504880168872

Score : 150 points

Problem Statement

Takahashi is at the origin on a two-dimensional coordinate plane.

The cost for him to move from point (a, b) to point (c, d) is \sqrt{(a - c)^2 + (b - d)^2}.

Find the total cost when he starts at the origin, visits N points (X_1, Y_1), \ldots, (X_N, Y_N) in this order, and then returns to the origin.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq X_i, Y_i \leq 10^9
  • All input values are integers.

Input

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

N
X_1 Y_1
\vdots
X_N Y_N

Output

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


Sample Input 1

2
1 2
-1 0

Sample Output 1

6.06449510224597979401

The journey consists of the following three steps:

  • Move from (0, 0) to (1, 2). The cost is \sqrt{(0 - 1)^2 + (0 - 2)^2} = \sqrt{5} = 2.236067977....
  • Move from (1, 2) to (-1, 0). The cost is \sqrt{(1 - (-1))^2 + (2 - 0)^2} = \sqrt{8} = 2.828427124....
  • Move from (-1, 0) to (0, 0). The cost is \sqrt{(-1 - 0)^2 + (0 - 0)^2} = \sqrt{1} = 1.

The total cost is 6.064495102....


Sample Input 2

7
-14142 13562
-17320 50807
-22360 67977
24494 89742
-26457 51311
28284 27124
31622 77660

Sample Output 2

384694.57587932075868509383

Sample Input 3

5
-100000 100000
100000 -100000
-100000 100000
100000 -100000
-100000 100000

Sample Output 3

1414213.56237309504880168872
D - Chessboard

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

配点 : 200 点

問題文

チェス盤のどこにコマが置かれているか答えてください。

縦 8 マス、横 8 マスのグリッドがあります。グリッドの各マスには、次のルールで定められる長さ 2 の文字列の名前がついています。

  • 左から 1 列目にあるマスの名前の 1 文字目は a である。同様に、左から 2,3,\ldots,8 列目にあるマスの名前の 1 文字目は b, c, d, e, f, g, h である。
  • 下から 1 行目にあるマスの名前の 2 文字目は 1 である。同様に、下から 2,3,\ldots,8 行目にあるマスの名前の 2 文字目は 2, 3, 4, 5, 6, 7, 8 である。

例えば、グリッドの左下のマスの名前は a1、右下のマスの名前は h1、右上のマスの名前は h8 です。

グリッドの状態を表す長さ 8 の 8 つの文字列 S_1,\ldots,S_8 が与えられます。
S_i の j 文字目は、グリッドの上から i 行目 左から j 列目のマスにコマが置かれているとき *、置かれていないとき . であり、S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在します。
コマが置かれているマスの名前を求めてください。

制約

  • S_i は . および * のみからなる長さ 8 の文字列である
  • S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在する。

入力

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

S_1
S_2
S_3
S_4
S_5
S_6
S_7
S_8

出力

答えを出力せよ。


入力例 1

........
........
........
........
........
........
........
*.......

出力例 1

a1

問題文中で説明したとおり、グリッドの左下のマスの名前は a1 です。


入力例 2

........
........
........
........
........
.*......
........
........

出力例 2

b3

Score : 200 points

Problem Statement

Locate a piece on a chessboard.

We have a grid with 8 rows and 8 columns of squares. Each of the squares has a 2-character name determined as follows.

  • The first character of the name of a square in the 1-st column from the left is a. Similarly, the first character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th column from the left is b, c, d, e, f, g, h, respectively.
  • The second character of the name of a square in the 1-st row from the bottom is 1. Similarly, the second character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th row from the bottom is 2, 3, 4, 5, 6, 7, 8, respectively.

For instance, the bottom-left square is named a1, the bottom-right square is named h1, and the top-right square is named h8.

You are given 8 strings S_1,\ldots,S_8, each of length 8, representing the state of the grid.
The j-th character of S_i is * if the square at the i-th row from the top and j-th column from the left has a piece on it, and . otherwise. The character * occurs exactly once among S_1,\ldots,S_8. Find the name of the square that has a piece on it.

Constraints

  • S_i is a string of length 8 consisting of . and*.
  • The character * occurs exactly once among S_1,\ldots,S_8.

Input

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

S_1
S_2
S_3
S_4
S_5
S_6
S_7
S_8

Output

Print the answer.


Sample Input 1

........
........
........
........
........
........
........
*.......

Sample Output 1

a1

As explained in the problem statement, the bottom-left square is named a1.


Sample Input 2

........
........
........
........
........
.*......
........
........

Sample Output 2

b3
E - Slot Strategy

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

配点 : 300 点

問題文

N 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。 ここで、S_i は 0, 1, \ldots, 9 がちょうど 1 回ずつ現れる長さ 10 の文字列です。

それぞれのリールには対応するボタンがついており、高橋君は各非負整数 t について、 スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す(または何もしない)ことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、 i 番目のリールは S_i の (t\bmod{10})+1 文字目を表示して止まります。
ただし、t\bmod{10} で t を 10 で割ったあまりを表します。

高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。

制約

  • 2\leq N\leq 100
  • N は整数
  • S_i は 0, 1, \ldots, 9 がちょうど 1 回ずつ現れる長さ 10 の文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを出力せよ。


入力例 1

3
1937458062
8124690357
2385760149

出力例 1

6

高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。

  • スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2 の (0\bmod{10})+1=1 文字目である 8 を表示して止まります。
  • スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3 の (2\bmod{10})+1=3 文字目である 8 を表示して止まります。
  • スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1 の (6\bmod{10})+1=7 文字目である 8 を表示して止まります。

5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。


入力例 2

5
0123456789
0123456789
0123456789
0123456789
0123456789

出力例 2

40

全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。

Score : 300 points

Problem Statement

There is a slot machine with N reels.
The placement of symbols on the i-th reel is represented by a string S_i of length 10 containing each of 0, 1, \ldots, 9 exactly once.

Each reel has a corresponding button. For each non-negative integer t, Takahashi can press one of the buttons of his choice (or do nothing) t seconds after the reels start spinning.
If the button for the i-th reel is pressed t seconds after the start of the spin, the i-th reel will stop to display the ((t\bmod{10})+1)-th character of S_i.
Here, t\bmod{10} denotes the remainder when t is divided by 10.

Takahashi wants to stop all reels to make them display the same character.
Find the minimum number of seconds needed to achieve his objective after the start of the spin.

Constraints

  • 2\leq N\leq 100
  • N is an integer.
  • S_i is a string of length 10 containing each of 0, 1, \ldots, 9 exactly once.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print the minimum number of seconds needed to achieve Takahashi's objective after the start of the spin.


Sample Input 1

3
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can make all reels display 8 in 6 seconds after the start of the spin by stopping them as follows.

  • 0 seconds after the start of the spin, press the button for the 2-nd reel, making it stop to display the ((0\bmod{10})+1=1)-st character of S_2, 8.
  • 2 seconds after the start of the spin, press the button for the 3-rd reel, making it stop to display the ((2\bmod{10})+1=3)-rd character of S_3, 8.
  • 6 seconds after the start of the spin, press the button for the 1-st reel, making it stop to display the ((6\bmod{10})+1=7)-th character of S_1, 8.

There is no way to make all reels display the same character in five seconds or less, so the answer is 6.


Sample Input 2

5
0123456789
0123456789
0123456789
0123456789
0123456789

Sample Output 2

40

Note that he must stop all reels to make them display the same character.

F - Striped Horse

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

配点 : 300 点

問題文

りんごさんは馬を縞模様に塗ってシマウマにしようとしています。

1 から N の番号がついた N 個のマスが一列に並んでいます。
最初、全てのマスは白色であり、マス i を黒く塗るためにはコストが C_i かかります。

以下の手順を一度だけ行い、いくつかのマスを黒く塗ることを考えます。

  • 正整数 x を自由に選ぶ。
  • 1 \leq i \leq N を満たす整数 i のうち、 (i+x) を 2W で割った余りが W より小さくなるもの全てに対し、マス i を黒く塗る。

この手順を行うためのコストの合計の最小値を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T \leq 2\times 10^5
  • 1\leq N \leq 2\times 10^5
  • 1\leq W \leq 2\times 10^5
  • 1\leq C_i \leq 10^9
  • T 個のテストケースにおける N の総和は 2\times 10^5 以下
  • T 個のテストケースにおける W の総和は 2\times 10^5 以下
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N W
C_1 \dots C_N

出力

T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。


入力例 1

4
8 2
1 10 10 1 1 10 10 1
8 3
1 10 10 1 1 10 10 1
8 4
1 10 10 1 1 10 10 1
4 100
100000 100000 100000 100000

出力例 1

4
12
22
0

1 番目のテストケースにおいて、x=4 として手順を実行すると、マス 1,4,5,8 が黒く塗られ、コストの合計は 4 となります。 コストの合計を 4 未満にすることはできないため、これが最小値となります。

Score : 300 points

Problem Statement

Ringo-san is trying to paint a horse with stripes to make it a zebra.

There are N squares numbered 1 to N arranged in a line.
Initially, all squares are white, and the cost of painting square i black is C_i.

Consider performing the following procedure once to paint some squares black:

  • Choose a positive integer x freely.
  • Paint square i black for all integers i satisfying 1 \leq i \leq N such that the remainder when (i+x) is divided by 2W is less than W.

Find the minimum total cost to perform this procedure.

You are given T test cases; solve each of them.

Constraints

  • 1\leq T \leq 2\times 10^5
  • 1\leq N \leq 2\times 10^5
  • 1\leq W \leq 2\times 10^5
  • 1\leq C_i \leq 10^9
  • The sum of N over the T test cases is at most 2\times 10^5.
  • The sum of W over the T test cases is at most 2\times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case.

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N W
C_1 \dots C_N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

4
8 2
1 10 10 1 1 10 10 1
8 3
1 10 10 1 1 10 10 1
8 4
1 10 10 1 1 10 10 1
4 100
100000 100000 100000 100000

Sample Output 1

4
12
22
0

In the first test case, if the procedure is executed with x=4, squares 1,4,5,8 are painted black, and the total cost is 4. The total cost cannot be less than 4, so this is the minimum.

G - Takahashi the Wall Breaker

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

配点 : 400 点

問題文

高橋君は魚屋にウナギを買いに行こうとしています。

高橋君の住む町は縦 H マス横 W マスからなるグリッド状の区画に分かれており、 各区画は道か壁かのいずれかです。
以下、上から i 番目 (1\leq i \leq H) かつ左から j 番目 (1\leq j\leq W) の区画を区画 (i,j) で表します。
各区画の情報は H 個の長さ W の文字列 S_1,S_2,\ldots,S_H で与えられ、 具体的には S_i の j 文字目 (1\leq i \leq H,1\leq j\leq W) が . のとき区画 (i,j) が道であることを、 S_i の j 文字目が # のとき区画 (i,j) が壁であることを表します。

高橋君は次の 2 種類の行動を好きな順番で繰り返し行うことができます。

  • 上下左右に隣接する、町の中の区画であって、道であるようなものに移動する。
  • 上下左右の方向を一つ決め、その方向に前蹴りを行う。
    高橋君が前蹴りを行うと、現在いる区画からその方向に 1 つ前の区画および 2 つ前の区画について、その区画が壁ならば道に変えることができる。
    ここで、1 つ前の区画または 2 つ前の区画が町の外である場合でも前蹴りを行うことができるが、町の外が変化することはない。

高橋君は最初、区画 (A,B) におり、区画 (C,D) にある魚屋まで移動したいです。
ここで、高橋君の最初にいる区画および魚屋のある区画は道であることが保証されます。
高橋君が魚屋にたどり着くために必要な 前蹴りの回数 の最小値を求めてください。

制約

  • 1\leq H\leq 1000
  • 1\leq W\leq 1000
  • S_i は ., # のみからなる長さ W の文字列
  • 1\leq A,C\leq H
  • 1\leq B,D\leq W
  • (A,B)\neq (C,D)
  • H,W,A,B,C,D は整数
  • 高橋君の最初にいる区画および魚屋のある区画は道である。

入力

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

H W
S_1
S_2
\vdots
S_H
A B C D

出力

高橋君が魚屋にたどり着くために必要な 前蹴りの回数 の最小値を出力せよ。


入力例 1

10 10
..........
#########.
#.......#.
#..####.#.
##....#.#.
#####.#.#.
.##.#.#.#.
###.#.#.#.
###.#.#.#.
#.....#...
1 1 7 1

出力例 1

1

高橋君は最初、区画 (1,1) にいます。
道である区画への移動を繰り返して区画 (7,4) まで移動することができます。
区画 (7,4) において左方向に対して前蹴りを行うと区画 (7,3) と区画 (7,2) が壁から道に変わります。
その後、道である区画(道に変わった区画を含む)への移動を繰り返して、区画 (7,1) にある魚屋に移動することができます。

このとき、前蹴りを行った回数は 1 回であり、前蹴りを行わずに魚屋へ移動することは不可能なため、1 を出力します。


入力例 2

2 2
.#
#.
1 1 2 2

出力例 2

1

高橋君は最初、区画 (1,1) にいます。
右方向に対して前蹴りを行うと、区画 (1,2) を壁から道に変えることができます。
区画 (1,1) から右方向に 2 つ前のマスは町の外であるため、変化しません。
その後、高橋君は区画 (1,2) へ移動し、区画 (2,2) にある魚屋へ移動することができます。

このとき、前蹴りを行った回数は 1 回であり、前蹴りを行わずに魚屋へ移動することは不可能なため、1 を出力します。


入力例 3

1 3
.#.
1 1 1 3

出力例 3

1

前蹴りを行う際、それにより道に変えられ得る区画の中に魚屋のある区画が含まれていても問題ありません。 具体的には魚屋のある区画はもともと道であるため変化せず、特に前蹴りによって魚屋が壊れることはありません。


入力例 4

20 20
####################
##...##....###...###
#.....#.....#.....##
#..#..#..#..#..#..##
#..#..#....##..#####
#.....#.....#..#####
#.....#..#..#..#..##
#..#..#.....#.....##
#..#..#....###...###
####################
####################
##..#..##...###...##
##..#..#.....#.....#
##..#..#..#..#..#..#
##..#..#..#..#..#..#
##.....#..#..#..#..#
###....#..#..#..#..#
#####..#.....#.....#
#####..##...###...##
####################
3 3 18 18

出力例 4

3

Score : 400 points

Problem Statement

Takahashi is about to go buy eel at a fish shop.

The town where he lives is divided into a grid of H rows and W columns. Each cell is either a road or a wall.
Let us denote the cell at the i-th row from the top (1\leq i \leq H) and the j-th column from the left (1\leq j \leq W) as cell (i,j).
Information about each cell is given by H strings S_1,S_2,\ldots,S_H, each of length W. Specifically, if the j-th character of S_i (1\leq i \leq H,1\leq j\leq W) is ., cell (i,j) is a road; if it is #, cell (i,j) is a wall.

He can repeatedly perform the following two types of actions in any order:

  • Move to an adjacent cell (up, down, left, or right) that is within the town and is a road.
  • Choose one of the four directions (up, down, left, or right) and perform a front kick in that direction.
    When he performs a front kick, for each of the cells at most 2 steps away in that direction from the cell he is currently in, if that cell is a wall, it becomes a road.
    If some of the cells at most 2 steps away are outside the town, a front kick can still be performed, but anything outside the town does not change.

He starts in cell (A,B), and he wants to move to the fish shop in cell (C,D).
It is guaranteed that both the cell where he starts and the cell with the fish shop are roads.
Find the minimum number of front kicks he needs in order to reach the fish shop.

Constraints

  • 1\leq H\leq 1000
  • 1\leq W\leq 1000
  • Each S_i is a string of length W consisting of . and #.
  • 1\leq A,C\leq H
  • 1\leq B,D\leq W
  • (A,B)\neq (C,D)
  • H, W, A, B, C, and D are integers.
  • The cell where Takahashi starts and the cell with the fish shop are roads.

Input

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

H W
S_1
S_2
\vdots
S_H
A B C D

Output

Print the minimum number of front kicks needed for Takahashi to reach the fish shop.


Sample Input 1

10 10
..........
#########.
#.......#.
#..####.#.
##....#.#.
#####.#.#.
.##.#.#.#.
###.#.#.#.
###.#.#.#.
#.....#...
1 1 7 1

Sample Output 1

1

Takahashi starts in cell (1,1).
By repeatedly moving to adjacent road cells, he can reach cell (7,4).
If he performs a front kick to the left from cell (7,4), cells (7,3) and (7,2) turn from walls to roads.
Then, by continuing to move through road cells (including those that have become roads), he can reach the fish shop in cell (7,1).

In this case, the number of front kicks performed is 1, and it is impossible to reach the fish shop without performing any front kicks, so print 1.


Sample Input 2

2 2
.#
#.
1 1 2 2

Sample Output 2

1

Takahashi starts in cell (1,1).
When he performs a front kick to the right, cell (1,2) turns from a wall to a road.
The cell two steps to the right of (1,1) is outside the town, so it does not change.
Then, he can move to cell (1,2) and then to the fish shop in cell (2,2).

In this case, the number of front kicks performed is 1, and it is impossible to reach the fish shop without performing any front kicks, so print 1.


Sample Input 3

1 3
.#.
1 1 1 3

Sample Output 3

1

When performing a front kick, it is fine if the fish shop’s cell is within the cells that could be turned into a road. Specifically, the fish shop’s cell is a road from the beginning, so it remains unchanged; particularly, the shop is not destroyed by the front kick.


Sample Input 4

20 20
####################
##...##....###...###
#.....#.....#.....##
#..#..#..#..#..#..##
#..#..#....##..#####
#.....#.....#..#####
#.....#..#..#..#..##
#..#..#.....#.....##
#..#..#....###...###
####################
####################
##..#..##...###...##
##..#..#.....#.....#
##..#..#..#..#..#..#
##..#..#..#..#..#..#
##.....#..#..#..#..#
###....#..#..#..#..#
#####..#.....#.....#
#####..##...###...##
####################
3 3 18 18

Sample Output 4

3
H - Cables and Servers

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

配点 : 450 点

問題文

1 から N の番号がついた N 台のサーバーと 1 から M の番号がついた M 本のケーブルがあります。
ケーブル i はサーバー A_i とサーバー B_i を双方向に結んでいます。

以下の操作を何回か( 0 回でもよい)行うことで、全てのサーバー同士がケーブルを介して繋がっているようにしてください。

  • 操作:ケーブルを 1 つ選び、その片方の端を別のサーバーに繋ぎ変える

操作の最小回数と、最小回数となる操作列を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • 入力は全て整数である

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

操作回数の最小値を K とする。K+1 行出力せよ。
1 行目には K を出力せよ。
i+1 行目には i 回目の操作で選ぶケーブルの番号、繋ぎ変える前に繋がっていたサーバーの番号、繋ぎ変えたあと繋がっているサーバーの番号をこの順に空白区切りで出力せよ。

条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

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

出力例 1

1
1 1 3

ケーブル 1 のサーバー 1 に繋がっている側の端をサーバー 3 に繋ぎ変えることで、サーバー同士がケーブルを介して繋がっている状態にすることができます。

図

このほか、「ケーブル 5 のサーバー 4 に繋がっている側の端をサーバー 1 に繋ぎ変える」という操作や、「ケーブル 2 のサーバー 2 に繋がっている側の端をサーバー 3 に繋ぎ変える」という操作でもサーバー同士がケーブルを介して繋がっている状態にすることができるため、正解とみなされます。


入力例 2

4 3
3 4
4 1
1 2

出力例 2

0

操作をする必要がないこともあります。


入力例 3

5 4
3 3
3 3
3 3
3 3

出力例 3

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

Score : 450 points

Problem Statement

There are N servers numbered from 1 to N and M cables numbered from 1 to M.
Cable i connects servers A_i and B_i bidirectionally.

By performing the following operation some number of times (possibly zero), make all servers connected via cables.

  • Operation: Choose one cable and reconnect one of its ends to a different server.

Find the minimum number of operations required and output an operation sequence achieving this minimum.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i, B_i \leq N
  • All input values are integers.

Input

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Let the minimum number of operations be K. Print K+1 lines.

  • The first line should contain K.
  • The (i+1)-th line should contain three space-separated integers: the number of the cable chosen in the i-th operation, the server number that was originally connected at that end, and the server number to which it is connected after the operation, in this order.

If there are multiple valid solutions, any one of them will be accepted.


Sample Input 1

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

Sample Output 1

1
1 1 3

By reconnecting the end of cable 1 that is connected to server 1 to server 3, the servers can be connected via cables.

図

Operations such as reconnecting the end of cable 5 that is connected to server 4 to server 1, or reconnecting the end of cable 2 that is connected to server 2 to server 3, will also result in all servers being connected and are considered correct.


Sample Input 2

4 3
3 4
4 1
1 2

Sample Output 2

0

No operation may be necessary.


Sample Input 3

5 4
3 3
3 3
3 3
3 3

Sample Output 3

4
1 3 5
2 3 4
3 3 2
4 3 1
I - Erase Subarrays

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

配点 : 500 点

問題文

正整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。
あなたは次の操作を 0 回以上何度でも繰り返せます。

  • A から(空でない)連続する部分列を選び、削除する。

x=1,2,\ldots,M に対し、次の問題を解いてください。

  • A の要素の総和をちょうど x にするために必要な操作回数の最小値を求めてください。ただし、どのように操作を行っても A の要素の総和をちょうど x にできない場合は代わりに -1 と出力してください。

なお、A が空である時、A の要素の総和は 0 であるとします。

制約

  • 1 \leq N,M \leq 3000
  • 1 \leq a_i \leq 3000
  • 入力はすべて整数

入力

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

N M
a_1 \ldots a_N

出力

M 行出力せよ。i 行目には x=i に対する答えを出力せよ。


入力例 1

4 5
1 2 3 4

出力例 1

1
2
1
1
1

操作回数が最小である操作の例を以下に示します。

  • x=1 について、a_2,a_3,a_4 に対して操作をすることで A の要素の総和が x になります。
  • x=2 について、a_3,a_4 に対して操作をした後、a_1 に対して操作をすることで A の要素の総和が x になります。
  • x=3 について、a_3,a_4 に対して操作をすることで A の要素の総和が x になります。
  • x=4 について、a_1,a_2,a_3 に対して操作をすることで A の要素の総和が x になります。
  • x=5 について、a_2,a_3 に対して操作をすることで A の要素の総和が x になります。

入力例 2

1 5
3

出力例 2

-1
-1
0
-1
-1

入力例 3

12 20
2 5 6 5 2 1 7 9 7 2 5 5

出力例 3

2
1
2
2
1
2
1
2
2
1
2
1
1
1
2
2
1
1
1
1

Score : 500 points

Problem Statement

You are given an integer array A=(a_1,a_2,\ldots,a_N).
You may perform the following operation any number of times (possibly zero).

  • Choose a nonempty contiguous subarray of A, and delete it from the array.

For each x=1,2,\ldots,M, solve the following problem:

  • Find the minimum possible number of operations to make the sum of elements of A equal x. If it is impossible to make the sum of elements of A equal x, print -1 instead.

Note that the sum of elements of an empty array is 0.

Constraints

  • 1 \leq N,M \leq 3000
  • 1 \leq a_i \leq 3000
  • All values in the input are integers.

Input

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

N M
a_1 \ldots a_N

Output

Print M lines. The i-th line should contain the answer for x=i.


Sample Input 1

4 5
1 2 3 4

Sample Output 1

1
2
1
1
1

The followings are examples of minimum number of operations that achieve the goal.

  • For x=1, delete a_2,a_3,a_4, and the sum of elements of A becomes x.
  • For x=2, delete a_3,a_4, then delete a_1, and the sum of elements of A becomes x.
  • For x=3, delete a_3,a_4, and the sum of elements of A becomes x.
  • For x=4, delete a_1,a_2,a_3, and the sum of elements of A becomes x.
  • For x=5, delete a_2,a_3, and the sum of elements of A becomes x.

Sample Input 2

1 5
3

Sample Output 2

-1
-1
0
-1
-1

Sample Input 3

12 20
2 5 6 5 2 1 7 9 7 2 5 5

Sample Output 3

2
1
2
2
1
2
1
2
2
1
2
1
1
1
2
2
1
1
1
1