A - Filter

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

配点 : 100

問題文

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

A から偶数だけすべて取り出し、もとの順番を保って出力してください。

制約

  • 1\leq N\leq 100
  • 1\leq A _ i\leq 100\ (1\leq i\leq N)
  • A には 1 つ以上偶数が含まれる
  • 入力はすべて整数

入力

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

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

出力

A から偶数を取り出した列を、空白区切りで 1 行に出力せよ。


入力例 1

5
1 2 3 5 6

出力例 1

2 6

A=(1,2,3,5,6) です。 このうち偶数なのは A _ 2=2,A _ 5=62 つなので、26 をこの順に空白区切りで出力してください。


入力例 2

5
2 2 2 3 3

出力例 2

2 2 2

A の中には同じ要素がある場合もあります。


入力例 3

10
22 3 17 8 30 15 12 14 11 17

出力例 3

22 8 30 12 14

Score : 100 points

Problem Statement

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

Print all even numbers in A without changing the order.

Constraints

  • 1\leq N\leq 100
  • 1\leq A _ i\leq 100\ (1\leq i\leq N)
  • A contains at least one even number.
  • All values in the input are integers.

Input

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

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

Output

Print a line containing the sequence of all even numbers in A, with spaces in between.


Sample Input 1

5
1 2 3 5 6

Sample Output 1

2 6

We have A=(1,2,3,5,6). Among them are two even numbers, A _ 2=2 and A _ 5=6, so print 2 and 6 in this order, with a space in between.


Sample Input 2

5
2 2 2 3 3

Sample Output 2

2 2 2

A may contain equal elements.


Sample Input 3

10
22 3 17 8 30 15 12 14 11 17

Sample Output 3

22 8 30 12 14
B - Middle Letter

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

配点 : 100

問題文

英小文字からなる長さが奇数の文字列 S が与えられます。

S の中央の文字を出力してください。

中央の文字とは ある長さが奇数の文字列 T について、 T の長さを |T| として、T の前から \frac{|T|+1}{2} 番目の文字を中央の文字とします。

制約

  • S は英小文字からなる長さが奇数の文字列
  • S の長さは 1 以上 99 以下

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

o

atcoder の中央の文字は o です。


入力例 2

a

出力例 2

a

Score : 100 points

Problem Statement

You are given an odd-length string S consisting of lowercase English letters.

Print the central character of S.

What is the central character? For an odd-length string T, its central character is the \frac{|T|+1}{2}-th character from the beginning, where |T| is the length of T.

Constraints

  • S is an odd-length string consisting of lowercase English letters.
  • The length of S is between 1 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

o

The central character of atcoder is o.


Sample Input 2

a

Sample Output 2

a
C - log2(N)

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

配点 : 200

問題文

正整数 N が与えられるので、 2^k \le N となる最大の整数 k を求めてください。

制約

  • N1 \le N \le 10^{18} を満たす整数である

入力

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

N

出力

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


入力例 1

6

出力例 1

2
  • k=22^2=4 \le 6 を満たします。
  • k \ge 3 である全ての整数 k について 2^k > 6 となります。

以上より、答えは k=2 となります。


入力例 2

1

出力例 2

0

2^0=1 であることに注意してください。


入力例 3

1000000000000000000

出力例 3

59

入力が 32 bit 整数に収まらない場合があります。

Score : 200 points

Problem Statement

Given a positive integer N, find the maximum integer k such that 2^k \le N.

Constraints

  • N is an integer satisfying 1 \le N \le 10^{18}.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

6

Sample Output 1

2
  • k=2 satisfies 2^2=4 \le 6.
  • For every integer k such that k \ge 3, 2^k > 6 holds.

Therefore, the answer is k=2.


Sample Input 2

1

Sample Output 2

0

Note that 2^0=1.


Sample Input 3

1000000000000000000

Sample Output 3

59

The input value may not fit into a 32-bit integer.

D - Humidifier 2

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

配点 : 250

問題文

AtCoder社のオフィスは HW 列のマス目で表されます。上から i 行目、左から j 列目のマスを (i, j) と表します。

