A - "atcoder".substr()

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

配点 : 100

問題文

文字列 atcoderL 文字目から R 文字目までを出力してください。

制約

  • L,R は整数
  • 1 \le L \le R \le 7

入力

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

L R

出力

答えを出力せよ。


入力例 1

3 6

出力例 1

code

atcoder3 文字目から 6 文字目までを出力すると code となります。


入力例 2

4 4

出力例 2

o

入力例 3

1 7

出力例 3

atcoder

Score : 100 points

Problem Statement

Print the L-th through R-th characters of the string atcoder.

Constraints

  • L and R are integers.
  • 1 \le L \le R \le 7

Input

Input is given from Standard Input in the following format:

L R

Output

Print the answer.


Sample Input 1

3 6

Sample Output 1

code

The 3-rd through 6-th characters of atcoder are code.


Sample Input 2

4 4

Sample Output 2

o

Sample Input 3

1 7

Sample Output 3

atcoder
B - Nice Grid

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

配点 : 200

問題文

次の図に示す、各マスが黒または白に塗られた縦 15\times15 列のグリッドにおいて、 上から R 行目、左から C 列目のマスが何色かを出力して下さい。

制約

  • 1 \leq R, C \leq 15
  • R, C は整数

入力

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

R C

出力

図のグリッドにおいて上から R 行目、左から C 列目のマスが黒色の場合は black と、白色の場合は white と出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

3 5

出力例 1

black

図のグリッドにおいて上から 3 行目、左から 5 列目のマスは黒色です。 よって、black と出力します。


入力例 2

4 5

出力例 2

white

図のグリッドにおいて上から 4 行目、左から 5 列目のマスは白色です。 よって、white と出力します。

Score : 200 points

Problem Statement

Print the color of the cell at the R-th row from the top and C-th column from the left in the following grid with 15 vertical rows and 15 horizontal columns.

Constraints

  • 1 \leq R, C \leq 15
  • R and C are integers.

Input

Input is given from Standard Input in the following format:

R C

Output

In the grid above, if the color of the cell at the R-th row from the top and C-th column from the left is black, then print black; if the cell is white, then print white. Note that the judge is case-sensitive.


Sample Input 1

3 5

Sample Output 1

black

In the grid above, the cell at the 3-rd row from the top and 5-th column from the left is black. Thus, black should be printed.


Sample Input 2

4 5

Sample Output 2

white

In the grid above, the cell at the 4-th row from the top and 5-th column from the left is white. Thus, white should be printed.

C - Matrix Reducing

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

配点 : 300

問題文

H_1W_1 列の行列 A と、H_2W_2 列の行列 B が与えられます。

  • 1 \leq i \leq H_1 かつ 1 \leq j \leq W_1 を満たす整数の組 (i, j) について、行列 Ai 行目 j 列目の要素は A_{i, j} です。
  • 1 \leq i \leq H_2 かつ 1 \leq j \leq W_2 を満たす整数の組 (i, j) について、行列 Bi 行目 j 列目の要素は B_{i, j} です。

行列 A に対して、下記の 2 つの操作のうちどちらかを行うことを、好きなだけ( 0 回でも良い)繰り返すことができます。

  • A の行を任意に 1 つ選んで削除する。
  • A の列を任意に 1 つ選んで削除する。

行列 A を行列 B に一致させることができるかどうかを判定して下さい。

制約

  • 1 \leq H_2 \leq H_1 \leq 10
  • 1 \leq W_2 \leq W_1 \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • 1 \leq B_{i, j} \leq 10^9
  • 入力中の値はすべて整数

入力

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

H_1 W_1
A_{1, 1} A_{1, 2} \ldots A_{1, W_1}
A_{2, 1} A_{2, 2} \ldots A_{2, W_1}
\vdots
A_{H_1, 1} A_{H_1, 2} \ldots A_{H_1, W_1}
H_2 W_2
B_{1, 1} B_{1, 2} \ldots B_{1, W_2}
B_{2, 1} B_{2, 2} \ldots B_{2, W_2}
\vdots
B_{H_2, 1} B_{H_2, 2} \ldots B_{H_2, W_2}

出力

行列 A を行列 B に一致させることができる場合は Yes を、 一致させることができない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

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

出力例 1

Yes

初期状態の行列 A から 2 列目を削除すると、行列 A

