A - Glutton Takahashi

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

配点 : 100

問題文

高橋君は N 個の料理を食べようとしています。

i 番目に食べようとしている料理は、S_i = sweet のとき甘い料理であり、S_i = salty のとき塩辛い料理です。

高橋君は甘い料理を 2 つ連続で食べると気持ち悪くなってしまい、その後料理が食べられなくなってしまいます。

高橋君がすべての料理を食べることができるか判定してください。

制約

  • N1 以上 100 以下の整数
  • S_isweet または salty

入力

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

N
S_1
S_2
\vdots
S_N

出力

高橋君がすべての料理を食べることができるならば Yes を、できないならば No を出力せよ。


入力例 1

5
salty
sweet
salty
salty
sweet

出力例 1

Yes

高橋君は甘い料理を 2 つ連続で食べることがないので、気持ち悪くなることなくすべての料理を食べることができます。


入力例 2

4
sweet
salty
sweet
sweet

出力例 2

Yes

高橋君は気持ち悪くなってしまいますが、すべての料理を食べることができます。


入力例 3

6
salty
sweet
sweet
salty
sweet
sweet

出力例 3

No

高橋君は 3 番目の料理を食べると気持ち悪くなってしまい、4 番目以降の料理が食べられなくなります。

Score : 100 points

Problem Statement

Takahashi is planning to eat N dishes.

The i-th dish he plans to eat is sweet if S_i = sweet, and salty if S_i = salty.

If he eats two sweet dishes consecutively, he will feel sick and be unable to eat any more dishes.

Determine whether he can eat all the dishes.

Constraints

  • N is an integer between 1 and 100, inclusive.
  • Each S_i is sweet or salty.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print Yes if Takahashi can eat all the dishes, and No otherwise.


Sample Input 1

5
salty
sweet
salty
salty
sweet

Sample Output 1

Yes

He will not eat two sweet dishes consecutively, so he can eat all the dishes without feeling sick.


Sample Input 2

4
sweet
salty
sweet
sweet

Sample Output 2

Yes

He will feel sick but can still eat all the dishes.


Sample Input 3

6
salty
sweet
sweet
salty
sweet
sweet

Sample Output 3

No

He feels sick when eating the 3rd dish and cannot eat the 4th and subsequent dishes.

B - Takahashi san 2

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

配点 : 100

問題文

キーエンスでは、役割や年齢、立場の違いに関係なく「さん」付けして呼ぶという文化があります。

英小文字のみからなる文字列 S が与えられます。
Ssan で終わっているならば Yes を、終わっていないならば No を出力してください。

制約

  • S は英小文字のみからなる長さ 4 以上 30 以下の文字列

入力

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

S

出力

Ssan で終わっているならば Yes を、終わっていないならば No を出力せよ。


入力例 1

takahashisan

出力例 1

Yes

文字列 S=takahashisansan で終わっているため、 Yes を出力します。


入力例 2

aokikun

出力例 2

No

文字列 S=aokikunsan で終わっていないため、 No を出力します。

Score : 100 points

Problem Statement

KEYENCE has a culture of addressing everyone with the suffix "-san," regardless of roles, age, or positions.

You are given a string S consisting of lowercase English letters.
If S ends with san, print Yes; otherwise, print No.

Constraints

  • S is a string of length between 4 and 30, inclusive, consisting of lowercase English letters.

Input

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

S

Output

If S ends with san, print Yes; otherwise, print No.


Sample Input 1

takahashisan

Sample Output 1

Yes

The string S= takahashisan ends with san, so print Yes.


Sample Input 2

aokikun

Sample Output 2

No

The string S= aokikun does not end with san, so print No.

C - Sum of Digits Sequence

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

配点 : 200

問題文

正整数 x に対して、f(x)x の十進表記における各桁の和として定義します。例えば、f(123) = 1 + 2 + 3 = 6 です。

無限数列 A = (A_0, A_1, A_2, \ldots) を以下の式により定義します。

  • A_0 = 1
  • i \geq 1 のとき A_i = \displaystyle\sum_{j = 0}^{i - 1} f(A_j)

正整数 N が与えられます。A_N の値を求めてください。

制約

  • N1 以上 100 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

6

出力例 1

23
  • A_0 = 1
  • A_1 = f(A_0) = 1
  • A_2 = f(A_0) + f(A_1) = 2
  • A_3 = f(A_0) + f(A_1) + f(A_2) = 4
  • A_4 = f(A_0) + f(A_1) + f(A_2) + f(A_3) = 8
  • A_5 = f(A_0) + f(A_1) + f(A_2) + f(A_3) + f(A_4) = 16
  • A_6 = f(A_0) + f(A_1) + f(A_2) + f(A_3) + f(A_4) + f(A_5) = 23

