A - Who Ate the Cake?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君のケーキが誰かに食べられてしまいました。ケーキを食べた犯人の候補として、人 1、人 2、人 3 の三人が挙げられています。

犯人の目撃者はりんごさんとすぬけくんの二人がいます。りんごさんは人 A が犯人でないことを覚えており、すぬけくんは人 B が犯人でないことを覚えています。

二人の記憶から犯人を一人に特定できるかどうか判定し、特定できるならばその人の番号を出力してください。

制約

  • 1\leq A,B\leq 3
  • 入力は全て整数

入力

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

A B

出力

二人の記憶から犯人を一人に特定できるならばその人の番号を出力し、特定できないならば -1 を出力せよ。


入力例 1

1 2

出力例 1

3

二人の記憶から、人 3 が犯人であることが特定できます。


入力例 2

1 1

出力例 2

-1

二人の記憶からでは、人 2,3 のどちらが犯人か特定することができません。よって -1 を出力します。


入力例 3

3 1

出力例 3

2

Score : 100 points

Problem Statement

Takahashi's cake has been eaten by someone. There are three suspects: person 1, person 2, and person 3.

There are two witnesses, Ringo and Snuke. Ringo remembers that person A is not the culprit, and Snuke remembers that person B is not the culprit.

Determine if the culprit can be uniquely identified based on the memories of the two witnesses. If the culprit can be identified, print the person's number.

Constraints

  • 1 \leq A, B \leq 3
  • All input values are integers.

Input

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

A B

Output

If the culprit can be uniquely identified based on the memories of the two witnesses, print the person's number; otherwise, print -1.


Sample Input 1

1 2

Sample Output 1

3

From the memories of the two witnesses, it can be determined that person 3 is the culprit.


Sample Input 2

1 1

Sample Output 2

-1

From the memories of the two witnesses, it cannot be determined whether person 2 or person 3 is the culprit. Therefore, print -1.


Sample Input 3

3 1

Sample Output 3

2
B - Counting Passes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

N 人の人 1,2,\dots,N がある試験を受け、人 iA_i 点を取りました。
この試験では、 L 点以上を取った人のみが合格となります。
N 人のうち何人が合格したか求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 100
  • 1 \le L \le 1000
  • 0 \le A_i \le 1000

入力

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

N L
A_1 A_2 \dots A_N

出力

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


入力例 1

5 60
60 20 100 90 40

出力例 1

3

5 人が試験を受けました。 60 点以上取ると合格です。

  • 160 点を取ったので、合格です。
  • 220 点を取ったので、不合格です。
  • 3100 点を取ったので、合格です。
  • 490 点を取ったので、合格です。
  • 540 点を取ったので、不合格です。

以上より、合格したのは 3 人だと分かります。


入力例 2

4 80
79 78 77 76

出力例 2

0

合格者がいない場合もあります。


入力例 3

10 50
31 41 59 26 53 58 97 93 23 84

出力例 3

6

Score : 100 points

Problem Statement

N people labeled 1,2,\dots,N took an exam, and person i scored A_i points.
Only those who scored at least L points pass this exam.
Determine how many people out of the N have passed the exam.

Constraints

  • All input values are integers.
  • 1 \le N \le 100
  • 1 \le L \le 1000
  • 0 \le A_i \le 1000

Input

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

N L
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5 60
60 20 100 90 40

Sample Output 1

3

Five people took the exam. You need to score at least 60 points to pass.

  • Person 1 scored 60 points, so they passed.
  • Person 2 scored 20 points, so they did not pass.
  • Person 3 scored 100 points, so they passed.
  • Person 4 scored 90 points, so they passed.
  • Person 5 scored 40 points, so they did not pass.

From the above, we can see that three people have passed.


Sample Input 2

4 80
79 78 77 76

Sample Output 2

0

There may be cases no one has passed.


Sample Input 3

10 50
31 41 59 26 53 58 97 93 23 84

Sample Output 3

6
C - Climbing Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 個の台が一列に並んでおり、左から i 番目の台の高さは H_i です。

高橋君は最初、左端の台の上に立っています。