各マスの状態は文字 S_{i,j} で表され、 S_{i,j}# のときそのマスは机、. のときそのマスは床です。床のマスは 2 つ以上存在することが保証されます。

あなたは床のマスから異なる 2 マスを選び、加湿器を設置します。

加湿器が設置されたとき、あるマス (i,j) は加湿器があるマス (i',j') からのマンハッタン距離が D 以下であるとき、またその時に限り加湿されます。 なお、マス (i,j) からマス (i',j') までのマンハッタン距離は |i-i'|+|j-j'| で表されます。 ここで、加湿器が置かれた床のマスは必ず加湿されていることに注意してください。

加湿される 床のマス の個数として考えられる最大値を求めてください。

制約

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • 2 \leq H \times W
  • 0 \leq D \leq H+W-2
  • H,W,D は整数
  • S_{i,j}# または . である (1 \leq i \leq H, 1 \leq j \leq W)
  • 床のマスは 2 つ以上存在する

入力

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

H W D
S_{1,1}S_{1,2}\cdotsS_{1,W}
S_{2,1}S_{2,2}\cdotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\cdotsS_{H,W}

出力

答えを出力せよ。


入力例 1

2 5 1
.###.
.#.##

出力例 1

3

マス (1,1) とマス (1,5) に加湿器を置いた時:

  • マス (1,1) の加湿器によってマス (1,1),(2,1)2 マスが加湿されます。
  • マス (1,5) の加湿器によってマス (1,5)1 マスが加湿されます。

よって、合計 3 マス加湿されています。また、4 マス以上加湿するような加湿器の置き方は存在しないため、答えは 3 です。


入力例 2

5 5 2
.#.#.
.....
.#.#.
#.#.#
.....

出力例 2

15

マス (2,4) とマス (5,3) に加湿器を置いた時、15 個の床のマスが加湿されます。


入力例 3

4 4 2
....
.##.
.##.
....

出力例 3

10

Score : 250 points

Problem Statement

The AtCoder company office can be represented as a grid of H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.

The state of each cell is represented by a character S_{i,j}. If S_{i,j} is #, that cell contains a desk; if S_{i,j} is ., that cell is a floor. It is guaranteed that there are at least two floor cells.

You will choose two distinct floor cells and place a humidifier on each.

After placing the humidifiers, a cell (i,j) is humidified if and only if it is within a Manhattan distance D from at least one of the humidifier cells (i',j'). The Manhattan distance between (i,j) and (i',j') is defined as |i - i'| + |j - j'|. Note that any floor cell on which a humidifier is placed is always humidified.

Find the maximum possible number of humidified floor cells.

Constraints

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • 2 \leq H \times W
  • 0 \leq D \leq H+W-2
  • H,W,D are integers.
  • S_{i,j} is # or .. (1 \leq i \leq H, 1 \leq j \leq W)
  • There are at least two floor cells.

Input

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

H W D
S_{1,1}S_{1,2}\cdotsS_{1,W}
S_{2,1}S_{2,2}\cdotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\cdotsS_{H,W}

Output

Print the answer.


Sample Input 1

2 5 1
.###.
.#.##

Sample Output 1

3

When placing humidifiers on (1,1) and (1,5):

  • From the humidifier on (1,1), two cells (1,1) and (2,1) are humidified.
  • From the humidifier on (1,5), one cell (1,5) is humidified.

In total, three cells are humidified. No configuration can humidify four or more floor cells, so the answer is 3.


Sample Input 2

5 5 2
.#.#.
.....
.#.#.
#.#.#
.....

Sample Output 2

15

When placing humidifiers on (2,4) and (5,3), 15 floor cells are humidified.


Sample Input 3

4 4 2
....
.##.
.##.
....

Sample Output 3

10
E - Ideal Sheet

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

配点 : 300

問題文

高橋君は黒いマスと透明なマスからなるシート A,B1 枚ずつと、透明なマスのみからなる無限に広がるシート C を持っています。
また、高橋君には黒いマスと透明なマスからなる、理想とするシート X が存在します。