1 3 4 5
6 8 9 10
11 13 14 15
16 18 19 20

となります。そこからさらに 3 行目を削除すると、行列 A

1 3 4 5
6 8 9 10
16 18 19 20

となります。そこからさらに 1 行目を削除すると、行列 A

6 8 9 10
16 18 19 20

となります。そこからさらに 4 列目を削除すると、行列 A

6 8 9
16 18 19

となります。これは行列 B と一致します。 操作の繰り返しによって行列 A を行列 B に一致させることができるので Yes を出力します。


入力例 2

3 3
1 1 1
1 1 1
1 1 1
1 1
2

出力例 2

No

どのように操作を行っても、 行列 A を行列 B に一致させることはできません。 よって、No を出力します。

Score : 300 points

Problem Statement

You are given a matrix A with H_1 rows and W_1 columns, and a matrix B with H_2 rows and W_2 columns.

  • For all integer pairs (i, j) such that 1 \leq i \leq H_1 and 1 \leq j \leq W_1, the element at the i-th row and j-th column of matrix A is A_{i, j}.
  • For all integer pairs (i, j) such that 1 \leq i \leq H_2 and 1 \leq j \leq W_2, the element at the i-th row and j-th column of matrix B is B_{i, j}.

You may perform the following operations on the matrix A any number of (possibly 0) times in any order:

  • Choose an arbitrary row of A and remove it.
  • Choose an arbitrary column of A and remove it.

Determine if it is possible to make the matrix A equal the matrix B.