であるため、A_6 = 23 です。


入力例 2

45

出力例 2

427

Score : 200 points

Problem Statement

For a positive integer x, define f(x) as the sum of the digits in the decimal representation of x. For example, f(123) = 1 + 2 + 3 = 6.

Define an infinite sequence A = (A_0, A_1, A_2, \ldots) by the following formula:

  • A_0 = 1
  • For i \geq 1, A_i = \displaystyle\sum_{j = 0}^{i - 1} f(A_j)

You are given a positive integer N. Find the value of A_N.

Constraints

  • N is an integer between 1 and 100, inclusive.

Input

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

N

Output

Print the answer.


Sample Input 1

6

Sample Output 1

23
  • A_0 = 1
  • A_1 = f(A_0) = 1
  • A_2 = f(A_0) + f(A_1) = 2
  • A_3 = f(A_0) + f(A_1) + f(A_2) = 4
  • A_4 = f(A_0) + f(A_1) + f(A_2) + f(A_3) = 8
  • A_5 = f(A_0) + f(A_1) + f(A_2) + f(A_3) + f(A_4) = 16
  • A_6 = f(A_0) + f(A_1) + f(A_2) + f(A_3) + f(A_4) + f(A_5) = 23

Thus, A_6 = 23.


Sample Input 2

45

Sample Output 2

427
D - Integer Division Returns

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

配点 : 200

問題文

-10^{18} 以上 10^{18} 以下の整数 X が与えられるので、\left\lceil \dfrac{X}{10} \right\rceil を出力してください。
ここで、\left\lceil a \right\rceila 以上の整数のうち最小のものを意味します。

制約

  • -10^{18} \leq X \leq 10^{18}
  • X は整数

入力

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

X

出力

\left\lceil \dfrac{X}{10} \right\rceil を整数として出力せよ。


入力例 1

27

出力例 1

3

\frac{27}{10} = 2.7 以上の整数は 3, 4, 5, \dots です。この中で一番小さい整数は 3 なので、\left \lceil \frac{27}{10} \right \rceil = 3 となります。


入力例 2

-13

出力例 2

-1

\frac{-13}{10} = -1.3 以上の整数は、全ての正整数および 0, -1 です。この中で一番小さい整数は -1 なので、\left \lceil \frac{-13}{10} \right \rceil = -1 となります。


入力例 3

40

出力例 3

4

\frac{40}{10} = 4 以上の整数で一番小さい整数は 4 自身です。


入力例 4

-20

出力例 4

-2

入力例 5

123456789123456789

出力例 5

12345678912345679

Score: 200 points

Problem Statement

Given an integer X between -10^{18} and 10^{18}, inclusive, print \left\lceil \dfrac{X}{10} \right\rceil.
Here, \left\lceil a \right\rceil denotes the smallest integer not less than a.

Constraints

  • -10^{18} \leq X \leq 10^{18}
  • X is an integer.

Input

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

X

Output

Print \left\lceil \dfrac{X}{10} \right\rceil as an integer.


Sample Input 1

27

Sample Output 1

3

The integers not less than \frac{27}{10} = 2.7 are 3, 4, 5, \dots. Among these, the smallest is 3, so \left \lceil \frac{27}{10} \right \rceil = 3.


Sample Input 2

-13

Sample Output 2

-1

The integers not less than \frac{-13}{10} = -1.3 are all positive integers, 0, and -1. Among these, the smallest is -1, so \left \lceil \frac{-13}{10} \right \rceil = -1.


Sample Input 3

40

Sample Output 3

4

The smallest integer not less than \frac{40}{10} = 4 is 4 itself.


Sample Input 4

-20

Sample Output 4

-2

Sample Input 5

123456789123456789

Sample Output 5

12345678912345679
E - Number Place

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

配点 : 250

問題文

9\times 9 のマス目 A があり、各マスには 1 以上 9 以下の整数が書き込まれています。
具体的には、 A の上から i 行目、左から j 列目のマスには A_{i,j} が書き込まれています。

A が次の条件をすべてみたしているならば Yes を、そうでないならば No を出力してください。

  • A の各行について、その行に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
  • A の各列について、その列に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
  • A の行を上から 3 行ずつ 3 つに分け、同様に列も左から 3 列ずつ 3 つに分ける。 これによって A9 つの 3\times 3 のマス目に分けたとき、それぞれの 3\times 3 のマス目には 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。