シート A,B,X の大きさはそれぞれ縦 H_A マス \timesW_A マス、縦 H_B マス \timesW_B マス、縦 H_X マス \timesW_X マスです。
シート A の各マスは .# からなる長さ W_A の文字列 H_AA_1,A_2,\ldots,A_{H_A} によって表され、
A_i (1\leq i\leq H_A)j 文字目 (1\leq j\leq W_A) が、 . のときシート A の上から i 行目かつ左から j 列目のマスは透明なマスであり、 # のとき黒いマスです。
シート B,X の各マスも、同様に長さ W_B の文字列 H_BB_1,B_2,\ldots,B_{H_B} および長さ W_X の文字列 H_XX_1,X_2,\ldots,X_{H_X} によって表されます。

高橋君の目標は、次の手順で、シート A,B,C から、A,B に存在する すべての黒いマスを使って シート X を作り出すことです。

  1. シート A,B をマス目に沿ってシート C に貼り付ける。この時、シート A,B はそれぞれ好きな場所に平行移動させて貼って良いが、シートを切り分けたり、回転させたりしてはいけない。
  2. シート C からマス目に沿って H_X\times W_X マスの領域を切り出す。ここで、切り出されたシートの各マスは、シート A または B の黒いマスが貼り付けられていれば黒いマスに、そうでなければ透明なマスとなる。

このとき、貼り付ける位置と切り出す領域をうまくとることで高橋君は目標を達成できるか、すなわち次の条件をともにみたすことにできるか判定してください。

  • 切り出されたシートはシート A,B黒いマスをすべて 含む。切り出されたシートの上でシート A,B の黒いマスどうしが重なって存在していても構わない。
  • 切り出されたシートは、回転させたり裏返したりすることなくシート X と一致する。

制約

  • 1\leq H_A,W_A,H_B,W_B,H_X,W_X\leq 10
  • H_A,W_A,H_B,W_B,H_X,W_X は整数
  • A_i.# のみからなる長さ W_A の文字列
  • B_i.# のみからなる長さ W_B の文字列
  • X_i.# のみからなる長さ W_X の文字列
  • シート A,B,X はそれぞれ少なくとも 1 つ以上の黒いマスを含む。

入力

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

出力

高橋君が問題文中の目標を達成できるならば Yes を、できないならば No を出力せよ。


入力例 1

3 5
#.#..
.....
.#...
2 2
#.
.#
5 3
...
#.#
.#.
.#.
...

出力例 1

Yes

まず、シート A をシート C に貼り付けると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

さらに、シート B をシート A と左上を合わせて貼ってみると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

ここで、上で具体的に図示されている範囲のうち、上から 1 行目かつ左から 2 列目のマスを左上として 5\times 3 マスを切り出すと下図のようになります。

...
#.#
.#.
.#.
...

これはシート A,B のすべての黒いマスを含んでおり、また、シート X と一致しているため条件を満たしています。

よって、Yes を出力します。


入力例 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

出力例 2

No

シート AB を回転させて貼ってはいけないことに注意してください。


入力例 3

1 1
#
1 2
##
1 1
#

出力例 3

No

どのように貼ったり切り出したりしても、シート B の黒いマスをすべて含むように切り出すことはできないため、1 つめの条件をみたすことができません。 よって、No を出力します。


入力例 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

出力例 4

Yes

Score : 300 points

Problem Statement

Takahashi has two sheets A and B, each composed of black squares and transparent squares, and an infinitely large sheet C composed of transparent squares.
There is also an ideal sheet X for Takahashi composed of black squares and transparent squares.

The sizes of sheets A, B, and X are H_A rows \times W_A columns, H_B rows \times W_B columns, and H_X rows \times W_X columns, respectively.
The squares of sheet A are represented by H_A strings of length W_A, A_1, A_2, \ldots, A_{H_A} consisting of . and #.
If the j-th character (1\leq j\leq W_A) of A_i (1\leq i\leq H_A) is ., the square at the i-th row from the top and j-th column from the left is transparent; if it is #, that square is black.
Similarly, the squares of sheets B and X are represented by H_B strings of length W_B, B_1, B_2, \ldots, B_{H_B}, and H_X strings of length W_X, X_1, X_2, \ldots, X_{H_X}, respectively.