Constraints

  • 1 \leq H_2 \leq H_1 \leq 10
  • 1 \leq W_2 \leq W_1 \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • 1 \leq B_{i, j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H_1 W_1
A_{1, 1} A_{1, 2} \ldots A_{1, W_1}
A_{2, 1} A_{2, 2} \ldots A_{2, W_1}
\vdots
A_{H_1, 1} A_{H_1, 2} \ldots A_{H_1, W_1}
H_2 W_2
B_{1, 1} B_{1, 2} \ldots B_{1, W_2}
B_{2, 1} B_{2, 2} \ldots B_{2, W_2}
\vdots
B_{H_2, 1} B_{H_2, 2} \ldots B_{H_2, W_2}

Output

Print Yes if it is possible to make the matrix A equal the matrix B; print No otherwise. Note that the judge is case-sensitive.


Sample Input 1

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

Sample Output 1

Yes

Removing the 2-nd column from the initial A results in:

1 3 4 5
6 8 9 10
11 13 14 15
16 18 19 20

Then, removing the 3-rd row from A results in:

1 3 4 5
6 8 9 10
16 18 19 20

Then, removing the 1-st row from A results in:

6 8 9 10
16 18 19 20

Then, removing the 4-th column from A results in:

6 8 9
16 18 19

Now the matrix equals the matrix B.
Thus, we can make the matrix A equal the matrix B by repeating the operations, so Yes should be printed.


Sample Input 2

3 3
1 1 1
1 1 1
1 1 1
1 1
2

Sample Output 2

No

Regardless of how we perform the operations, we cannot make the matrix A equal the matrix B, so No should be printed.

D - "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
E - Blackout 2

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

配点 : 500

問題文

ある国には N 個の都市と M 個の発電所があります。これらを総称して地点と呼びます。
地点には 1,2,\dots,N+M の番号がつけられており、そのうち都市は地点 1,2,\dots,N で発電所は地点 N+1,N+2,\dots,N+M です。

この国には電線が E 本あり、電線 i ( 1 \le i \le E ) は地点 U_i と地点 V_i を双方向に結びます。
また、ある都市に 電気が通っている とは、ある都市から電線をいくつか辿って少なくともひとつの発電所に辿り着くことができる状態を言います。

今、 Q 個のイベントが起こります。そのうち i ( 1 \le i \le Q ) 番目のイベントでは電線 X_i が切れ、その電線を辿ることができなくなります。一度切れた電線は、その後のイベントにおいても切れたままです。

全てのイベントについて、そのイベントが終わった直後に電気が通っている都市の数を求めてください。

制約

  • 入力は全て整数
  • 1 \le N,M
  • N+M \le 2 \times 10^5
  • 1 \le Q \le E \le 5 \times 10^5
  • 1 \le U_i < V_i \le N+M
  • i \neq j ならば、 U_i \neq U_j または V_i \neq V_j
  • 1 \le X_i \le E
  • X_i は相異なる

入力

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

N M E
U_1 V_1
U_2 V_2
\vdots
U_E V_E
Q
X_1
X_2
\vdots
X_Q

出力

Q 行出力せよ。
そのうち i ( 1 \le i \le Q ) 行目には i 番目のイベントが終わった直後に電気が通っている都市の数を出力せよ。


入力例 1

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

出力例 1

4
4
2
2
2
1

はじめ、全ての都市に電気が通っています。

  • 1 番目のイベントによって地点 5 と地点 10 を結ぶ電線 3 が切れます。
    • これにより、都市 5 に電気が通らなくなり、電気が通っている都市の数は 4 となります。
  • 2 番目のイベントによって地点 2 と地点 9 を結ぶ電線 5 が切れます。
  • 3 番目のイベントによって地点 3 と地点 6 を結ぶ電線 8 が切れます。
    • これにより、都市 2,3 に電気が通らなくなり、電気が通っている都市の数は 2 となります。
  • 4 番目のイベントによって地点 1 と地点 8 を結ぶ電線 10 が切れます。
  • 5 番目のイベントによって地点 4 と地点 10 を結ぶ電線 2 が切れます。
  • 6 番目のイベントによって地点 1 と地点 7 を結ぶ電線 7 が切れます。
    • これにより、都市 1 に電気が通らなくなり、電気が通っている都市の数は 1 となります。

Score : 500 points

Problem Statement

A country has N cities and M power plants, which we collectively call places.
The places are numbered 1,2,\dots,N+M, among which Places 1,2,\dots,N are the cities and Places N+1,N+2,\dots,N+M are the power plants.

This country has E power lines. Power Line i (1 \le i \le E) connects Place U_i and Place V_i bidirectionally.
A city is said to be electrified if one can reach at least one of the power plants from the city using some power lines.

Now, Q events will happen. In the i-th (1 \le i \le Q) event, Power Line X_i breaks, making it unusable. Once a power line breaks, it remains broken in the succeeding events.

Find the number of electrified cities right after each events.

Constraints

  • All values in input are integers.
  • 1 \le N,M
  • N+M \le 2 \times 10^5
  • 1 \le Q \le E \le 5 \times 10^5
  • 1 \le U_i < V_i \le N+M
  • If i \neq j, then U_i \neq U_j or V_i \neq V_j.
  • 1 \le X_i \le E
  • X_i are distinct.

Input

Input is given from Standard Input in the following format:

N M E
U_1 V_1
U_2 V_2
\vdots
U_E V_E
Q
X_1
X_2
\vdots
X_Q

Output

Print Q lines.
The i-th line should contain the number of electrified cities right after the i-th event.


Sample Input 1

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

Sample Output 1

4
4
2
2
2
1

Initially, all cities are electrified.

  • The 1-st event breaks Power Line 3 that connects Point 5 and Point 10.
    • Now City 5 is no longer electrified, while 4 cities remain electrified.
  • The 2-nd event breaks Power Line 5 that connects Point 2 and Point 9.
  • The 3-rd event breaks Power Line 8 that connects Point 3 and Point 6.
    • Now Cities 2 and 3 are no longer electrified, while 2 cities remain electrified.
  • The 4-th event breaks Power Line 10 that connects Point 1 and Point 8.
  • The 5-th event breaks Power Line 2 that connects Point 4 and Point 10.
  • The 6-th event breaks Power Line 7 that connects Point 1 and Point 7.
    • Now City 1 is no longer electrified, while 1 city remains electrified.
F - Monochromatic Path

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

配点 : 500

問題文

各マスが白または黒で塗られた HW 列のグリッドがあります。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) について、 グリッドの上から i 行目、左から j 行目のマス(以下、単にマス (i, j) と呼ぶ)の色は A_{i, j} で表されます。 A_{i, j} = 0 のときマス (i, j) は白色、A_{i, j} = 1 のときマス (i, j) は黒色です。

「下記の 2 つの操作のどちらかを行うこと」を好きなだけ( 0 回でも良い)繰り返すことができます。

  • 1 \leq i \leq H を満たす整数を選び、R_i 円払った後、グリッドの上から i 行目にあるそれぞれのマスの色を反転させる(白色のマスは黒色に、黒色のマスは白色に塗り替える)。
  • 1 \leq j \leq W を満たす整数を選び、C_j 円払った後、グリッドの左から j 列目にあるそれぞれのマスの色を反転させる。

