A - 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
B - ABC400 Party

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

ABC400 を記念した式典において、400 人の高橋君を AB 列の長方形状に隙間なく並べようとしています。

正整数 A が与えられるので、このような並べ方ができるような正整数 B の値を出力してください。ただし、そのような正整数 B が存在しない場合には -1 を出力してください。

制約

  • A1 以上 400 以下の整数

入力

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

A

出力

問題文の指示に従って B の値あるいは -1 を出力せよ。


入力例 1

10

出力例 1

40

400 人の高橋君を 1040 列の長方形状に並べることができます。


入力例 2

11

出力例 2

-1

入力例 3

400

出力例 3

1

Score : 100 points

Problem Statement

In the ceremony commemorating ABC400, we want to arrange 400 people in a rectangular formation of A rows and B columns without any gaps.

You are given a positive integer A. Print the value of a positive integer B for which such an arrangement is possible. If there is no such positive integer B, print -1.

Constraints

  • A is an integer between 1 and 400, inclusive.

Input

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

A

Output

Print the value of B or -1 as specified by the problem statement.


Sample Input 1

10

Sample Output 1

40

We can arrange 400 people in 10 rows and 40 columns.


Sample Input 2

11

Sample Output 2

-1

Sample Input 3

400

Sample Output 3

1
C - Japanese Cursed Doll

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の人がおり、i 人目 (1\leq i\leq N) の現在の髪の長さは L_i です。
すべての人は 1 日経つごとに髪の長さが 1 ずつ増えます。

髪の長さが T 以上の人が初めて P 人以上になるのは現在から何日後か出力してください。
ただし、現在の時点ですでに髪の長さが T 以上の人が P 人以上にいる場合は 0 を出力してください。

制約

  • 1\leq N\leq 100
  • 1\leq L_i\leq 100
  • 1\leq T\leq 100
  • 1\leq P\leq N
  • 入力はすべて整数

入力

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

N T P
L_1 L_2 \ldots L_N

出力

髪の長さが T 以上の人が初めて P 人以上になるのは現在から何日後か出力せよ。 ただし、現在の時点ですでに条件をみたしている場合は 0 を出力せよ。


入力例 1

5 10 3
3 11 1 6 2

出力例 1

7

5 人の人がおり、現在の時点で髪の長さはそれぞれ 3,11,1,6,2 であるため、髪の長さが 10 以上の人は 1 人です。

現在から 7 日後にはそれぞれの人の髪の長さは順に 10,18,8,13,9 となり、髪の長さが 10 以上の人は 3 人となります。
現在から 6 日後の時点では髪の長さが 10 以上の人は 2 人であるため条件をみたしておらず、よって 7 を出力します。


入力例 2

2 5 2
10 10

出力例 2

0

現在の時点ですでに髪の長さが 5 以上の人が 2 人いるため条件をみたしており、0 を出力します。


入力例 3

3 10 1
1 2 3

出力例 3

7

Score : 200 points

Problem Statement

There are N people, and the current hair length of the i-th person (1 \leq i \leq N) is L_i.
Each person's hair grows by 1 per day.

Print the number of days after which the number of people whose hair length is at least T becomes P or more for the first time.
If there are already P or more people whose hair length is at least T now, print 0.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq L_i \leq 100
  • 1 \leq T \leq 100
  • 1 \leq P \leq N
  • All input values are integers.

Input

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

N T P
L_1 L_2 \ldots L_N

Output

Print the number of days after which the number of people whose hair length is at least T becomes P or more for the first time. If this condition is already satisfied now, print 0.


Sample Input 1

5 10 3
3 11 1 6 2

Sample Output 1

7

There are five people, and their current hair lengths are 3, 11, 1, 6, 2, so there is one person whose hair length is at least 10.

After seven days, the hair lengths of the people will be 10, 18, 8, 13, 9, respectively, and there will be three people whose hair length is at least 10.
After six days, there are only two people whose hair length is at least 10, not satisfying the condition, so print 7.


Sample Input 2

2 5 2
10 10

Sample Output 2

0

