A - 321-like Checker

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

以下の条件を満たす正整数 x を 321-like Number と呼びます。

  • x の各桁を上から見ると狭義単調減少になっている。
  • すなわち、x が d 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
    • ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )

なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。

例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。

N が入力として与えられるので、 N が 321-like Number なら Yes 、そうでないなら No と出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 99999

入力

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

N

出力

N が 321-like Number なら Yes 、そうでないなら No と出力せよ。


入力例 1

321

出力例 1

Yes

N=321 に対して、以下が成り立ちます。

  • 上から 1 桁目の 3 は上から 2 桁目の 2 より大きい。
  • 上から 2 桁目の 2 は上から 3 桁目の 1 より大きい。

以上より、 321 は 321-like Number です。


入力例 2

123

出力例 2

No

N=123 について、例えば以下が成り立ちます。

  • 上から 1 桁目の 1 は上から 2 桁目の 2 より大きくない。

このことから、 123 は 321-like Number ではありません。


入力例 3

1

出力例 3

Yes

入力例 4

86411

出力例 4

No

Score : 100 points

Problem Statement

A positive integer x is called a 321-like Number when it satisfies the following condition.

  • The digits of x are strictly decreasing from top to bottom.
  • In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
    • (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).

Note that all one-digit positive integers are 321-like Numbers.

For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.

You are given N as input. Print Yes if N is a 321-like Number, and No otherwise.

Constraints

  • All input values are integers.
  • 1 \le N \le 99999

Input

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

N

Output

Print Yes if N is a 321-like Number, and No otherwise.


Sample Input 1

321

Sample Output 1

Yes

For N=321, the following holds:

  • The first digit from the top, 3, is greater than the second digit from the top, 2.
  • The second digit from the top, 2, is greater than the third digit from the top, 1.

Thus, 321 is a 321-like Number.


Sample Input 2

123

Sample Output 2

No

For N=123, the following holds:

  • The first digit from the top, 1, is not greater than the second digit from the top, 2.

Thus, 123 is not a 321-like Number.


Sample Input 3

1

Sample Output 3

Yes

Sample Input 4

86411

Sample Output 4

No
B - Four Digits

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

0 以上 9999 以下の整数 N が与えられます。

N の先頭に必要なだけ 0 をつけ、4 桁の文字列にしたものを出力してください。

制約

  • 0 \leq N \leq 9999
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

321

出力例 1

0321

321 は 3 桁なので、先頭に 1 つ 0 をつけると 4 桁になります。


入力例 2

7777

出力例 2

7777

入力例 3

1

出力例 3

0001

Score : 100 points

Problem Statement

You are given an integer N between 0 and 9999 (inclusive).

Print it as a four-digit string after appending to it the necessary number of leading zeros.

Constraints

  • 0 \leq N \leq 9999
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

321

Sample Output 1

0321

321 has three digits, so we need to add one leading zero to it to make it have four digits.


Sample Input 2

7777

Sample Output 2

7777

Sample Input 3

1

Sample Output 3

0001
C - Who is Missing?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

長さ M の整数列 A=(A_1,A_2,\dots,A_M) が与えられます。
A の各要素は 1 以上 N 以下で、全ての要素は相異なります。

A の要素として含まれない 1 以上 N 以下の整数を、昇順に全て列挙してください。

制約

  • 入力は全て整数
  • 1 \le M \le N \le 1000
  • 1 \le A_i \le N
  • A の要素は相異なる

入力

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

N M
A_1 A_2 \dots A_M

出力

A の要素として含まれない 1 以上 N 以下の整数を昇順に全て挙げた列が (X_1,X_2,\dots,X_C) であるとき、以下の形式で出力せよ。

C
X_1 X_2 \dots X_C

入力例 1

10 3
3 9 2

出力例 1

7
1 4 5 6 7 8 10

A=(3,9,2) です。
A の要素として含まれない 1 以上 10 以下の整数を昇順に全て挙げると、 1,4,5,6,7,8,10 となります。


入力例 2

6 6
1 3 5 2 4 6