下記の条件を満たすようにするためにかかる合計費用の最小値を出力して下さい。

  • マス (1, 1) から「現在いるマスの右隣もしくは下隣のマスに移動する」 ことを繰り返してマス (H, W) まで移動する経路であって、通るマス(マス (1, 1) とマス (H, W) も含む)の色がすべて同じであるようなものが存在する。

本問題の制約下において、有限回の操作によって上記の条件を満たすようにすることが可能であることが証明できます。

制約

  • 2 \leq H, W \leq 2000
  • 1 \leq R_i \leq 10^9
  • 1 \leq C_j \leq 10^9
  • A_{i, j} \in \lbrace 0, 1\rbrace
  • 入力はすべて整数

入力

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

H W
R_1 R_2 \ldots R_H
C_1 C_2 \ldots C_W
A_{1, 1}A_{1, 2}\ldots A_{1, W}
A_{2, 1}A_{2, 2}\ldots A_{2, W}
\vdots
A_{H, 1}A_{H, 2}\ldots A_{H, W}

出力

答えを出力せよ。


入力例 1

3 4
4 3 5
2 6 7 4
0100
1011
1010

出力例 1

9

白色のマスを 0 、黒色のマスを 1 で表します。 初期状態のグリッドに対し、R_2 = 3 円払って上から 2 行目にあるそれぞれのマスを反転させると、

0100
0100
1010

となります。さらに、C_2 = 6 円払って左から 2 列目にあるそれぞれのマスを反転させると、

0000
0000
1110

となり、マス (1, 1) からマス (3, 4) への移動経路であって通るマスの色がすべて同じであるようなものが存在する状態になります(例えば (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4) という経路)。 かかった合計費用は 3+6 = 9 円であり、このときが考えられる中で最小です。


入力例 2

15 20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78
39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18
01011100110000001111
10101111100010011000
11011000011010001010
00010100011111010100
11111001101010001011
01111001100101011100
10010000001110101110
01001011100100101000
11001000100101011000
01110000111011100101
00111110111110011111
10101111111011101101
11000011000111111001
00011101011110001101
01010000000001000000

出力例 2

125

Score : 500 points

Problem Statement