Since there are already two people whose hair length is at least 5 now, satisfying the condition, so print 0.


Sample Input 3

3 10 1
1 2 3

Sample Output 3

7
D - TaK Code

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は 2 次元コード TaK Code を考案しました。以下の条件を全て満たすものが TaK Code です。

  • 9 マス 横 9 マスの領域である
  • 左上及び右下の縦 3 マス 横 3 マスの領域に含まれる計 18 マスは全て黒である
  • 左上及び右下の縦 3 マス 横 3 マスの領域に八方位で隣接する計 14 マスは全て白である

TaK Code を回転させることはできません。

N マス、横 M マスのグリッドがあります。 グリッドの状態は N 個の長さ M の文字列 S_1,\ldots,S_N で与えられ、上から i 行目左から j 列目のマスは S_ij 文字目が # のとき黒、. のとき白です。

グリッドに完全に含まれる縦 9 マス横 9 マスの領域で、TaK Code の条件を満たす箇所を全て求めてください。

制約

  • 9 \leq N,M \leq 100
  • N,M は整数である
  • S_i. および # のみからなる長さ M の文字列である

入力

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

N M
S_1
\vdots
S_N

出力

上から i 行目左から j 列目のマスを左上とする縦 9 マス 横 9 マスの領域が TaK Code の条件を満たす (i,j) の組全てを辞書順の昇順で 1 行に 1 組ずつ、i,j をこの順に空白区切りで出力せよ。
(i,j) の組の辞書順の昇順とは、i の昇順、i が等しい組は j の昇順にすることである。


入力例 1

19 18
###......###......
###......###......
###..#...###..#...
..............#...
..................
..................
......###......###
......###......###
......###......###
.###..............
.###......##......
.###..............
............###...
...##.......###...
...##.......###...
.......###........
.......###........
.......###........
........#.........

出力例 1

1 1
1 10
7 7
10 2

TaK Code は以下のものです。# が黒マス、. が白マス、? が白黒どちらでもよいマスを表します。

###.?????
###.?????
###.?????
....?????
?????????
?????....
?????.###
?????.###
?????.###

入力で与えられたグリッドの上から 10 マス目左から 2 列目のマスを左上とする縦 9 マス 横 9 マスの領域は以下のようになっており、TaK Code の条件を満たします。

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

入力例 2

9 21
###.#...........#.###
###.#...........#.###
###.#...........#.###
....#...........#....
#########...#########
....#...........#....
....#.###...###.#....
....#.###...###.#....
....#.###...###.#....

出力例 2

1 1

入力例 3

18 18
######............
######............
######............
######............
######............
######............
..................
..................
..................
..................
..................
..................
............######
............######
............######
............######
............######
............######

出力例 3


TaK Code の条件を満たす箇所が 1 つもないこともあります。

Score : 200 points

Problem Statement

Takahashi invented Tak Code, a two-dimensional code. A TaK Code satisfies all of the following conditions:

  • It is a region consisting of nine horizontal rows and nine vertical columns.
  • All the 18 cells in the top-left and bottom-right three-by-three regions are black.
  • All the 14 cells that are adjacent (horizontally, vertically, or diagonally) to the top-left or bottom-right three-by-three region are white.

It is not allowed to rotate a TaK Code.

You are given a grid with N horizontal rows and M vertical columns. The state of the grid is described by N strings, S_1,\ldots, and S_N, each of length M. The cell at the i-th row from the top and j-th column from the left is black if the j-th character of S_i is #, and white if it is ..

Find all the nine-by-nine regions, completely contained in the grid, that satisfy the conditions of a TaK Code.

Constraints

  • 9 \leq N,M \leq 100
  • N and M are integers.
  • S_i is a string of length M consisting of . and #.

Input

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

N M
S_1
\vdots
S_N

Output

For all pairs (i,j) such that the nine-by-nine region, whose top-left cell is at the i-th row from the top and j-th columns from the left, satisfies the conditions of a TaK Code, print a line containing i, a space, and j in this order.
The pairs must be sorted in lexicographical ascending order; that is, i must be in ascending order, and within the same i, j must be in ascending order.