高橋君は高い所が好きなので、次のルールで可能な限り移動を繰り返します。

  • いま立っているのが右端の台ではなく、かつ、右隣にある台の高さが自分がいま立っている台より高いとき、右隣の台に移動する

最終的に高橋君が立っている台の高さを求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq H_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

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

N
H_1 \ldots H_N

出力

答えを出力せよ。


入力例 1

5
1 5 10 4 2

出力例 1

10

最初、高橋君は左端にある高さ 1 の台に立っています。右隣の台の高さは 5 であり、いま立っている台より高いので、右隣の台に移動します。

移動後、高橋君は左から 2 番目にある高さ 5 の台に立っています。右隣の台の高さは 10 であり、いま立っている台より高いので、右隣の台に移動します。

移動後、高橋君は左から 3 番目にある高さ 10 の台に立っています。右隣の台の高さは 4 であり、いま立っている台より低いので、高橋君は移動をやめます。

よって、最終的に高橋君が立っている台の高さは 10 です。


入力例 2

3
100 1000 100000

出力例 2

100000

入力例 3

4
27 1828 1828 9242

出力例 3

1828

Score : 200 points

Problem Statement

There are N platforms arranged in a row. The height of the i-th platform from the left is H_i.

Takahashi is initially standing on the leftmost platform.

Since he likes heights, he will repeat the following move as long as possible.

  • If the platform he is standing on is not the rightmost one, and the next platform to the right has a height greater than that of the current platform, step onto the next platform.

Find the height of the final platform he will stand on.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq H_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H_1 \ldots H_N

Output

Print the answer.


Sample Input 1

5
1 5 10 4 2

Sample Output 1

10

Takahashi is initially standing on the leftmost platform, whose height is 1. The next platform to the right has a height of 5 and is higher than the current platform, so he steps onto it.

He is now standing on the 2-nd platform from the left, whose height is 5. The next platform to the right has a height of 10 and is higher than the current platform, so he steps onto it.

He is now standing on the 3-rd platform from the left, whose height is 10. The next platform to the right has a height of 4 and is lower than the current platform, so he stops moving.

Thus, the height of the final platform Takahashi will stand on is 10.


Sample Input 2

3
100 1000 100000

Sample Output 2

100000

Sample Input 3

4
27 1828 1828 9242

Sample Output 3

1828
D - Adjacency List

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

1, \dots, N と番号付けられた N 個の都市と、都市間を結ぶ M 本の道路があります。
i \, (1 \leq i \leq M) 番目の道路は都市 A_i と都市 B_i を結んでいます。

以下の指示に従い、N 行にわたって出力してください。

  • 都市 i \, (1 \leq i \leq N) と道路で直接結ばれた都市が d_i 個あるとし、それらを昇順に都市 a_{i, 1}, \dots, a_{i, d_i} とおく。
  • i \, (1 \leq i \leq N) 行目には、d_i + 1 個の整数 d_i, a_{i, 1}, \dots, a_{i, d_i} を、この順番で空白区切りで出力せよ。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (i \neq j) ならば (A_i, B_i) \neq (A_j, B_j)
  • 入力される値は全て整数

入力

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

N M
A_1 B_1
\vdots
A_M B_M

出力

問題文の指示に従い、N 行にわたって出力せよ。


入力例 1

6 6
3 6
1 3
5 6
2 5
1 2
1 6

出力例 1

3 2 3 6
2 1 5
2 1 6
0
2 2 6
3 1 3 5

都市 1 と道路で直接結ばれているのは都市 2, 3, 6 です。よって、d_1 = 3, a_{1, 1} = 2, a_{1, 2} = 3, a_{1, 3} = 6 であるので、1 行目には 3, 2, 3, 6 をこの順番で空白区切りで出力します。

a_{i, 1}, \dots, a_{i, d_i} は昇順に並んでいなければならないことに注意してください。例えば、1 行目に 3, 3, 2, 6 をこの順番で出力した場合、不正解となります。


入力例 2

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

出力例 2

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

Score : 200 points

Problem Statement

There are N cities numbered 1, \dots, N, and M roads connecting cities.
The i-th road (1 \leq i \leq M) connects city A_i and city B_i.