Takahashi's goal is to create sheet X using all black squares in sheets A and B by following the steps below with sheets A, B, and C.

  1. Paste sheets A and B onto sheet C along the grid. Each sheet can be pasted anywhere by translating it, but it cannot be cut or rotated.
  2. Cut out an H_X\times W_X area from sheet C along the grid. Here, a square of the cut-out sheet will be black if a black square of sheet A or B is pasted there, and transparent otherwise.

Determine whether Takahashi can achieve his goal by appropriately choosing the positions where the sheets are pasted and the area to cut out, that is, whether he can satisfy both of the following conditions.

  • The cut-out sheet includes all black squares of sheets A and B. The black squares of sheets A and B may overlap on the cut-out sheet.
  • The cut-out sheet coincides sheet X without rotating or flipping.

Constraints

  • 1\leq H_A, W_A, H_B, W_B, H_X, W_X\leq 10
  • H_A, W_A, H_B, W_B, H_X, W_X are integers.
  • A_i is a string of length W_A consisting of . and #.
  • B_i is a string of length W_B consisting of . and #.
  • X_i is a string of length W_X consisting of . and #.
  • Sheets A, B, and X each contain at least one black square.

Input

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

Output

If Takahashi can achieve the goal described in the problem statement, print Yes; otherwise, print No.


Sample Input 1

3 5
#.#..
.....
.#...
2 2
#.
.#
5 3
...
#.#
.#.
.#.
...

Sample Output 1

Yes

First, paste sheet A onto sheet C, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

Next, paste sheet B so that its top-left corner aligns with that of sheet A, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

Now, cut out a 5\times 3 area with the square in the first row and second column of the range illustrated above as the top-left corner, as shown in the figure below.

...
#.#
.#.
.#.
...

This includes all black squares of sheets A and B and matches sheet X, satisfying the conditions.

Therefore, print Yes.


Sample Input 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

Sample Output 2

No

Note that sheets A and B may not be rotated or flipped when pasting them.


Sample Input 3

1 1
#
1 2
##
1 1
#

Sample Output 3

No

No matter how you paste or cut, you cannot cut out a sheet that includes all black squares of sheet B, so you cannot satisfy the first condition. Therefore, print No.


Sample Input 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

Sample Output 4

Yes
F - Invisible Hand

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

配点 : 300

問題文

りんご市場に N 人の売り手と M 人の買い手がいます。

i 番目の売り手は、A_i 円以上でならりんごを売ってもよいと考えています。

i 番目の買い手は、B_i 円以下でならりんごを買ってもよいと考えています。

次の条件を満たすような最小の整数 X を求めてください。

条件:りんごを X 円で売ってもよいと考える売り手の人数が、りんごを X 円で買ってもよいと考える買い手の人数以上である。

制約

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

入力

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

N M
A_1 \ldots A_N
B_1 \ldots B_M

出力

答えを出力せよ。


入力例 1

3 4
110 90 120
100 80 120 10000

出力例 1

110

りんごを 110 円で売ってもよいと考える売り手は 1,2 番目の 2 人であり、りんごを 110 円で買ってもよいと考える買い手は 3,4 番目の 2 人であるため、110 は条件を満たします。

110 未満の整数が条件を満たすことはないため、これが答えです。


入力例 2

5 2
100000 100000 100000 100000 100000
100 200

出力例 2

201

入力例 3

3 2
100 100 100
80 120

出力例 3

100

Score : 300 points

Problem Statement

There are N sellers and M buyers in an apple market.

The i-th seller may sell an apple for A_i yen or more (yen is the currency in Japan).

The i-th buyer may buy an apple for B_i yen or less.

Find the minimum integer X that satisfies the following condition.

Condition: The number of people who may sell an apple for X yen is greater than or equal to the number of people who may buy an apple for X yen.

Constraints

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

Input

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