出力例 2

0

A の要素として含まれない 1 以上 6 以下の整数がひとつもありません。
この場合、 1 行目に 0 と出力し、 2 行目は空行としてください。


入力例 3

9 1
9

出力例 3

8
1 2 3 4 5 6 7 8

Score : 200 points

Problem Statement

You are given a sequence of M integers A = (A_1, A_2, \dots, A_M).
Each element of A is an integer between 1 and N, inclusive, and all elements are distinct.

List all integers between 1 and N that do not appear in A in ascending order.

Constraints

  • All input values are integers.
  • 1 \le M \le N \le 1000
  • 1 \le A_i \le N
  • The elements of A are distinct.

Input

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

N M
A_1 A_2 \dots A_M

Output

Let (X_1, X_2, \dots, X_C) be the sequence of all integers between 1 and N, inclusive, that do not appear in A, listed in ascending order. The output should be in the following format:

C
X_1 X_2 \dots X_C

Sample Input 1

10 3
3 9 2

Sample Output 1

7
1 4 5 6 7 8 10

Here, A=(3,9,2).
The integers between 1 and 10 that do not appear in A, listed in ascending order, are 1,4,5,6,7,8,10.


Sample Input 2

6 6
1 3 5 2 4 6

Sample Output 2

0

No integer between 1 and 6 is missing from A.
In this case, print 0 on the first line and leave the second line empty.


Sample Input 3

9 1
9

Sample Output 3

8
1 2 3 4 5 6 7 8
D - 1D Pawn

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

N 個のマスが左右一列に並んでおり、左から順にマス 1、マス 2、…、マス N と番号づけられています。
また、K 個のコマがあり、最初左から i 番目のコマはマス A_i に置かれています。
これらに対して、Q 回の操作を行います。 i 回目の操作では次の操作を行います。

  • 左から L_i 番目のコマが一番右のマスにあるならば何も行わない。
  • そうでない時、左から L_i 番目のコマがあるマスの 1 つ右のマスにコマが無いならば、左から L_i 番目のコマを 1 つ右のマスに移動させる。 1 つ右のマスにコマがあるならば、何も行わない。

Q 回の操作が終了した後の状態について、i=1,2,\ldots,K に対して左から i 番目のコマがあるマスの番号を出力してください。

制約

  • 1\leq K\leq N\leq 200
  • 1\leq A_1<A_2<\cdots<A_K\leq N
  • 1\leq Q\leq 1000
  • 1\leq L_i\leq K
  • 入力はすべて整数

入力

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

N K Q
A_1 A_2 \ldots A_K
L_1 L_2 \ldots L_Q

出力

K 個の整数を空白区切りで一行に出力せよ。 ここで、i 個目の整数は、 Q 回の操作が終了した後の状態について、左から i 番目のコマの番号を表す。


入力例 1

5 3 5
1 3 4
3 3 1 1 2

出力例 1

2 4 5

最初、コマはマス 1, 3, 4 にあります。これに対して以下のように操作が行われます。

  • 左から 3 番目のコマはマス 4 にあります。 これは一番右のマスでなく、その 1 つ右のマスにもコマが置かれていないため、左から 3 番目のコマをマス 5 に動かします。 コマはマス 1, 3, 5 にある状態になります。
  • 左から 3 番目のコマはマス 5 にあります。 これは一番右のマスなので、何も行いません。 コマはマス 1, 3, 5 にある状態のままです。
  • 左から 1 番目のコマはマス 1 にあります。 これは一番右のマスでなく、その 1 つ右のマスにもコマが置かれていないため、左から 1 番目のコマをマス 2 に動かします。 コマはマス 2, 3, 5 にある状態になります。
  • 左から 1 番目のコマはマス 2 にあります。 これは一番右のマスでありませんが、その 1 つ右のマス(マス 3 )にコマが置かれているため、何も行いません。 コマはマス 2, 3, 5 にある状態のままです。
  • 左から 2 番目のコマはマス 3 にあります。 これは一番右のマスでなく、その右のマスにもコマが置かれていないため、左から 2 番目のコマをマス 4 に動かします。 コマはマス 2, 4, 5 にある状態になります。