Print N lines as follows.

  • Let d_i be the number of cities directly connected to city i \, (1 \leq i \leq N), and those cities be city a_{i, 1}, \dots, city a_{i, d_i}, in ascending order.
  • The i-th line (1 \leq i \leq N) should contain d_i + 1 integers d_i, a_{i, 1}, \dots, a_{i, d_i} in this order, separated by spaces.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (A_i, B_i) \neq (A_j, B_j) if (i \neq j).
  • All values in the input are integers.

Input

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

N M
A_1 B_1
\vdots
A_M B_M

Output

Print N lines as specified in the Problem Statement.


Sample Input 1

6 6
3 6
1 3
5 6
2 5
1 2
1 6

Sample Output 1

3 2 3 6
2 1 5
2 1 6
0
2 2 6
3 1 3 5

The cities directly connected to city 1 are city 2, city 3, and city 6. Thus, we have d_1 = 3, a_{1, 1} = 2, a_{1, 2} = 3, a_{1, 3} = 6, so you should print 3, 2, 3, 6 in the first line in this order, separated by spaces.

Note that a_{i, 1}, \dots, a_{i, d_i} must be in ascending order. For instance, it is unacceptable to print 3, 3, 2, 6 in the first line in this order.


Sample Input 2

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

Sample Output 2

4 2 3 4 5
4 1 3 4 5
4 1 2 4 5
4 1 2 3 5
4 1 2 3 4
E - Sowing Stones

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1 から N まで番号がつけられた N 個のマスが一列に並んでいます。最初、 M 個のマスに石が入っており、マス X_i には A_i(1 \leq i \leq M) の石が入っています。

あなたは以下の操作を好きな回数( 0 回でもよい)行うことができます。

  • マス i (1 \leq i \leq N-1) に石があるとき、マス i から石を 1 つマス i+1 に移動させる。

N 個のマスすべてに石がちょうど 1 個ずつ入っている状態にするために必要な操作回数の最小値を求めてください。ただし、不可能な場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^{9}
  • 1 \leq M \leq 2 \times 10^{5}
  • M \leq N
  • 1 \leq X_i \leq N (1 \leq i \leq M)
  • X_i \neq X_j (1 \leq i < j \leq M)
  • 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
  • 入力は全て整数

入力

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

N M
X_1 X_2 \ldots X_M
A_1 A_2 \ldots A_M

出力

答えを出力せよ。


入力例 1

5 2
1 4
3 2

出力例 1

4

以下の 4 回の操作で、5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることができます。

  • マス 1 の石を 1 つマス 2 に移動させる。
  • マス 2 の石を 1 つマス 3 に移動させる。
  • マス 4 の石を 1 つマス 5 に移動させる。
  • マス 1 の石を 1 つマス 2 に移動させる。

また、3 回以下の操作では 5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、4 を出力します。


入力例 2

10 3
1 4 8
4 2 4

出力例 2

-1

どのように操作を行っても 10 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、-1 を出力します。

Score : 300 points

Problem Statement

There are N cells numbered from 1 to N in a row. Initially, M cells contain stones, and cell X_i contains A_i stones (1 \leq i \leq M).

You can perform the following operation any number of times (possibly zero):

  • If cell i (1 \leq i \leq N-1) contains a stone, move one stone from cell i to cell i+1.

Find the minimum number of operations required to reach a state where each of the N cells contains exactly one stone. If it is impossible, print -1.

Constraints

  • 2 \leq N \leq 2 \times 10^{9}
  • 1 \leq M \leq 2 \times 10^{5}
  • M \leq N
  • 1 \leq X_i \leq N (1 \leq i \leq M)
  • X_i \neq X_j (1 \leq i < j \leq M)
  • 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
  • All input values are integers.

Input

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

N M
X_1 X_2 \ldots X_M
A_1 A_2 \ldots A_M

Output

Print the answer.


Sample Input 1

5 2
1 4
3 2

Sample Output 1

4

You can reach a state where each of the five cells contains exactly one stone with four operations as follows:

  • Move one stone from cell 1 to cell 2.
  • Move one stone from cell 2 to cell 3.
  • Move one stone from cell 4 to cell 5.
  • Move one stone from cell 1 to cell 2.

It is impossible to achieve the goal in three or fewer operations. Therefore, print 4.


Sample Input 2