Sample Input 1

19 18
###......###......
###......###......
###..#...###..#...
..............#...
..................
..................
......###......###
......###......###
......###......###
.###..............
.###......##......
.###..............
............###...
...##.......###...
...##.......###...
.......###........
.......###........
.......###........
........#.........

Sample Output 1

1 1
1 10
7 7
10 2

A TaK Code looks like the following, where # is a black cell, . is a white cell, and ? can be either black or white.

###.?????
###.?????
###.?????
....?????
?????????
?????....
?????.###
?????.###
?????.###

In the grid given by the input, the nine-by-nine region, whose top-left cell is at the 10-th row from the top and 2-nd column from the left, satisfies the conditions of a TaK Code, as shown below.

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

Sample Input 2

9 21
###.#...........#.###
###.#...........#.###
###.#...........#.###
....#...........#....
#########...#########
....#...........#....
....#.###...###.#....
....#.###...###.#....
....#.###...###.#....

Sample Output 2

1 1

Sample Input 3

18 18
######............
######............
######............
######............
######............
######............
..................
..................
..................
..................
..................
..................
............######
............######
............######
............######
............######
............######

Sample Output 3


There may be no region that satisfies the conditions of TaK Code.

E - Triangle?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

xy 平面上に 1 から N までの番号が付いた N 個の点があります。
i は座標 (X_i,Y_i) にあり、相異なる 2 点は異なる位置に存在します。
この N 点から 3 点を選ぶとき、選ばれた 3 点を線分で結んだ図形が正の面積を持つ三角形になるような点の選び方の総数を求めてください。

制約

  • 入力は全て整数である
  • 3 \le N \le 300
  • -10^9 \le X_i,Y_i \le 10^9
  • i \neq j ならば (X_i,Y_i) \neq (X_j,Y_j)

入力

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

N
X_1 Y_1
X_2 Y_2
\dots
X_N Y_N

出力

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


入力例 1

4
0 1
1 3
1 1
-1 -1

出力例 1

3

点を図示すると、以下のようになります。

三角形をなすような点の選び方は、 \{1,2,3\},\{1,3,4\},\{2,3,4\}3 つです。


入力例 2

20
224 433
987654321 987654321
2 0
6 4
314159265 358979323
0 0
-123456789 123456789
-1000000000 1000000000
124 233
9 -6
-4 0
9 5
-7 3
333333333 -333333333
-9 -1
7 -10
-1 5
324 633
1000000000 -1000000000
20 0

出力例 2

1124

Score : 300 points

Problem Statement

In the xy-plane, we have N points numbered 1 through N.
Point i is at the coordinates (X_i,Y_i). Any two different points are at different positions.
Find the number of ways to choose three of these N points so that connecting the chosen points with segments results in a triangle with a positive area.

Constraints

  • All values in input are integers.
  • 3 \le N \le 300
  • -10^9 \le X_i,Y_i \le 10^9
  • (X_i,Y_i) \neq (X_j,Y_j) if i \neq j.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\dots
X_N Y_N

Output

Print the answer as an integer.


Sample Input 1

4
0 1
1 3
1 1
-1 -1

Sample Output 1

3

The figure below illustrates the points.

There are three ways to choose points that form a triangle: \{1,2,3\},\{1,3,4\},\{2,3,4\}.


Sample Input 2

20
224 433
987654321 987654321
2 0
6 4
314159265 358979323
0 0
-123456789 123456789
-1000000000 1000000000
124 233
9 -6
-4 0
9 5
-7 3
333333333 -333333333
-9 -1
7 -10
-1 5
324 633
1000000000 -1000000000
20 0

Sample Output 2

1124
F - T-shirts

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder 社はロゴ入りの T シャツを販売しています。

高橋君の N 日間の予定が 0, 1, 2 のみからなる長さ N の文字列 S で与えられます。
具体的には、1\leq i\leq N をみたす整数 i について、

  • Si 文字目が 0 のとき、i 日目に何の予定も入っていません。
  • Si 文字目が 1 のとき、i 日目に高橋君は食事に行く予定があります。
  • Si 文字目が 2 のとき、i 日目に高橋君は競技プログラミングのイベントに行く予定が入っています。