N M
A_1 \ldots A_N
B_1 \ldots B_M

Output

Print the answer.


Sample Input 1

3 4
110 90 120
100 80 120 10000

Sample Output 1

110

Two sellers, the 1-st and 2-nd, may sell an apple for 110 yen; two buyers, the 3-rd and 4-th, may buy an apple for 110 yen. Thus, 110 satisfies the condition.

Since an integer less than 110 does not satisfy the condition, this is the answer.


Sample Input 2

5 2
100000 100000 100000 100000 100000
100 200

Sample Output 2

201

Sample Input 3

3 2
100 100 100
80 120

Sample Output 3

100
G - "redocta".swap(i,i+1)

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

配点 : 400

問題文

atcoder の並べ替えである文字列 S が与えられます。
この文字列 S に対して以下の操作を 0 回以上行います。

  • S 中の隣接する 2 文字を選び、入れ替える。

Satcoder にするために必要な最小の操作回数を求めてください。

制約

  • Satcoder の並べ替えである文字列

入力

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

S

出力

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


入力例 1

catredo

出力例 1

8

catredo \rightarrow [ac]tredo \rightarrow actre[od] \rightarrow actr[oe]d \rightarrow actro[de] \rightarrow act[or]de \rightarrow acto[dr]e \rightarrow a[tc]odre \rightarrow atcod[er]
という流れで操作を行うと、 8 回で Satcoder にすることができ、これが達成可能な最小の操作回数です。


入力例 2

atcoder

出力例 2

0

この場合、文字列 S は元から atcoder です。


入力例 3

redocta

出力例 3

21

Score : 400 points

Problem Statement

You are given a string S that is a permutation of atcoder.
On this string S, you will perform the following operation 0 or more times:

  • Choose two adjacent characters of S and swap them.

Find the minimum number of operations required to make S equal atcoder.

Constraints

  • S is a string that is a permutation of atcoder

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer as an integer.


Sample Input 1

catredo

Sample Output 1

8

You can make S equal atcoder in 8 operations as follows:
catredo \rightarrow [ac]tredo \rightarrow actre[od] \rightarrow actr[oe]d \rightarrow actro[de] \rightarrow act[or]de \rightarrow acto[dr]e \rightarrow a[tc]odre \rightarrow atcod[er]
This is the minimum number of operations achievable.


Sample Input 2

atcoder

Sample Output 2

0

In this case, the string S is already atcoder.


Sample Input 3

redocta

Sample Output 3

21
H - Round Trip

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

配点 : 500

問題文

H 行、横 W 列のマス目があり、上から i \, (1 \leq i \leq H) 行目、左から j \, (1 \leq j \leq W) 列目のマスを (i, j) と表します。

各マスは「始点」「道」「障害物」のいずれかです。
マス (i, j) の状態は文字 C_{i, j} で表され、C_{i, j} = S なら始点、C_{i, j} = . なら道、C_{i, j} = # なら障害物です。始点のマスはただ一つ存在します。

始点のマスを出発し、上下または左右に隣接するマスに移動することを繰り返して、障害物のマスを通らずに始点のマスへ戻ってくるような長さ 4 以上の経路であって、最初と最後を除き同じマスを通らないようなものが存在するか判定してください。
より厳密には、以下の条件を満たす整数 n およびマスの列 (x_0, y_0), (x_1, y_1), \dots, (x_n, y_n) が存在するか判定してください。

  • n \geq 4
  • C_{x_0, y_0} = C_{x_n, y_n} = S
  • 1 \leq i \leq n - 1 ならば C_{x_i, y_i} = .
  • 1 \leq i \lt j \leq n - 1 ならば (x_i, y_i) \neq (x_j, y_j)
  • 0 \leq i \leq n - 1 ならばマス (x_i, y_i) とマス (x_{i+1}, y_{i+1}) は上下または左右に隣接する

制約

  • 4 \leq H \times W \leq 10^6
  • H, W2 以上の整数
  • C_{i, j}S.# のいずれか
  • C_{i, j} = S となる (i, j) がただ一つ存在する

入力

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