よって、Q 回の操作が終わった後でコマはマス 2, 4, 5 に置かれているため、2,4,5 を空白区切りでこの順に出力します。


入力例 2

2 2 2
1 2
1 2

出力例 2

1 2

入力例 3

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

出力例 3

2 5 6 7 9 10

Score : 200 points

Problem Statement

There are N squares, indexed Square 1, Square 2, …, Square N, arranged in a row from left to right.
Also, there are K pieces. The i-th piece from the left is initially placed on Square A_i.
Now, we will perform Q operations against them. The i-th operation is as follows:

  • If the L_i-th piece from the left is on its rightmost square, do nothing.
  • Otherwise, move the L_i-th piece from the left one square right if there is no piece on the next square on the right; if there is, do nothing.

Print the index of the square on which the i-th piece from the left is after the Q operations have ended, for each i=1,2,\ldots,K.

Constraints

  • 1\leq K\leq N\leq 200
  • 1\leq A_1<A_2<\cdots<A_K\leq N
  • 1\leq Q\leq 1000
  • 1\leq L_i\leq K
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A_1 A_2 \ldots A_K
L_1 L_2 \ldots L_Q

Output

Print K integers in one line, with spaces in between. The i-th of them should be the index of the square on which the i-th piece from the left is after the Q operations have ended.


Sample Input 1

5 3 5
1 3 4
3 3 1 1 2

Sample Output 1

2 4 5

At first, the pieces are on Squares 1, 3, and 4. The operations are performed against them as follows:

  • The 3-rd piece from the left is on Square 4. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 3-rd piece from the left to Square 5. Now, the pieces are on Squares 1, 3, and 5.
  • The 3-rd piece from the left is on Square 5. This is the rightmost square, so do nothing. The pieces are still on Squares 1, 3, and 5.
  • The 1-st piece from the left is on Square 1. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 1-st piece from the left to Square 2. Now, the pieces are on Squares 2, 3, and 5.
  • The 1-st piece from the left is on Square 2. This is not the rightmost square, but the next square on the right (Square 3) contains a piece, so do nothing. The pieces are still on Squares 2, 3, and 5.
  • The 2-nd piece from the left is on Square 3. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 2-nd piece from the left to Square 4; Now, the pieces are still on Squares 2, 4, and 5.

Thus, after the Q operations have ended, the pieces are on Squares 2, 4, and 5, so 2, 4, and 5 should be printed in this order, with spaces in between.


Sample Input 2

2 2 2
1 2
1 2

Sample Output 2

1 2

Sample Input 3

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

Sample Output 3

2 5 6 7 9 10
E - Divide and Divide

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

黒板に整数 N が 1 個書かれています。
高橋君は黒板に書かれている 2 以上の整数が全て無くなるまで以下の一連の操作を繰り返します。

  • 黒板に書かれている 2 以上の整数を 1 つ選び x とする。
  • 黒板から x を 1 個消す。そして、2 個の整数 \left \lfloor \dfrac{x}{2} \right\rfloor, \left\lceil \dfrac{x}{2} \right\rceil を新たに黒板に書く。
  • この一連の操作を行うために高橋君は x 円払う必要がある。

ここで \lfloor a \rfloor は a 以下の整数のうち最大のものを、\lceil a \rceil は a 以上の整数のうち最小のものを意味します。

操作を行えなくなった時点で高橋君が払った金額の総和は何円ですか?
なお、どのような順番で操作を行っても高橋君が払う金額の総和は一定であることが証明できます。

制約

  • 2 \leq N \leq 10^{17}

入力

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

N

出力

高橋君が払った金額の総和が何円であるかを出力せよ。


入力例 1

3

出力例 1

5