高橋君は無地の T シャツを M 枚持っており、1 日目の直前の時点ですべて洗濯済みの状態となっています。
これに加えて、次の条件をみたすように行動できるように、高橋君は AtCoder のロゴ入りの T シャツを何枚か購入する事にしました。

  • 食事に行く日には、無地の T シャツ 1 枚またはロゴ入りの T シャツ 1 枚を着用する。
  • 競技プログラミングのイベントに行く日にはロゴ入りの T シャツ 1 枚を着用する。
  • 何の予定もない日には T シャツを着用しない。加えて、その時点で着用済みの T シャツを全て洗濯する。 洗濯した T シャツは翌日から着用できる。
  • 一度着用した T シャツは次に洗濯するまで着用できない。

N 日間のうち予定が入っている日すべてについて、条件をみたす T シャツを着用できるようにするために、高橋君は最低何枚のTシャツを購入する必要があるか求めてください。 新しく T シャツを購入する必要がないならば 0 を出力してください。
ただし、購入した T シャツも 1 日目の直前の時点ですべて洗濯済みの状態で存在するものとします。

制約

  • 1\leq M\leq N\leq 1000
  • S0, 1, 2 のみからなる長さ N の文字列
  • N,M は整数

入力

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

N M
S

出力

問題文の条件をみたすように行動するために 高橋君が購入する必要のある T シャツの枚数の最小値を出力せよ。
新しく購入する必要がないならば 0 を出力せよ。


入力例 1

6 1
112022

出力例 1

2

高橋君がロゴ入りの T シャツを 2 枚購入したとき、次のようにして高橋君は T シャツを着用することができます。

  • 1 日目、高橋君はロゴ入りの T シャツを着用して食事に行きます。
  • 2 日目、高橋君は無地の T シャツを着用して食事に行きます。
  • 3 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
  • 4 日目、高橋君は予定がないため、着用した T シャツをすべて洗濯します。これにより、1,2,3 日目に着用した T シャツを再び着用することが可能になります。
  • 5 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
  • 6 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。

高橋君がロゴ入りの T シャツを 1 枚以下しか購入しなかった場合には、 どのようにしても条件をみたすように T シャツを着用することができません。
よって、2 を出力します。


入力例 2

3 1
222

出力例 2

3

入力例 3

2 1
01

出力例 3

0

高橋君は新しく T シャツを購入する必要はありません。

Score : 300 points

Problem Statement

AtCoder Inc. sells T-shirts with its logo.

You are given Takahashi's schedule for N days as a string S of length N consisting of 0, 1, and 2.
Specifically, for an integer i satisfying 1\leq i\leq N,

  • if the i-th character of S is 0, he has no plan scheduled for the i-th day;
  • if the i-th character of S is 1, he plans to go out for a meal on the i-th day;
  • if the i-th character of S is 2, he plans to attend a competitive programming event on the i-th day.

Takahashi has M plain T-shirts, all washed and ready to wear just before the first day.
In addition, to be able to satisfy the following conditions, he will buy several AtCoder logo T-shirts.

  • On days he goes out for a meal, he will wear a plain or logo T-shirt.
  • On days he attends a competitive programming event, he will wear a logo T-shirt.
  • On days with no plans, he will not wear any T-shirts. Also, he will wash all T-shirts worn at that point. He can wear them again from the next day onwards.
  • Once he wears a T-shirt, he cannot wear it again until he washes it.

Determine the minimum number of T-shirts he needs to buy to be able to wear appropriate T-shirts on all scheduled days during the N days. If he does not need to buy new T-shirts, print 0.
Assume that the purchased T-shirts are also washed and ready to use just before the first day.

Constraints

  • 1\leq M\leq N\leq 1000
  • S is a string of length N consisting of 0, 1, and 2.
  • N and M are integers.

Input

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

N M
S

Output

Print the minimum number of T-shirts Takahashi needs to buy to be able to satisfy the conditions in the problem statement.
If he does not need to buy new T-shirts, print 0.