H W
C_{1, 1} \ldots C_{1, W}
\vdots
C_{H, 1} \ldots C_{H, W}

出力

問題文の条件を満たす経路が存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

4 4
....
#.#.
.S..
.##.

出力例 1

Yes

(3, 2) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (1, 4) \rightarrow (2, 4) \rightarrow (3, 4) \rightarrow (3, 3) \rightarrow (3, 2) という経路が条件を満たします。


入力例 2

2 2
S.
.#

出力例 2

No

入力例 3

5 7
.#...#.
..#.#..
...S...
..#.#..
.#...#.

出力例 3

No

Score : 500 points

Problem Statement

We have a grid with H rows from top to bottom and W columns from left to right. Let (i, j) denote the i-th row from the top (1 \leq i \leq H) and j-th column from the left (1 \leq j \leq W).

Each square is one of the following: the initial point, a road, and an obstacle.
A square (i, j) is represented by a character C_{i, j}. The square is the initial point if C_{i, j} = S, a road if C_{i, j} = ., and an obstacle if C_{i, j} = #. There is exactly one initial point.

Determine whether there is a path of length 4 or greater that starts at the initial point, repeats moving vertically or horizontally to an adjacent square, and returns to the initial point without going through an obstacle or visiting the same square multiple times except at the beginning and the end.
More formally, determine whether there are an integer n and a sequence of squares (x_0, y_0), (x_1, y_1), \dots, (x_n, y_n) that satisfy the following conditions.

  • n \geq 4.
  • C_{x_0, y_0} = C_{x_n, y_n} = S.
  • If 1 \leq i \leq n - 1, then C_{x_i, y_i} = ..
  • If 1 \leq i \lt j \leq n - 1, then (x_i, y_i) \neq (x_j, y_j).
  • If 0 \leq i \leq n - 1, then square (x_i, y_i) and square (x_{i+1}, y_{i+1}) are vertically or horizontally adjacent to each other.

Constraints

  • 4 \leq H \times W \leq 10^6
  • H and W are integers greater than or equal to 2.
  • C_{i, j} is S, ., or #.
  • There is exactly one (i, j) such that C_{i, j} = S.

Input

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

H W
C_{1, 1} \ldots C_{1, W}
\vdots
C_{H, 1} \ldots C_{H, W}

Output

If there is a path that satisfies the conditions in the Problem Statement, print Yes; otherwise, print No.


Sample Input 1

4 4
....
#.#.
.S..
.##.

Sample Output 1

Yes

The path (3, 2) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (1, 4) \rightarrow (2, 4) \rightarrow (3, 4) \rightarrow (3, 3) \rightarrow (3, 2) satisfies the conditions.


Sample Input 2

2 2
S.
.#

Sample Output 2

No

Sample Input 3

5 7
.#...#.
..#.#..
...S...
..#.#..
.#...#.

Sample Output 3

No
I - Hands on Ring (Hard)

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

配点 : 550

問題文

注:この問題は B 問題とほぼ同じ設定です。本文中で太字で示されている部分および制約のみが異なります。

あなたはあるリングを両手で握っています。 このリングは N\ (N\geq 3) 個のパーツ 1,2,\dots,N によって構成されており、パーツ i とパーツ i+1 (1\leq i\leq N-1)、およびパーツ 1 とパーツ N がそれぞれ隣接しています。

最初、左手はパーツ 1 を、右手はパーツ 2 を握っています。 あなたは、1 回の 操作 で以下のことを行えます。

  • 片方の手を、今握っているパーツに隣接するいずれかのパーツに移動する。ただし、移動先にもう一方の手がない場合に限る。

以下の図は、初期状態およびそこから行える操作と行えない操作の例を示したもので、リングの各パーツに書き込まれた数はそのパーツの番号を、L と書かれた丸は左手を、R と書かれた丸は右手を示しています。