高橋君が行う操作の一例を挙げると次のようになります。

  • はじめ、黒板には 3 が 1 個書かれている。
  • 高橋君は 3 を選ぶ。高橋君は 3 円を払い、黒板から 3 を 1 個消して \left \lfloor \dfrac{3}{2} \right\rfloor = 1, \left\lceil \dfrac{3}{2} \right\rceil = 2 を新たに黒板に書く。
  • 黒板には 2 が 1 個と 1 が 1 個書かれている。
  • 高橋君は 2 を選ぶ。高橋君は 2 円を払い、黒板から 2 を 1 個消して \left \lfloor \dfrac{2}{2} \right\rfloor = 1, \left\lceil \dfrac{2}{2} \right\rceil = 1 を新たに黒板に書く。
  • 黒板には 1 が 3 個書かれている。
  • 黒板から 2 以上の整数が全て無くなったので操作を終了する。

操作全体で高橋君は 3 + 2 = 5 円払ったので、5 を出力します。


入力例 2

340

出力例 2

2888

入力例 3

100000000000000000

出力例 3

5655884811924144128

Score: 300 points

Problem Statement

There is a single integer N written on a blackboard.
Takahashi will repeat the following series of operations until all integers not less than 2 are removed from the blackboard:

  • Choose one integer x not less than 2 written on the blackboard.
  • Erase one occurrence of x from the blackboard. Then, write two new integers \left \lfloor \dfrac{x}{2} \right\rfloor and \left\lceil \dfrac{x}{2} \right\rceil on the blackboard.
  • Takahashi must pay x yen to perform this series of operations.

Here, \lfloor a \rfloor denotes the largest integer not greater than a, and \lceil a \rceil denotes the smallest integer not less than a.

What is the total amount of money Takahashi will have paid when no more operations can be performed?
It can be proved that the total amount he will pay is constant regardless of the order in which the operations are performed.

Constraints

  • 2 \leq N \leq 10^{17}

Input

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

N

Output

Print the total amount of money Takahashi will have paid, in yen.


Sample Input 1

3

Sample Output 1

5

Here is an example of how Takahashi performs the operations:

  • Initially, there is one 3 written on the blackboard.
  • He chooses 3. He pays 3 yen, erases one 3 from the blackboard, and writes \left \lfloor \dfrac{3}{2} \right\rfloor = 1 and \left\lceil \dfrac{3}{2} \right\rceil = 2 on the blackboard.
  • There is one 2 and one 1 written on the blackboard.
  • He chooses 2. He pays 2 yen, erases one 2 from the blackboard, and writes \left \lfloor \dfrac{2}{2} \right\rfloor = 1 and \left\lceil \dfrac{2}{2} \right\rceil = 1 on the blackboard.
  • There are three 1s written on the blackboard.
  • Since all integers not less than 2 have been removed from the blackboard, the process is finished.

Takahashi has paid a total of 3 + 2 = 5 yen for the entire process, so print 5.


Sample Input 2

340

Sample Output 2

2888

Sample Input 3

100000000000000000

Sample Output 3

5655884811924144128
F - Takahashi Gets Lost

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 250 点

問題文

H 行 W 列のグリッドがあります。

グリッドの各マスは陸か海のどちらかであり、 その情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で与えられます。 上から i 行目、左から j 列目のマスを (i, j) で表すと、 S_i の j 文字目が . のときマス (i, j) は陸であり、# のときマス (i, j) は海です。

ここで、グリッドの外周のマス(すなわち、i = 1 、i = H 、j = 1 、j = W のうち少なくとも 1 個以上を満たすマス (i, j) )については、すべて海であることが制約として保証されます。

高橋君が乗った宇宙船が、グリッド上のあるマスに不時着してしまいました。 その後、高橋君は L 、R 、U 、D のみからなる長さ N の文字列 T で表される手順に沿って、グリッド上を N 回移動しました。 i = 1, 2, \ldots, N について、T の i 文字目は i 回目の移動の内容を下記の通り表します。

  • L のとき、左に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j-1) である。
  • R のとき、右に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j+1) である。
  • U のとき、上に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i-1, j) である。
  • D のとき、下に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i+1, j) である。