10 3
1 4 8
4 2 4

Sample Output 2

-1

No matter how you perform the operations, you cannot reach a state where all ten cells contain exactly one stone. Therefore, print -1.

F - Connect 6

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

NN 列のマス目があり、それぞれのマスは白または黒で塗られています。
マス目の状態は N 個の文字列 S_i で表され、 S_ij 文字目が # であることはマス目の上から i 行目、左から j 列目のマスが黒く塗られていることを、 . であることは白く塗られていることをさします。

高橋君はこのマス目のうち高々 2 つの白く塗られているマスを選び、黒く塗ることができます。
マス目の中に、黒く塗られたマスが縦、横、ななめのいずれかの向きに 6 つ以上連続するようにできるか判定してください。
ただし、黒く塗られたマスがななめに 6 つ以上連続するとは、NN 列のマス目に完全に含まれる 66 列のマス目であって、その少なくとも一方の対角線上のマスがすべて黒く塗られているようなものが存在する事をさします。

制約

  • 6 \leq N \leq 1000
  • \lvert S_i\rvert =N
  • S_i#. のみからなる。

入力

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

N
S_1
S_2
\vdots
S_N

出力

高々 2 つのマス目を黒く塗ることで条件をみたすようにできるなら Yes を、そうでないならば No を出力せよ。


入力例 1

8
........
........
.#.##.#.
........
........
........
........
........

出力例 1

Yes

上から 3 行目の左から 3, 6 番目のマスを塗ることで横方向に 6 つの黒く塗られたマスを連続させることができます。


入力例 2

6
######
######
######
######
######
######

出力例 2

Yes

高橋君はマス目を新たに黒く塗ることはできませんが、すでにこのマス目は条件をみたしています。


入力例 3

10
..........
#..##.....
..........
..........
....#.....
....#.....
.#...#..#.
..........
..........
..........

出力例 3

No

Score : 300 points

Problem Statement

There is a grid with N horizontal rows and N vertical columns, where each square is painted white or black.
The state of the grid is represented by N strings, S_i. If the j-th character of S_i is #, the square at the i-th row from the top and the j-th column from the left is painted black. If the character is ., then the square is painted white.

Takahashi can choose at most two of these squares that are painted white, and paint them black.
Determine if it is possible to make the grid contain 6 or more consecutive squares painted black aligned either vertically, horizontally, or diagonally.
Here, the grid is said to be containing 6 or more consecutive squares painted black aligned diagonally if the grid with N rows and N columns completely contains a subgrid with 6 rows and 6 columns such that all the squares on at least one of its diagonals are painted black.

Constraints

  • 6 \leq N \leq 1000
  • \lvert S_i\rvert =N
  • S_i consists of # and ..

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

If it is possible to fulfill the condition by painting at most two squares, then print Yes; otherwise, print No.


Sample Input 1

8
........
........
.#.##.#.
........
........
........
........
........

Sample Output 1

Yes

By painting the 3-rd and the 6-th squares from the left in the 3-rd row from the top, there will be 6 squares painted black aligned horizontally.


Sample Input 2

6
######
######
######
######
######
######

Sample Output 2

Yes

While Takahashi cannot choose a square to paint black, the grid already satisfies the condition.


Sample Input 3

10
..........
#..##.....
..........
..........
....#.....
....#.....
.#...#..#.
..........
..........
..........

Sample Output 3

No
G - Cross Explosion

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と表します。
はじめ、全てのマスには壁が 1 個ずつ立てられています。
以下で説明されるクエリを Q 個与えられる順に処理した後に、残っている壁の個数を求めてください。