Sample Input 1

6 1
112022

Sample Output 1

2

If Takahashi buys two logo T-shirts, he can wear T-shirts as follows:

  • On the first day, he wears a logo T-shirt to go out for a meal.
  • On the second day, he wears a plain T-shirt to go out for a meal.
  • On the third day, he wears a logo T-shirt to attend a competitive programming event.
  • On the fourth day, he has no plans, so he washes all the worn T-shirts. This allows him to reuse the T-shirts worn on the first, second, and third days.
  • On the fifth day, he wears a logo T-shirt to attend a competitive programming event.
  • On the sixth day, he wears a logo T-shirt to attend a competitive programming event.

If he buys one or fewer logo T-shirts, he cannot use T-shirts to meet the conditions no matter what. Hence, print 2.


Sample Input 2

3 1
222

Sample Output 2

3

Sample Input 3

2 1
01

Sample Output 3

0

He does not need to buy new T-shirts.

G - Writing a Numeral

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

文字列 S があり、初め S= 1 です。
以下の形式のクエリが Q 個与えられるので順に処理してください。

  • 1 x : S の末尾に数字 x を追加する
  • 2 : S の先頭の数字を削除する
  • 3 : S を十進数表記の数とみなした値を 998244353 で割った余りを出力する

制約

  • 1 \leq Q \leq 6 \times 10^5
  • 1 番目の形式のクエリについて、x \in \{1,2,3,4,5,6,7,8,9\}
  • 2 番目の形式のクエリは S2 文字以上の時にのみ与えられる
  • 3 番目の形式のクエリが 1 個以上存在する

入力

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

Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

ただし \mathrm{query}_ii 番目のクエリを表し、以下のいずれかの形式である。

1 x
2
3

出力

3 番目の形式のクエリの個数を q として、q 行出力せよ。i (1 \leq i \leq q) 行目には i 番目の 3 番目の形式のクエリに対する出力をせよ。


入力例 1

3
3
1 2
3

出力例 1

1
12

1 番目のクエリにおいて、S1 なので ( 1998244353 で割った余りに等しい) 1 を出力します。
2 番目のクエリにおいて、S12 になります。
3 番目のクエリにおいて、S12 なので ( 12998244353 で割った余りに等しい) 12 を出力します。


入力例 2

3
1 5
2
3

出力例 2

5

入力例 3

11
1 9
1 9
1 8
1 2
1 4
1 4
1 3
1 5
1 3
2
3

出力例 3

0

出力されるべき値は 998244353 で割った余りであることに注意してください。

Score : 400 points

Problem Statement

We have a string S. Initially, S= 1.
Process Q queries in the following formats in order.

  • 1 x : Append a digit x at the end of S.
  • 2 : Delete the digit at the beginning of S.
  • 3 : Print the number represented by S in decimal, modulo 998244353.

Constraints

  • 1 \leq Q \leq 6 \times 10^5
  • For each query in the first format, x \in \{1,2,3,4,5,6,7,8,9\}.
  • A query in the second format is given only if S has a length of 2 or greater.
  • There is at least one query in the third format.

Input

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

Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_i denotes the i-th query, which is in one of the following formats:

1 x
2
3

Output

Print q lines, where q is the number of queries in the third format. The i-th line (1 \leq i \leq q) should correspond to the i-th query in the third format.


Sample Input 1

3
3
1 2
3

Sample Output 1

1
12

In the first query, S is 1, so you should print 1 modulo 998244353, that is, 1.
In the second query, S becomes 12.
In the third query, S is 12, so you should print 12 modulo 998244353, that is, 12.


Sample Input 2

3
1 5
2
3

Sample Output 2

5

Sample Input 3

11
1 9
1 9
1 8
1 2
1 4
1 4
1 3
1 5
1 3
2
3

Sample Output 3

0

Be sure to print numbers modulo 998244353.

H - MEX

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

0,1,2 からなる長さ N の数列 A=(A_1,A_2,\dots,A_N) と、 M, E, X からなる長さ N の文字列 S=S_1S_2\dots S_N が与えられます。