高橋君の移動経路上のマス(不時着したマスおよび現在いるマスを含む)はいずれも海でないことがわかっています。 高橋君が現在いるマスとしてあり得るものの個数を出力してください。

制約

  • H, W, N は整数
  • 3 \leq H, W \leq 500
  • 1 \leq N \leq 500
  • T は L 、R 、U 、D のみからなる長さ N の文字列
  • S_i は . と # のみからなる長さ W の文字列
  • 高橋君が現在いるマスとしてあり得るものが少なくとも 1 個存在する。
  • グリッドの外周のマスはすべて海である。

入力

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

H W N
T
S_1
S_2
\vdots
S_H

出力

答えを出力せよ。


入力例 1

6 7 5
LULDR
#######
#...#.#
##...##
#.#...#
#...#.#
#######

出力例 1

2

下記の 2 つの場合がありえるため、高橋君が現在いるマスとしてあり得るものは (3, 4) と (4, 5) の 2 個です。

  • マス (3, 5) に不時着し、(3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4) と移動した場合
  • マス (4, 6) に不時着し、(4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5) と移動した場合

入力例 2

13 16 9
ULURDLURD
################
##..##.#..####.#
###.#..#.....#.#
#..##..#####.###
#...#..#......##
###.##.#..#....#
##.#####....##.#
###.###.#.#.#..#
######.....##..#
#...#.#.######.#
##..###..#..#.##
#...#.#.#...#..#
################

出力例 2

6

Score: 250 points

Problem Statement

There is a grid with H rows and W columns.

Each cell of the grid is land or sea, which is represented by H strings S_1, S_2, \ldots, S_H of length W. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left, and (i, j) is land if the j-th character of S_i is ., and (i, j) is sea if the character is #.

The constraints guarantee that all cells on the perimeter of the grid (that is, the cells (i, j) that satisfy at least one of i = 1, i = H, j = 1, j = W) are sea.

Takahashi's spaceship has crash-landed on a cell in the grid. Afterward, he moved N times on the grid following the instructions represented by a string T of length N consisting of L, R, U, and D. For i = 1, 2, \ldots, N, the i-th character of T describes the i-th move as follows:

  • L indicates a move of one cell to the left. That is, if he is at (i, j) before the move, he will be at (i, j-1) after the move.
  • R indicates a move of one cell to the right. That is, if he is at (i, j) before the move, he will be at (i, j+1) after the move.
  • U indicates a move of one cell up. That is, if he is at (i, j) before the move, he will be at (i-1, j) after the move.
  • D indicates a move of one cell down. That is, if he is at (i, j) before the move, he will be at (i+1, j) after the move.

It is known that all cells along his path (including the cell where he crash-landed and the cell he is currently on) are not sea. Print the number of cells that could be his current position.

Constraints

  • H, W, and N are integers.
  • 3 \leq H, W \leq 500
  • 1 \leq N \leq 500
  • T is a string of length N consisting of L, R, U, and D.
  • S_i is a string of length W consisting of . and #.
  • There is at least one cell that could be Takahashi's current position.
  • All cells on the perimeter of the grid are sea.

Input

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

H W N
T
S_1
S_2
\vdots
S_H

Output

Print the answer.


Sample Input 1

6 7 5
LULDR
#######
#...#.#
##...##
#.#...#
#...#.#
#######

Sample Output 1

2

The following two cases are possible, so there are two cells that could be Takahashi's current position: (3, 4) and (4, 5).

  • He crash-landed on cell (3, 5) and moved (3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4).
  • He crash-landed on cell (4, 6) and moved (4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5).

Sample Input 2

13 16 9
ULURDLURD
################
##..##.#..####.#
###.#..#.....#.#
#..##..#####.###
#...#..#......##
###.##.#..#....#
##.#####....##.#
###.###.#.#.#..#
######.....##..#
#...#.#.######.#
##..###..#..#.##
#...#.#.#...#..#
################

Sample Output 2

6
G - Synchronized Players

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

N 行 N 列のグリッドがあり、各マスは空きマスか障害物のあるマスのいずれかです。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