q 番目のクエリでは整数 R_q, C_q が与えられます。
あなたは (R_q, C_q) に爆弾を置いて壁を爆破します。その結果、以下の処理が行われます。

  • (R_q, C_q) に壁が存在する場合は、その壁を破壊して処理を終了する。
  • (R_q, C_q) に壁が存在しない場合は、(R_q, C_q) から上下左右に見て最初に現れる壁を破壊する。厳密に述べると、以下の 4 個の処理が同時に行われる。
    • (i, C_q) に壁が存在して (k, C_q) (i \lt k \lt R_q) に壁が存在しないような i \lt R_q が存在する時、(i, C_q) に存在する壁を破壊する。
    • (i, C_q) に壁が存在して (k, C_q) (R_q \lt k \lt i) に壁が存在しないような i \gt R_q が存在する時、(i, C_q) に存在する壁を破壊する。
    • (R_q, j) に壁が存在して (R_q, k) (j \lt k \lt C_q) に壁が存在しないような j \lt C_q が存在する時、(R_q, j) に存在する壁を破壊する。
    • (R_q, j) に壁が存在して (R_q, k) (C_q \lt k \lt j) に壁が存在しないような j \gt C_q が存在する時、(R_q, j) に存在する壁を破壊する。

制約

  • 1 \leq H, W
  • H \times W \leq 4 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq R_q \leq H
  • 1 \leq C_q \leq W
  • 入力される値は全て整数

入力

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

H W Q
R_1 C_1
R_2 C_2
\vdots
R_Q C_Q

出力

クエリを全て処理した後に、残っている壁の個数を出力せよ。


入力例 1

2 4 3
1 2
1 2
1 3

出力例 1

2

クエリを処理する手順を説明すると次のようになります。

  • 1 番目のクエリでは (R_1, C_1) = (1, 2) である。 (1, 2) に壁は存在するので (1, 2) の壁が爆破される。
  • 2 番目のクエリでは (R_2, C_2) = (1, 2) である。 (1, 2) に壁は存在しないので、(1, 2) から上下左右に見て最初に現れる壁である (2,2),(1,1),(1,3) が爆破される。
  • 3 番目のクエリでは (R_2, C_2) = (1, 3) である。 (1, 3) に壁は存在しないので、(1, 3) から上下左右に見て最初に現れる壁である (2,3),(1,4) が爆破される。

全てのクエリを処理した後に残っている壁は (2, 1), (2, 4)2 個です。


入力例 2

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

出力例 2

10

入力例 3

4 3 10
2 2
4 1
1 1
4 2
2 1
3 1
1 3
1 2
4 3
4 2

出力例 3

2

Score : 400 points

Problem Statement

There is a grid with 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.
Initially, there is one wall in each cell.
After processing Q queries explained below in the order they are given, find the number of remaining walls.

In the q-th query, you are given two integers R_q and C_q.
You place a bomb at (R_q, C_q) to destroy walls. As a result, the following process occurs.

  • If there is a wall at (R_q, C_q), destroy that wall and end the process.
  • If there is no wall at (R_q, C_q), destroy the first walls that appear when looking up, down, left, and right from (R_q, C_q). More precisely, the following four processes occur simultaneously:
    • If there exists an i \lt R_q such that a wall exists at (i, C_q) and no wall exists at (k, C_q) for all i \lt k \lt R_q, destroy the wall at (i, C_q).
    • If there exists an i \gt R_q such that a wall exists at (i, C_q) and no wall exists at (k, C_q) for all R_q \lt k \lt i, destroy the wall at (i, C_q).
    • If there exists a j \lt C_q such that a wall exists at (R_q, j) and no wall exists at (R_q, k) for all j \lt k \lt C_q, destroy the wall at (R_q, j).
    • If there exists a j \gt C_q such that a wall exists at (R_q, j) and no wall exists at (R_q, k) for all C_q \lt k \lt j, destroy the wall at (R_q, j).

Constraints

  • 1 \leq H, W
  • H \times W \leq 4 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq R_q \leq H
  • 1 \leq C_q \leq W
  • All input values are integers.

Input

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

H W Q
R_1 C_1
R_2 C_2
\vdots
R_Q C_Q

Output

Print the number of remaining walls after processing all queries.


Sample Input 1

2 4 3
1 2
1 2
1 3

Sample Output 1

2

The process of handling the queries can be explained as follows:

  • In the 1st query, (R_1, C_1) = (1, 2). There is a wall at (1, 2), so the wall at (1, 2) is destroyed.
  • In the 2nd query, (R_2, C_2) = (1, 2). There is no wall at (1, 2), so the walls at (2,2),(1,1),(1,3), which are the first walls that appear when looking up, down, left, and right from (1, 2), are destroyed.
  • In the 3rd query, (R_3, C_3) = (1, 3). There is no wall at (1, 3), so the walls at (2,3),(1,4), which are the first walls that appear when looking up, down, left, and right from (1, 3), are destroyed.