1 \leq i < j < k \leq N かつ S_iS_jS_k= MEX を満たす全ての整数の組 (i,j,k) に対する \text{mex}(A_i,A_j,A_k) の総和を求めてください。 ここで、\text{mex}(A_i,A_j,A_k)A_i,A_j,A_k のいずれとも一致しない最小の非負整数を意味します。

制約

  • 3\leq N \leq 2\times 10^5
  • N は整数
  • A_i \in \lbrace 0,1,2\rbrace
  • SM, E, X からなる長さ N の文字列

入力

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

N
A_1 A_2 \dots A_N
S

出力

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


入力例 1

4
1 1 0 2
MEEX

出力例 1

3

S_iS_jS_k = MEX となる i,j,k\ (1 \leq i < j < k \leq N) の組は (i,j,k)=(1,2,4),(1,3,4)2 つです。 \text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0,\text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3 より答えは 0+3=3 です。


入力例 2

3
0 0 0
XXX

出力例 2

0

入力例 3

15
1 1 2 0 0 2 0 2 0 0 0 0 0 2 2
EXMMXXXEMEXEXMM

出力例 3

13

Score : 475 points

Problem Statement

You are given a length-N sequence A=(A_1,A_2,\dots,A_N) consisting of 0, 1, and 2, and a length-N string S=S_1S_2\dots S_N consisting of M, E, and X.

Find the sum of \text{mex}(A_i,A_j,A_k) over all tuples of integers (i,j,k) such that 1 \leq i < j < k \leq N and S_iS_jS_k= MEX. Here, \text{mex}(A_i,A_j,A_k) denotes the minimum non-negative integer that equals neither A_i,A_j, nor A_k.

Constraints

  • 3\leq N \leq 2\times 10^5
  • N is an integer.
  • A_i \in \lbrace 0,1,2\rbrace
  • S is a string of length N consisting of M, E, and X.

Input

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

N
A_1 A_2 \dots A_N
S

Output

Print the answer as an integer.


Sample Input 1

4
1 1 0 2
MEEX

Sample Output 1

3

The tuples (i,j,k)\ (1 \leq i < j < k \leq N) such that S_iS_jS_k = MEX are the following two: (i,j,k)=(1,2,4),(1,3,4). Since \text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0 and \text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3, the answer is 0+3=3.


Sample Input 2

3
0 0 0
XXX

Sample Output 2

0

Sample Input 3

15
1 1 2 0 0 2 0 2 0 0 0 0 0 2 2
EXMMXXXEMEXEXMM

Sample Output 3

13
I - Predilection

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

長さ N の数列 A が与えられます。 数列の長さが 2 以上のとき、隣接する二つの値を選び、それらを削除し、それらが元にあった位置にそれらの和を挿入するという操作を好きなだけ行えます。 0 回以上の操作の後の数列として考えられるものは何通りあるか求め、998244353 で割ったあまりを出力してください。

制約

  • 1 \leq N \leq 2\times 10^5
  • |A_i| \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ。


入力例 1

3
1 -1 1

出力例 1

4

0 回以上の操作の後の数列として考えられるのは以下の 4 通りです。

  • {1,-1,1}
  • {1,0}
  • {0,1}
  • {1}

入力例 2

10
377914575 -275478149 0 -444175904 719654053 -254224494 -123690081 377914575 -254224494 -21253655

出力例 2

321

Score : 500 points

Problem Statement

Given is a sequence A of length N. You can do this operation any number of times: when the length of the sequence is at least 2, choose two adjacent values, delete them, and insert their sum where they were. How many sequences can result from zero or more operations? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • |A_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 \cdots A_N

Output

Print the answer.


Sample Input 1

3
1 -1 1

Sample Output 1

4

The following four sequences can result from zero or more operations.

  • {1,-1,1}
  • {1,0}
  • {0,1}
  • {1}

Sample Input 2

10
377914575 -275478149 0 -444175904 719654053 -254224494 -123690081 377914575 -254224494 -21253655

Sample Output 2

321