また、2 人のプレイヤーがグリッド上の相異なる空きマス上におり、各マスの情報は N 個の長さ N の文字列 S_1, S_2, \ldots, S_N として以下の形式で与えられます。

  • S_i の j 文字目が P であるとき、(i, j) は空きマスであり、プレイヤーがいる

  • S_i の j 文字目が . であるとき、(i, j) は空きマスであり、プレイヤーがいない

  • S_i の j 文字目が # であるとき、(i, j) は障害物のあるマスである

以下の操作を繰り返し 2 人のプレイヤーを同じマスに集めるために必要な操作回数の最小値を求めてください。ただし、操作の繰り返しにより 2 人のプレイヤーを同じマスに集めることができない場合には -1 を出力してください。

  • 上下左右のいずれかの方向を決める。そして各プレイヤーはともにその方向に隣接するマスへの移動を試みる。各プレイヤーは移動先のマスが存在し、かつ空きマスであるならば移動し、そうでないならば移動しない。

制約

  • N は 2 以上 60 以下の整数
  • S_i は長さ N の P, ., # からなる文字列
  • S_i の j 文字目が P であるような組 (i, j) の個数はちょうど 2 つ

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5
....#
#..#.
.P...
..P..
....#

出力例 1

3

はじめに (3, 2) にいるプレイヤーをプレイヤー 1、(4, 3) にいるプレイヤーをプレイヤー 2 とします。

例えば以下のようにすることで、3 回の操作で 2 人のプレイヤーが同じマスに集まります。

  • 左を選択する。プレイヤー 1 は (3, 1) に移動し、プレイヤー 2 は (4, 2) に移動する。

  • 上を選択する。プレイヤー 1 は移動せず、プレイヤー 2 は (3, 2) に移動する。

  • 左を選択する。プレイヤー 1 は移動せず、プレイヤー 2 は (3, 1) に移動する。


入力例 2

2
P#
#P

出力例 2

-1

入力例 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

出力例 3

10

Score: 400 points

Problem Statement

There is an N \times N grid, where each cell is either empty or contains an obstacle. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

There are also two players on distinct empty cells of the grid. The information about each cell is given as N strings S_1, S_2, \ldots, S_N of length N, in the following format:

  • If the j-th character of S_i is P, then (i, j) is an empty cell with a player on it.

  • If the j-th character of S_i is ., then (i, j) is an empty cell without a player.

  • If the j-th character of S_i is #, then (i, j) contains an obstacle.

Find the minimum number of moves required to bring the two players to the same cell by repeating the following operation. If it is impossible to bring the two players to the same cell by repeating the operation, print -1.

  • Choose one of the four directions: up, down, left, or right. Then, each player attempts to move to the adjacent cell in that direction. Each player moves if the destination cell exists and is empty, and does not move otherwise.

Constraints

  • N is an integer between 2 and 60, inclusive.
  • S_i is a string of length N consisting of P, ., and #.
  • There are exactly two pairs (i, j) where the j-th character of S_i is P.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5
....#
#..#.
.P...
..P..
....#

Sample Output 1

3

Let us call the player starting at (3, 2) Player 1 and the player starting at (4, 3) Player 2.

For example, doing the following brings the two players to the same cell in three moves:

  • Choose left. Player 1 moves to (3, 1), and Player 2 moves to (4, 2).

  • Choose up. Player 1 does not move, and Player 2 moves to (3, 2).

  • Choose left. Player 1 does not move, and Player 2 moves to (3, 1).


Sample Input 2

2
P#
#P

Sample Output 2

-1

Sample Input 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

Sample Output 3

10
H - Add and Mex

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

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

以下の操作を M 回行ってください。

  • 各 i\ (1\leq i \leq N) について、 A_i に i を加算する。その後 A に含まれない最小の非負整数を求める。

制約

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

入力

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

N M 
A_1 A_2 \ldots A_N

出力

M 行出力せよ。

i (1\leq i \leq M) 行目には i 回目の操作後に A に含まれない最小の非負整数を出力せよ。