制約

  • 1\leq A_{i,j}\leq 9
  • 入力はすべて整数

入力

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

A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}

出力

マス目 A が問題文の条件をすべてみたすならば Yes を、 そうでないならば No を出力せよ。


入力例 1

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

出力例 1

Yes

マス目 A は次のようになっています。

マス目 A3 つの条件をすべてみたしているため、Yes を出力します。


入力例 2

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

出力例 2

No

マス目 A は次のようになっています。

例えば左上の 3\times 3 のマス目に注目すると 3 つめの条件をみたしていないことが分かるため、No を出力します。


入力例 3

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

出力例 3

No

マス目 A は次のようになっています。

例えば一番左の列に注目すると 2 つめの条件をみたしていないことが分かるため、No を出力します。

Score : 250 points

Problem Statement

There is a 9\times 9 grid A, where each cell contains an integer between 1 and 9, inclusive.
Specifically, the cell at the i-th row from the top and j-th column from the left contains A_{i,j}.

If A satisfies all of the following conditions, print Yes. Otherwise, print No.

  • For each row of A, the nine cells in that row contain each integer from 1 to 9 exactly once.
  • For each column of A, the nine cells in that column contain each integer from 1 to 9 exactly once.
  • Divide the rows of A into three groups, each of three rows, from top to bottom, and similarly divide the columns into three groups, each of three columns, from left to right. Each 3\times 3 grid obtained from A in this way contains each integer from 1 to 9 exactly once.

Constraints

  • 1\leq A_{i,j}\leq 9
  • All input values are integers.

Input

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

A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}

Output

If the grid A satisfies all the conditions in the problem statement, print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

The grid A is shown below.

The grid A satisfies all three conditions, so print Yes.


Sample Input 2

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

Sample Output 2

No

The grid A is shown below.

For example, if you look at the top left 3\times 3 grid, you can see that the third condition is unsatisfied, so print No.


Sample Input 3

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

Sample Output 3

No

The grid A is shown below.

For example, if you look at the leftmost column, you can see that the second condition is unsatisfied, so print No.

F - Snake Numbers

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

配点 : 350

問題文

10 以上の正整数のうち、十進数表記したときに先頭の桁(最も大きい位)の数字がそれ以外のどの桁の数字よりも真に大きくなるようなものを ヘビ数 とよびます。 例えば、31201 はヘビ数ですが、35202 はヘビ数ではありません。

L 以上 R 以下のヘビ数が何個あるか求めてください。

制約

  • 10\leq L \leq R \leq 10^{18}
  • 入力は全て整数

入力

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

L R

出力

答えを出力せよ。


入力例 1

97 210

出力例 1

6

97 以上 210 以下のヘビ数は、97,98,100,200,201,2106 個です。


入力例 2

1000 9999

出力例 2

2025

入力例 3

252509054433933519 760713016476190692

出力例 3

221852052834757

Score : 350 points

Problem Statement

A positive integer not less than 10 whose top digit (the most significant digit) in decimal representation is strictly larger than every other digit in that number is called a Snake number. For example, 31 and 201 are Snake numbers, but 35 and 202 are not.

Find how many Snake numbers exist between L and R, inclusive.

Constraints

  • 10 \leq L \leq R \leq 10^{18}
  • All input values are integers.

Input

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

L R

Output

Print the answer.


Sample Input 1

97 210

Sample Output 1

6

The Snake numbers between 97 and 210, inclusive, are 97, 98, 100, 200, 201, and 210: there are six.


Sample Input 2

1000 9999

Sample Output 2

2025

Sample Input 3

252509054433933519 760713016476190692

Sample Output 3

221852052834757
G - ABC Transform

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

配点 : 400

問題文

A, B, C のみからなる文字列 S が与えられます。

S^{(0)}:=S とし、i=1,2,3,\ldots について S^{(i)}S^{(i-1)} の各文字を ABC, BCA, CAB と同時に置き換えたものと定義します。

以下の Q 個のクエリに答えてください。i 個目のクエリの内容は以下の通りです。

  • S^{(t_i)} の先頭から k_i 文字目を出力せよ。

制約

  • SA, B, C のみからなる長さ 1 以上 10^5 以下の文字列
  • 1 \leq Q \leq 10^5
  • 0 \leq t_i \leq 10^{18}
  • 1 \leq k_i \leq \min(10^{18},\ S^{(t_i)} の長さ)
  • Q, t_i, k_i は整数

入力

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

S
Q
t_1 k_1
t_2 k_2
\hspace{0.4cm}\vdots
t_Q k_Q

出力