After processing all queries, there are two remaining walls, at (2, 1) and (2, 4).


Sample Input 2

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

Sample Output 2

10

Sample Input 3

4 3 10
2 2
4 1
1 1
4 2
2 1
3 1
1 3
1 2
4 3
4 2

Sample Output 3

2
H - Duplicate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

1 から 9 までの数字からなる文字列 S に対して、 f(S) を次の手順によって得られる文字列 T とします。(S_iSi 番目の文字を意味します)

  • 文字列 T がある。はじめ、T は空文字列である。
  • i=1, 2, \dots, |S| - 1 の順に次の操作を行う。
    • S_{i+1} を整数として解釈したときの値を n とする。T の末尾に S_in 個追加する。

例えば S = 313 のとき、以下の手順によって f(S) = 3111 に決まります。

  • はじめ T は空文字列である。
  • i=1 のとき n = 1 である。T31 個追加する。T3 になる。
  • i=2 のとき n = 3 である。T13 個追加する。T3111 になる。
  • 操作を終了する。T として 3111 を得る。

1 から 9 までの数字からなる長さ N の文字列 S が与えられます。
あなたは「Sf(S) に置き換える」という操作を S の長さが 1 になるまで繰り返します。
操作が終了するまでに行う操作を行う回数を 998244353 で割った余りを求めてください。ただし、操作が無限に続く場合は -1 を出力してください。

制約

  • 2 \leq N \leq 10^6
  • S1, 2, 3, 4, 5, 6, 7, 8, 9 からなる長さ N の文字列

入力

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

N
S

出力

操作が終了するまでに行う操作を行う回数を 998244353 で割った余りを出力せよ。ただし、操作が無限に続く場合は -1 を出力せよ。


入力例 1

3
313

出力例 1

4

S = 313 の場合、操作を 4 回行うと S の長さが 1 になります。

  • f(S) = 3111 である。S3111 に置き換える。
  • f(S) = 311 である。S311 に置き換える。
  • f(S) = 31 である。S31 に置き換える。
  • f(S) = 3 である。S3 に置き換える。
  • S の長さが 1 になったので操作を終了する。

入力例 2

9
123456789

出力例 2

-1

S = 123456789 の場合、操作が無限に続きます。この場合は -1 を出力してください。


入力例 3

2
11

出力例 3

1

Score : 600 points

Problem Statement

For a string S consisting of digits from 1 through 9, let f(S) be the string T obtained by the following procedure. (S_i denotes the i-th character of S.)

  • Let T be an initially empty string.
  • For i=1, 2, \dots, |S| - 1, perform the following operation:
    • Append n copies of S_i to the tail of T, where n is the value when S_{i+1} is interpreted as an integer.

For example, S = 313 yields f(S) = 3111 by the following steps.

  • T is initially empty.
  • For i=1, we have n = 1. Append one copy of 3 to T, which becomes 3.
  • For i=2, we have n = 3. Append three copies of 1 to T, which becomes 3111.
  • Terminate the procedure. We obtain T = 3111.

You are given a length-N string S consisting of digits from 1 through 9.
You repeat the following operation until the length of S becomes 1: replace S with f(S).
Find how many times, modulo 998244353, you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1 instead.

Constraints

  • 2 \leq N \leq 10^6
  • S is a length-N string consisting of 1, 2, 3, 4, 5, 6, 7, 8, and 9.

Input

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

N
S

Output

Print the number of times, modulo 998244353, that you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1 instead.


Sample Input 1

3
313

Sample Output 1

4

If S = 313, the length of S be comes 1 after four operations.

  • We have f(S) = 3111. Replace S with 3111.
  • We have f(S) = 311. Replace S with 311.
  • We have f(S) = 31. Replace S with 31.
  • We have f(S) = 3. Replace S with 3.
  • Now that the length of S is 1, terminate the repetition.

Sample Input 2

9
123456789

Sample Output 2

-1

If S = 123456789, you indefinitely repeat the operation. In this case, -1 should be printed.


Sample Input 3

2
11

Sample Output 3