入力例 1

3 3
-1 -1 -6

出力例 1

2
2
0

1 回目の操作では、数列 A は

(-1 + 1, -1 +2 ,-6+3) = (0,1,-3)

になります。 A に含まれない最小の非負整数は 2 です。

2 回目の操作では、数列 A は

(0 + 1, 1 +2 ,-3+3) = (1,3,0)

になります。 A に含まれない最小の非負整数は 2 です。

3 回目の操作では、数列 A は

(1 + 1, 3 +2 ,0+3) = (2,5,3)

になります。 A に含まれない最小の非負整数は 0 です。


入力例 2

5 6
-2 -2 -5 -7 -15

出力例 2

1
3
2
0
0
0

Score : 500 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.

Perform the following operation M times:

  • For each i\ (1\leq i \leq N), add i to A_i. Then, find the minimum non-negative integer not contained in A.

Constraints

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

Input

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

N M 
A_1 A_2 \ldots A_N

Output

Print M lines.

The i-th (1\leq i \leq M) line should contain the minimum non-negative integer not contained in A after the i-th operation.


Sample Input 1

3 3
-1 -1 -6

Sample Output 1

2
2
0

The 1-st operation makes the sequence A

(-1 + 1, -1 +2 ,-6+3) = (0,1,-3).

The minimum non-negative integer not contained in A is 2.

The 2-nd operation makes the sequence A

(0 + 1, 1 +2 ,-3+3) = (1,3,0).

The minimum non-negative integer not contained in A is 2.

The 3-rd operation makes the sequence A

(1 + 1, 3 +2 ,0+3) = (2,5,3).

The minimum non-negative integer not contained in A is 0.


Sample Input 2

5 6
-2 -2 -5 -7 -15

Sample Output 2

1
3
2
0
0
0
I - Make Bipartite

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N+1 頂点の無向グラフが与えられます。
頂点には頂点 0 、頂点 1 、\ldots 、頂点 N と名前がついています。

i=1,2,\ldots,N について、頂点 0 と頂点 i を結ぶ重み A_i の無向辺があります。

また、i=1,2,\ldots,N について、頂点 i と頂点 i+1 を結ぶ重み B_i の無向辺があります。(ただし、頂点 N+1 は頂点 1 とみなします。)

上に述べた 2N 本の辺の他に辺はありません。

このグラフからいくつかの辺を削除して、グラフを二部グラフにします。
削除する辺の重みの総和の最小値はいくつですか?

制約

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

入力

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

答えを出力せよ。


入力例 1

5
31 4 159 2 65
5 5 5 5 10

出力例 1

16


頂点 0,2 を結ぶ辺(重み 4 )、頂点 0,4 を結ぶ辺(重み 2 )、頂点 1,5 を結ぶ辺(重み 10 )を削除するとグラフは二部グラフになります。


入力例 2

4
100 100 100 1000000000
1 2 3 4

出力例 2

10

Score : 500 points

Problem Statement

Given is an undirected graph with N+1 vertices.
The vertices are called Vertex 0, Vertex 1, \ldots, Vertex N.

For each i=1,2,\ldots,N, the graph has an undirected edge with a weight of A_i connecting Vertex 0 and Vertex i.

Additionally, for each i=1,2,\ldots,N, the graph has an undirected edge with a weight of B_i connecting Vertex i and Vertex i+1. (Here, Vertex N+1 stands for Vertex 1.)

The graph has no edge other than these 2N edges above.

Let us delete some of the edges from this graph so that the graph will be bipartite.
What is the minimum total weight of the edges that have to be deleted?

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Print the answer.


Sample Input 1

5
31 4 159 2 65
5 5 5 5 10

Sample Output 1

16


Deleting the edge connecting Vertices 0,2 (weight: 4), the edge connecting Vertices 0,4 (weight: 2), and the edge connecting Vertices 1,5 (weight: 10) makes the graph bipartite.


Sample Input 2

4
100 100 100 1000000000
1 2 3 4

Sample Output 2

10