We have a grid with H rows and W columns. Each square is painted either white or black. For each integer pair (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, the color of the square at the i-th row from the top and j-th column from the left (we simply denote this square by Square (i, j)) is represented by A_{i, j}. Square (i, j) is white if A_{i, j} = 0, and black if A_{i, j} = 1.

You may perform the following operations any number of (possibly 0) times in any order:

  • Choose an integer i such that 1 \leq i \leq H, pay R_i yen (the currency in Japan), and invert the color of each square in the i-th row from the top in the grid. (White squares are painted black, and black squares are painted white.)
  • Choose an integer j such that 1 \leq j \leq W, pay C_j yen, and invert the color of each square in the j-th column from the left in the grid.

Print the minimum total cost to satisfy the following condition:

  • There exists a path from Square (1, 1) to Square (H, W) that can be obtained by repeatedly moving down or to the right, such that all squares in the path (including Square (1, 1) and Square (H, W)) have the same color.

We can prove that it is always possible to satisfy the condition in a finite number of operations under the Constraints of this problem.

Constraints

  • 2 \leq H, W \leq 2000
  • 1 \leq R_i \leq 10^9
  • 1 \leq C_j \leq 10^9
  • A_{i, j} \in \lbrace 0, 1\rbrace
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
R_1 R_2 \ldots R_H
C_1 C_2 \ldots C_W
A_{1, 1}A_{1, 2}\ldots A_{1, W}
A_{2, 1}A_{2, 2}\ldots A_{2, W}
\vdots
A_{H, 1}A_{H, 2}\ldots A_{H, W}

Output

Print the answer.


Sample Input 1

3 4
4 3 5
2 6 7 4
0100
1011
1010

Sample Output 1

9

We denote a white square by 0 and a black square by 1. On the initial grid, you can pay R_2 = 3 yen to invert the color of each square in the 2-nd row from the top to make the grid:

0100
0100
1010

Then, you can pay C_2 = 6 yen to invert the color of each square in the 2-nd row from the left to make the grid:

0000
0000
1110

Now, there exists a path from Square (1, 1) to Square (3, 4) such that all squares in the path have the same color (such as the path (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4)). The total cost paid is 3+6 = 9 yen, which is the minimum possible.


Sample Input 2

15 20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78
39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18
01011100110000001111
10101111100010011000
11011000011010001010
00010100011111010100
11111001101010001011
01111001100101011100
10010000001110101110
01001011100100101000
11001000100101011000
01110000111011100101
00111110111110011111
10101111111011101101
11000011000111111001
00011101011110001101
01010000000001000000

Sample Output 2

125
G - String Fair

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

配点 : 600

問題文

文字列品評会では、英小文字のみからなる空でない文字列 S に対して、その「美しさ」を決定します。

文字列 S の美しさは、N 個の審査項目の点数の和として定められます。 i = 1, 2, \ldots, N について、i 番目の審査項目の点数は 「文字列 T_i(入力で与えられる長さ 3 以下の文字列)が S に連続な部分文字列として含まれる回数」に P_i を掛けて得られる値です。

英小文字のみからなる空でない文字列 S の美しさとしてあり得る最大値を出力して下さい。 ただし、美しさとしていくらでも大きい値が考えられる場合は、代わりに Infinity と出力して下さい。

ここで、文字列 U = U_1U_2\ldots U_{|U|} に文字列 V が連続な部分列として含まれる回数とは、 1 \leq i \leq |U|-|V|+1 を満たす整数 i であって U_iU_{i+1}\ldots U_{i+|V|-1} = V を満たすものの個数です。

制約

  • 1 \leq N \leq 18278
  • N は整数
  • T_i は英小文字のみからなる長さ 1 以上 3 以下の文字列
  • i \neq j \Rightarrow T_i \neq T_j
  • -10^9 \leq P_i \leq 10^9
  • P_i は整数

入力

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

N
T_1 P_1
T_2 P_2
\vdots
T_N P_N

出力

英小文字のみからなる空でない文字列 S の美しさとしてあり得る最大値を出力せよ。 ただし、美しさとしていくらでも大きい値が考えられる場合は、代わりに Infinity と出力せよ。


入力例 1

3
a -5
ab 10
ba -20

出力例 1

Infinity

例えば、S = abzabz について、

  • 1 番目の審査項目の点数は、aS に連続な部分列として含まれる回数が 2 回であることから、2 \times (-5) = -10
  • 2 番目の審査項目の点数は、abS に連続な部分列として含まれる回数が 2 回であることから、2 \times 10 = 20
  • 3 番目の審査項目の点数は、baS に連続な部分列として含まれる回数が 0 回であることから、0 \times (-20) = 0

であるので、S の美しさは (-10) + 20 + 0 = 10 となります。

別の例として、S = abzabzabz について、

  • 1 番目の審査項目の点数は、aS に連続な部分列として含まれる回数が 3 回であることから、3 \times (-5) = -15
  • 2 番目の審査項目の点数は、abS に連続な部分列として含まれる回数が 3 回であることから、3 \times 10 = 30
  • 3 番目の審査項目の点数は、baS に連続な部分列として含まれる回数が 0 回であることから、0 \times (-20) = 0

であるので、S の美しさは (-15) + 30 + 0 = 15 となります。

一般に、正の整数 X について、abzX 回繰り返した文字列の美しさは 5X となります。 S の美しさとしていくらでも大きい値が考えられるため、Infinity を出力します。


入力例 2

28
a -5
ab 10
ba -20
bb -20
bc -20
bd -20
be -20
bf -20
bg -20
bh -20
bi -20
bj -20
bk -20
bl -20
bm -20
bn -20
bo -20
bp -20
bq -20
br -20
bs -20
bt -20
bu -20
bv -20
bw -20
bx -20
by -20
bz -20

出力例 2

5

S = ab が考えられる美しさの最大値を達成します。


入力例 3

26
a -1
b -1
c -1
d -1
e -1
f -1
g -1
h -1
i -1
j -1
k -1
l -1
m -1
n -1
o -1
p -1
q -1
r -1
s -1
t -1
u -1
v -1
w -1
x -1
y -1
z -1

出力例 3

-1

S は空でない文字列でなければならないことに注意して下さい。

Score : 600 points

Problem Statement

In a string fair, they determine the beauty of a non-empty string S consisting of lowercase English letters.

The beauty of string S equals the sum of N scores determined by N criteria. For i = 1, 2, \ldots, N, the score determined by the i-th criterion is "the number of occurrences of a string T_i (of length at most 3 given in the Input) in S as a consecutive subsequence" multiplied by P_i.

Print the maximum possible beauty of a non-empty string S consisting of lowercase English letters. If it is possible to obtain infinitely large beauty, print Infinity instead.

Here, the number of occurrences of a string V in a string U = U_1U_2\ldots U_{|U|} as a consecutive subsequence is defined to be the number of integers i such that 1 \leq i \leq |U|-|V|+1 and U_iU_{i+1}\ldots U_{i+|V|-1} = V.

Constraints

  • 1 \leq N \leq 18278
  • N is an integer.
  • T_i is a string of length between 1 and 3 consisting of lowercase English letters.
  • i \neq j \Rightarrow T_i \neq T_j
  • -10^9 \leq P_i \leq 10^9
  • P_i is an integer.

Input

Input is given from Standard Input in the following format:

N
T_1 P_1
T_2 P_2
\vdots
T_N P_N

Output

Print the maximum possible beauty of a non-empty string S consisting of lowercase English letters. If it is possible to obtain infinitely large beauty, print Infinity instead.


Sample Input 1

3
a -5
ab 10
ba -20

Sample Output 1

Infinity

For example, if S = abzabz:

  • The score determined by the 1-st criterion is 2 \times (-5) = -10 points, because a occurs twice in S as a consecutive subsequence.
  • The score determined by the 2-nd criterion is 2 \times 10 = 20 points, because ab occurs twice in S as a consecutive subsequence.
  • The score determined by the 3-rd criterion is 0 \times (-20) = 0 points, because ba occurs 0 times in S as a consecutive subsequence.

Thus, the beauty of S is (-10) + 20 + 0 = 10.

As another example, if S = abzabzabz:

  • The score determined by the 1-st criterion is 3 \times (-5) = -15 points, because a occurs 3 times in S as a consecutive subsequence.
  • The score determined by the 2-nd criterion is 3 \times 10 = 30 points, because ab occurs 3 times in S as a consecutive subsequence.
  • The score determined by the 3-rd criterion is 0 \times (-20) = 0 points, because ba occurs 0 times in S as a consecutive subsequence.

Thus, the beauty of S is (-15) + 30 + 0 = 15.

In general, for a positive integer X, if S is a concatenation of X copies of abz, then the beauty of S is 5X. Since you can obtain as large beauty as you want, Infinity should be printed.


Sample Input 2

28
a -5
ab 10
ba -20
bb -20
bc -20
bd -20
be -20
bf -20
bg -20
bh -20
bi -20
bj -20
bk -20
bl -20
bm -20
bn -20
bo -20
bp -20
bq -20
br -20
bs -20
bt -20
bu -20
bv -20
bw -20
bx -20
by -20
bz -20

Sample Output 2

5

S = ab achieves the maximum beauty possible.


Sample Input 3

26
a -1
b -1
c -1
d -1
e -1
f -1
g -1
h -1
i -1
j -1
k -1
l -1
m -1
n -1
o -1
p -1
q -1
r -1
s -1
t -1
u -1
v -1
w -1
x -1
y -1
z -1

Sample Output 3

-1

Note that S should be a non-empty string.

Ex - Perfect Binary Tree

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

配点 : 600

問題文

頂点に 1,2,\dots,N の番号が付いた、 N 頂点の根付き木があります。
根は頂点 1 であり、頂点 i \ge 2 について、その親は P_i(<i) です。
整数 k=1,2,\dots,N に対し次の問題を解いてください。

番号が 1 以上 k 以下の頂点を、頂点 1 を含むようにいくつか選ぶ方法は 2^{k-1} 通りあります。
そのうち、選ばれた頂点集合から誘導される部分グラフの形状が頂点 1 を根とする (頂点数がある正整数 d を用いて 2^d-1 と表せるような) 完全二分木になるような頂点の選び方はいくつですか?
答えは非常に大きくなる場合があるので、998244353 で割った余りを求めてください。

誘導される部分グラフとは?

あるグラフ G から、一部の頂点を選んだ集合を S とします。この頂点集合 S から誘導される部分グラフ H は以下のように構成されます。

  • H の頂点集合は S と一致させます。
  • その後、 H に以下のように辺を追加します。
    • i,j \in S, i < j を満たす全ての頂点対 (i,j) について、 G 中に i,j を結ぶ辺が存在すれば、 H にも i,j を結ぶ辺を追加する。
完全二分木とは? 完全二分木とは、以下の全ての条件を満たす根付き木です。
  • 葉以外の全ての頂点が、ちょうど 2 つの子を持つ。
  • 全ての葉が根から等距離にある。
ただし、頂点が 1 つで辺が 0 本のグラフも完全二分木であるものとします。

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 1 \le P_i < i

入力

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

N
P_2 P_3 \dots P_N

出力

N 行出力せよ。 そのうち i ( 1 \le i \le N ) 行目には k=i についての答えを整数として出力せよ。


入力例 1

10
1 1 2 1 2 5 5 5 1

出力例 1

1
1
2
2
4
4
4
5
7
10
  • k \ge 1 であるとき \{1\}
  • k \ge 3 であるとき \{1,2,3\}
  • k \ge 5 であるとき \{1,2,5\},\{1,3,5\}
  • k \ge 8 であるとき \{1,2,4,5,6,7,8\}
  • k \ge 9 であるとき \{1,2,4,5,6,7,9\},\{1,2,4,5,6,8,9\}
  • k = 10 であるとき \{1,2,10\},\{1,3,10\},\{1,5,10\}

が数えるべき頂点の選び方となります。


入力例 2

1

出力例 2

1

N=1 である場合、入力の 2 行目は空行です。


入力例 3

10
1 2 3 4 5 6 7 8 9

出力例 3

1
1
1
1
1
1
1
1
1
1

入力例 4

13
1 1 1 2 2 2 3 3 3 4 4 4

出力例 4

1
1
2
4
4
4
4
4
7
13
13
19
31

Score : 600 points

Problem Statement

We have a rooted tree with N vertices numbered 1,2,\dots,N.
The tree is rooted at Vertex 1, and the parent of Vertex i \ge 2 is Vertex P_i(<i).
For each integer k=1,2,\dots,N, solve the following problem:

There are 2^{k-1} ways to choose some of the vertices numbered between 1 and k so that Vertex 1 is chosen.
How many of them satisfy the following condition: the subgraph induced by the set of chosen vertices forms a perfect binary tree (with 2^d-1 vertices for a positive integer d) rooted at Vertex 1?
Since the count may be enormous, print the count modulo 998244353.

What is an induced subgraph?

Let S be a subset of the vertex set of a graph G. The subgraph H induced by this vertex set S is constructed as follows:

  • Let the vertex set of H equal S.
  • Then, we add edges to H as follows:
    • For all vertex pairs (i, j) such that i,j \in S, i < j, if there is an edge connecting i and j in G, then add an edge connecting i and j to H.
What is a perfect binary tree? A perfect binary tree is a rooted tree that satisfies all of the following conditions:
  • Every vertex that is not a leaf has exactly 2 children.
  • All leaves have the same distance from the root.
Here, we regard a graph with 1 vertex and 0 edges as a perfect binary tree, too.

Constraints

  • All values in input are integers.
  • 1 \le N \le 3 \times 10^5
  • 1 \le P_i < i

Input

Input is given from Standard Input in the following format:

N
P_2 P_3 \dots P_N

Output

Print N lines. The i-th (1 \le i \le N) line should contain the answer as an integer when k=i.


Sample Input 1

10
1 1 2 1 2 5 5 5 1

Sample Output 1

1
1
2
2
4
4
4
5
7
10

The following ways of choosing vertices should be counted:

  • \{1\} when k \ge 1
  • \{1,2,3\} when k \ge 3
  • \{1,2,5\},\{1,3,5\} when k \ge 5
  • \{1,2,4,5,6,7,8\} when k \ge 8
  • \{1,2,4,5,6,7,9\},\{1,2,4,5,6,8,9\} when k \ge 9
  • \{1,2,10\},\{1,3,10\},\{1,5,10\} when k = 10

Sample Input 2

1

Sample Output 2

1

If N=1, the 2-nd line of the Input is empty.


Sample Input 3

10
1 2 3 4 5 6 7 8 9

Sample Output 3

1
1
1
1
1
1
1
1
1
1

Sample Input 4

13
1 1 1 2 2 2 3 3 3 4 4 4

Sample Output 4

1
1
2
4
4
4
4
4
7
13
13
19
31