あなたは今から与えられる Q 個の指示に順番に従う必要があります。 i\ (1\leq i\leq Q) 個目の指示は文字 H_i および整数 T_i によって表され、その意味は以下の通りです:

  • 操作を何回か(0 回でもよい)行うことで、H_iL ならば左手、R ならば右手が、パーツ T_i を握っている状態にする。 このとき、H_i によって指定された手ではない方の手を 動かしてもよい

なお、本問題の設定および制約の下では、どのような指示も達成可能なことが証明できます。

すべての指示に従うために必要な操作回数の合計の最小値を求めてください。

制約

  • 3\leq N \leq 3000
  • 1\leq Q \leq 3000
  • H_iL または R
  • 1\leq T_i\leq N
  • N,Q,T_i は整数

入力

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

N Q
H_1 T_1
H_2 T_2
\vdots
H_Q T_Q

出力

すべての指示に従うために必要な操作回数の合計の最小値を出力せよ。


入力例 1

6 3
R 4
L 5
R 5

出力例 1

6

以下のように操作を行うことで、Q 個の指示すべてに順番に従うことができます。

  1. 右手をパーツ 2\rightarrow 3\rightarrow 4 と移動させることで、1 番目の指示に従う。
  2. 左手をパーツ 1\rightarrow 6\rightarrow 5 と移動させることで、2 番目の指示に従う。
  3. 左手をパーツ 5\rightarrow 6 と移動させたのち、右手をパーツ 4\rightarrow 5 と移動させることで、3 番目の指示に従う。

このとき行う操作回数の合計は 2+2+1+1=6 であり、これが最小です。


入力例 2

100 2
L 1
R 2

出力例 2

0

操作を 1 度も行わずに指示に従うことができる場合もあります。


入力例 3

30 8
R 23
R 26
R 29
L 20
R 29
R 19
L 7
L 16

出力例 3

58

Score : 550 points

Problem Statement

Note: This problem has almost the same setting as Problem B. Only the parts in bold in the main text and constraints differ.

You are holding a ring with both hands. This ring consists of N\ (N \geq 3) parts numbered 1,2,\dots,N, where parts i and i+1 (1 \leq i \leq N-1) are adjacent, and parts 1 and N are also adjacent.

Initially, your left hand is holding part 1, and your right hand is holding part 2. In one operation, you can do the following:

  • Move one of your hands to an adjacent part of the part it is currently holding. However, you can do this only if the other hand is not on the destination part.

The following figure shows the initial state and examples of operations that can and cannot be made from there. The number written on each part of the ring represents the part number, and the circles labeled L and R represent your left and right hands, respectively.

You need to follow Q instructions given to you in order. The i-th (1 \leq i \leq Q) instruction is represented by a character H_i and an integer T_i, meaning the following:

  • Perform some number of operations (possibly zero) so that your left hand (if H_i is L) or your right hand (if H_i is R) is holding part T_i. Here, you may move the other hand not specified by H_i.

Under the settings and constraints of this problem, it can be proved that any instructions are achievable.

Find the minimum total number of operations required to follow all the instructions.

Constraints

  • 3\leq N \leq 3000
  • 1\leq Q \leq 3000
  • H_i is L or R.
  • 1 \leq T_i \leq N
  • N, Q, and T_i are integers.

Input

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

N Q
H_1 T_1
H_2 T_2
\vdots
H_Q T_Q

Output

Print the minimum total number of operations required to follow all the instructions.


Sample Input 1

6 3
R 4
L 5
R 5

Sample Output 1

6

By performing the following operations, you can follow all Q instructions in order.

  1. Move your right hand as part 2 \rightarrow 3 \rightarrow 4 to follow the first instruction.
  2. Move your left hand as part 1 \rightarrow 6 \rightarrow 5 to follow the second instruction.
  3. Move your left hand as part 5 \rightarrow 6, then move your right hand as part 4 \rightarrow 5 to follow the third instruction.

In this case, the total number of operations is 2+2+1+1=6, which is the minimum.


Sample Input 2

100 2
L 1
R 2

Sample Output 2

0

There are cases where you can follow the instructions without performing any operations.


Sample Input 3

30 8
R 23
R 26
R 29
L 20
R 29
R 19
L 7
L 16

Sample Output 3

58