Q 個のクエリを添字の昇順に、すなわち与えられる順に処理し、出力ごとに改行せよ。


入力例 1

ABC
4
0 1
1 1
1 3
1 6

出力例 1

A
B
C
B

S^{(0)}=ABC, S^{(1)}=BCCAAB です。

よって各クエリへの答えは順に A, B, C, B となります。


入力例 2

CBBAACCCCC
5
57530144230160008 659279164847814847
29622990657296329 861239705300265164
509705228051901259 994708708957785197
176678501072691541 655134104344481648
827291290937314275 407121144297426665

出力例 2

A
A
C
A
A

Score : 400 points

Problem Statement

You are given a string S consisting of A, B, C.

Let S^{(0)}:=S. For i=1,2,3,\ldots, let S^{(i)} be the result of simultaneously replacing the characters of S^{(i-1)} as follows: ABC, BCA, CAB.

Answer Q queries. The i-th query is as follows.

  • Print the k_i-th character from the beginning of S^{(t_i)}.

Constraints

  • S is a string of length between 1 and 10^5 (inclusive) consisting of A, B, C.
  • 1 \leq Q \leq 10^5
  • 0 \leq t_i \leq 10^{18}
  • 1 \leq k_i \leq \min(10^{18}, the length of S^{(t_i)})
  • Q, t_i, k_i are integers.

Input

Input is given from Standard Input in the following format:

S
Q
t_1 k_1
t_2 k_2
\hspace{0.4cm}\vdots
t_Q k_Q

Output

Process the Q queries in ascending order of index, that is, in the given order. Each answer should be followed by a newline.


Sample Input 1

ABC
4
0 1
1 1
1 3
1 6

Sample Output 1

A
B
C
B

We have S^{(0)}=ABC, S^{(1)}=BCCAAB.

Thus, the answers to the queries are A, B, C, B in the given order.


Sample Input 2

CBBAACCCCC
5
57530144230160008 659279164847814847
29622990657296329 861239705300265164
509705228051901259 994708708957785197
176678501072691541 655134104344481648
827291290937314275 407121144297426665

Sample Output 2

A
A
C
A
A
H - Sugoroku 3

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

配点 : 500

問題文

マス 1 からマス NN 個のマスがあります。はじめ、あなたはマス 1 にいます。

また、マス 1 からマス N-1 にはそれぞれサイコロが置いてあります。マス i のサイコロは 0 以上 A_i 以下の整数を等確率にランダムで出します。(サイコロを振る操作は毎回独立です。)

あなたは、マス N に到達するまで、現在いるマスに置かれているサイコロを振り、出た目の数だけ進むことを繰り返します。厳密に言うと、マス X にいるときにサイコロで Y が出た場合はマス X+Y に移動します。

サイコロを振る回数の期待値 \bmod\ 998244353 を求めてください。

注記

求める期待値は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N-i(1 \le i \le N-1)
  • 入力は全て整数。

入力

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

N
A_1 A_2 \dots A_{N-1}

出力

答えを出力せよ。


入力例 1

3
1 1

出力例 1

4

求める期待値は 4 であるため、4 を出力します。

マス N に到達するまでの流れとしては、以下のようなものが考えられます。

  • マス 11 を出し、マス 2 に移動する。
  • マス 20 を出し、移動しない。
  • マス 21 を出し、マス 3 に移動する。

このようになる確率は \frac{1}{8} です。


入力例 2

5
3 1 2 1

出力例 2

332748122

Score : 500 points

Problem Statement

There are N squares called Square 1 though Square N. You start on Square 1.

Each of the squares from Square 1 through Square N-1 has a die on it. The die on Square i is labeled with the integers from 0 through A_i, each occurring with equal probability. (Die rolls are independent of each other.)

Until you reach Square N, you will repeat rolling a die on the square you are on. Here, if the die on Square x rolls the integer y, you go to Square x+y.

Find the expected value, modulo 998244353, of the number of times you roll a die.

Notes

It can be proved that the sought expected value is always a rational number. Additionally, if that value is represented \frac{P}{Q} using two coprime integers P and Q, there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N-i(1 \le i \le N-1)
  • 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-1}

Output

Print the answer.


Sample Input 1

3
1 1

Sample Output 1

4

The sought expected value is 4, so 4 should be printed.

Here is one possible scenario until reaching Square N:

  • Roll 1 on Square 1, and go to Square 2.
  • Roll 0 on Square 2, and stay there.
  • Roll 1 on Square 2, and go to Square 3.

This scenario occurs with probability \frac{1}{8}.


Sample Input 2

5
3 1 2 1

Sample Output 2

332748122
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