1
I - Permutation Distance

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

(1,2,\ldots,N) の順列 P=(P _ 1,P _ 2,\ldots,P _ N) が与えられます。

すべての i\ (1\leq i\leq N) に対して、以下の値を求めてください。

  • D _ i=\displaystyle\min_{j\neq i}\left\lparen\left\lvert P _ i-P _ j\right\rvert+\left\lvert i-j\right\rvert\right\rparen
順列とは (1,2,\ldots,N) の順列とは、(1,2,\ldots,N) を並べ替えて得られる数列のことをいいます。 つまり、長さ N の数列 Ai\ (1\leq i\leq N) がその中にちょうど 1 回だけ現れるとき、かつそのときに限り(1,2,\ldots,N) の順列です。

制約

  • 2 \leq N \leq 2\times10^5
  • 1 \leq P _ i \leq N\ (1\leq i\leq N)
  • i\neq j\implies P _ i\neq P _ j
  • 入力はすべて整数

入力

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

N
P _ 1 P _ 2 \ldots P _ N

出力

D _ i\ (1\leq i\leq N)i の昇順に空白区切りで出力せよ。


入力例 1

4
3 2 4 1

出力例 1

2 2 3 3 

たとえば、i=1 について

  • j=2 のとき、\left\lvert P _ i-P _ j\right\rvert=1,\left\lvert i-j\right\rvert=1 です。
  • j=3 のとき、\left\lvert P _ i-P _ j\right\rvert=1,\left\lvert i-j\right\rvert=2 です。
  • j=4 のとき、\left\lvert P _ i-P _ j\right\rvert=2,\left\lvert i-j\right\rvert=3 です。

よって、j=2 のとき \left\lvert P _ i-P _ j\right\rvert+\left\lvert i-j\right\rvert=2 で最小となるので、D _ 1=2 です。


入力例 2

7
1 2 3 4 5 6 7

出力例 2

2 2 2 2 2 2 2 

入力例 3

16
12 10 7 14 8 3 11 13 2 5 6 16 4 1 15 9

出力例 3

3 3 3 5 3 4 3 3 4 2 2 4 4 4 4 7 

Score : 500 points

Problem Statement

You are given a permutation P=(P _ 1,P _ 2,\ldots,P _ N) of (1,2,\ldots,N).

Find the following value for all i\ (1\leq i\leq N):

  • D _ i=\displaystyle\min_{j\neq i}\left\lparen\left\lvert P _ i-P _ j\right\rvert+\left\lvert i-j\right\rvert\right\rparen.
What is a permutation? A permutation of (1,2,\ldots,N) is a sequence that is obtained by rearranging (1,2,\ldots,N). In other words, a sequence A of length N is a permutation of (1,2,\ldots,N) if and only if each i\ (1\leq i\leq N) occurs in A exactly once.

Constraints

  • 2 \leq N \leq 2\times10^5
  • 1 \leq P _ i \leq N\ (1\leq i\leq N)
  • i\neq j\implies P _ i\neq P _ j
  • All values in the input are integers.

Input

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

N
P _ 1 P _ 2 \ldots P _ N

Output

Print D _ i\ (1\leq i\leq N) in ascending order of i, separated by spaces.


Sample Input 1

4
3 2 4 1

Sample Output 1

2 2 3 3 

For example, for i=1,

  • if j=2, we have \left\lvert P _ i-P _ j\right\rvert=1 and \left\lvert i-j\right\rvert=1;
  • if j=3, we have \left\lvert P _ i-P _ j\right\rvert=1 and \left\lvert i-j\right\rvert=2;
  • if j=4, we have \left\lvert P _ i-P _ j\right\rvert=2 and \left\lvert i-j\right\rvert=3.

Thus, the value is minimum when j=2, where \left\lvert P _ i-P _ j\right\rvert+\left\lvert i-j\right\rvert=2, so D _ 1=2.


Sample Input 2

7
1 2 3 4 5 6 7

Sample Output 2

2 2 2 2 2 2 2 

Sample Input 3

16
12 10 7 14 8 3 11 13 2 5 6 16 4 1 15 9

Sample Output 3

3 3 3 5 3 4 3 3 4 2 2 4 4 4 4 7