A - Decisive Battle

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

配点 : 100

問題文

ある都にて、東軍と西軍が戦っています。

高橋君は戦っている軍勢の情報を文字列 S として記録しました。
S には文字 EW のみが含まれており、 S に含まれる文字 E の数と東軍の人数、 W の数と西軍の人数がそれぞれ一致します。

高橋君が記録した S が与えられるので、東軍の人数が西軍の人数より多いなら East 、西軍の人数が東軍の人数より多いなら West と出力してください。

なお、 S の長さは奇数であることが保証されます。そのため、東軍と西軍のどちらか一方がもう一方に比べて真に人数が多い (つまり、両方の軍勢の人数が同じであることはない) ことが証明できます。

制約

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

入力

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

S

出力

答えを出力せよ。


入力例 1

EEWEW

出力例 1

East

SE3 個、 W2 個含まれるため、東軍 3 人に対して西軍 2 人です。
東軍の人数が西軍の人数より多いので East と出力してください。


入力例 2

WWWWWWW

出力例 2

West

SE0 個、 W7 個含まれるため、東軍 0 人に対して西軍 7 人です。
西軍の人数が東軍の人数より多いので West と出力してください。

Score : 100 points

Problem Statement

The Eastern Army and the Western Army are at war in a certain capital.

Takahashi recorded information about the fighting forces as a string S.
S contains only the characters E and W, and the number of Es in S equals the number of soldiers in the Eastern Army, and the number of Ws equals the number of soldiers in the Western Army.

Given the string S that Takahashi recorded, output East if the Eastern Army has more soldiers than the Western Army, or West if the Western Army has more soldiers than the Eastern Army.

The length of S is guaranteed to be odd. Thus, it can be proved that exactly one of the two armies strictly outnumbers the other (that is, the two armies cannot have the same number of soldiers).

Constraints

  • S is a string of length between 1 and 99, inclusive, consisting of E and W.
  • The length of S is odd.

Input

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

S

Output

Output the answer.


Sample Input 1

EEWEW

Sample Output 1

East

S contains three Es and two Ws, so the Eastern Army has three soldiers and the Western Army has two soldiers.
Since the Eastern Army has more soldiers than the Western Army, output East.


Sample Input 2

WWWWWWW

Sample Output 2

West

S contains zero Es and seven Ws, so the Eastern Army has zero soldiers and the Western Army has seven soldiers.
Since the Western Army has more soldiers than the Eastern Army, output West.

B - Crop

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

配点 : 200

問題文

高さ H ピクセル、幅 W ピクセルの白黒画像があります。 上から i 行目、左から j 列目のピクセルの色は文字 C_{i,j} として与えられ、. は白、# は黒を表します。

この画像の上下左右の端から、すべてのピクセルが白であるような行や列を削除します。 具体的には、次の処理を順に行います。

  1. 画像の最上行がすべて白である限り、最上行を削除することを繰り返す。
  2. 画像の最下行がすべて白である限り、最下行を削除することを繰り返す。
  3. 画像の最左列がすべて白である限り、最左列を削除することを繰り返す。
  4. 画像の最右列がすべて白である限り、最右列を削除することを繰り返す。

処理後の画像を出力してください。

なお、与えられる画像には黒いピクセルが少なくとも 1 つ存在します。

制約

  • 1 \le H, W \le 50
  • C_{i,j}. または # である
  • 黒いピクセルが少なくとも 1 つ存在する

入力

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

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

出力

処理後の画像を、以下の形式で出力せよ。
ここで、h,w は処理後の画像の高さと幅(ピクセル単位)である。
また、c_{i,j} は処理後の画像の上から i 行目、左から j 列目のピクセルの色を表す文字であり、白のときは . 、黒のときは # とする。

c_{1,1}c_{1,2}\ldots c_{1,w}
c_{2,1}c_{2,2}\ldots c_{2,w}
\vdots
c_{h,1}c_{h,2}\ldots c_{h,w}

入力例 1

4 5
.....
..#..
.###.
.....

出力例 1

.#.
###

上端の 1 行、下端の 1 行、左端の 1 列、右端の 1 列を削除すればよいです。


入力例 2

3 4
#...
....
...#

出力例 2

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

この画像では、最上行、最下行、最左列、最右列のいずれにも黒いピクセルが含まれています。
したがってどの行や列も削除されず、元の画像をそのまま出力すればよいです。


入力例 3

5 6
......
......
...#..
......
......

出力例 3

#

Score : 200 points

Problem Statement

There is a black-and-white image of height H pixels and width W pixels. The color of the pixel at the i-th row from the top and j-th column from the left is given as a character C_{i,j}, where . represents white and # represents black.

From the top, bottom, left, and right edges of this image, remove rows and columns where all pixels are white. Specifically, perform the following operations in order:

  1. While the topmost row of the image is entirely white, repeat removing the topmost row.
  2. While the bottommost row of the image is entirely white, repeat removing the bottommost row.
  3. While the leftmost column of the image is entirely white, repeat removing the leftmost column.
  4. While the rightmost column of the image is entirely white, repeat removing the rightmost column.

Output the image after processing.

The given image contains at least one black pixel.

Constraints

  • 1 \le H, W \le 50
  • C_{i,j} is . or #.
  • At least one black pixel exists.

Input

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

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

Output

Output the image after processing in the following format.
Here, h and w are the height and width of the image after processing, in pixels, respectively.
c_{i,j} is the character representing the color of the pixel at the i-th row from the top and j-th column from the left; c_{i,j} must be . if the pixel is white and # if it is black.

c_{1,1}c_{1,2}\ldots c_{1,w}
c_{2,1}c_{2,2}\ldots c_{2,w}
\vdots
c_{h,1}c_{h,2}\ldots c_{h,w}

Sample Input 1

4 5
.....
..#..
.###.
.....

Sample Output 1

.#.
###

You should remove one row from the top edge, one row from the bottom edge, one column from the left edge, and one column from the right edge.


Sample Input 2

3 4
#...
....
...#

Sample Output 2

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

In this image, all of the topmost row, bottommost row, leftmost column, and rightmost column contain black pixels.
Therefore, without removing any rows or columns, you should output the original image as is.


Sample Input 3

5 6
......
......
...#..
......
......

Sample Output 3

#
C - Plumage Palette

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

配点 : 300

問題文

高橋君は鳥 1,2,\dots,NN 羽の鳥を M 日間観察しました。
高橋君が観察した鳥には色 1,2,\dots,NN 色のうちいずれか 1 色がついていますが、これらの鳥には観察期間中に色が変化するという興味深い特徴があります。

iD_i-1 日目以前の観察では色 A_i であり、 D_i 日目以降の観察では色 B_i になりました。
ただし、 D_i=1 である場合はその鳥の色は 1 日目の観察から B_i であり、 A_i=B_i である場合はその鳥の色は観察期間中に変化しませんでした。

j=1,2,\dots,M について、 j 日目の観察で鳥の色が何種類あったかを求めてください。

制約

  • 1 \le N \le 3 \times 10^5
  • 1 \le M \le 3 \times 10^5
  • 1 \le A_i,B_i \le N
  • 1 \le D_i \le M
  • 入力はすべて整数

入力

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

N M
A_1 D_1 B_1
A_2 D_2 B_2
\vdots
A_N D_N B_N

出力

M 行出力せよ。
そのうち j 行目には、 j 日目に鳥の色が何種類あったかを出力せよ。


入力例 1

6 7
1 3 2
2 6 5
5 5 1
3 3 5
4 1 6
6 3 6

出力例 1

5
5
3
3
4
4
4

この入力では、 6 羽の鳥を 7 日間観察します。

  • 1 日目の観察で、各鳥の色は 1,2,5,3,6,6 でした。鳥の色は全部で 5 種類です。
  • 2 日目の観察で、各鳥の色は 1,2,5,3,6,6 でした。鳥の色は全部で 5 種類です。
  • 3 日目の観察で、各鳥の色は 2,2,5,5,6,6 でした。鳥の色は全部で 3 種類です。
  • 4 日目の観察で、各鳥の色は 2,2,5,5,6,6 でした。鳥の色は全部で 3 種類です。
  • 5 日目の観察で、各鳥の色は 2,2,1,5,6,6 でした。鳥の色は全部で 4 種類です。
  • 6 日目の観察で、各鳥の色は 2,5,1,5,6,6 でした。鳥の色は全部で 4 種類です。
  • 7 日目の観察で、各鳥の色は 2,5,1,5,6,6 でした。鳥の色は全部で 4 種類です。

Score : 300 points

Problem Statement

Takahashi observed N birds, numbered 1, 2, \dots, N, over M days.
Each of the birds that Takahashi observed has one of N colors, numbered 1, 2, \dots, N, and these birds have an interesting characteristic: their color can change during the observation period.

Bird i had color A_i in observations on or before day D_i - 1, and color B_i in observations on or after day D_i.
Here, if D_i = 1, then the bird's color was B_i from the observation on day 1, and if A_i = B_i, then the bird's color did not change during the observation period.

For each j = 1, 2, \dots, M, find the number of different colors of birds on day j.

Constraints

  • 1 \le N \le 3 \times 10^5
  • 1 \le M \le 3 \times 10^5
  • 1 \le A_i, B_i \le N
  • 1 \le D_i \le M
  • All input values are integers.

Input

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

N M
A_1 D_1 B_1
A_2 D_2 B_2
\vdots
A_N D_N B_N

Output

Output M lines.
The j-th line should contain the number of different colors of birds on day j.


Sample Input 1

6 7
1 3 2
2 6 5
5 5 1
3 3 5
4 1 6
6 3 6

Sample Output 1

5
5
3
3
4
4
4

In this input, six birds are observed over seven days.

  • On day 1, the color of each bird was 1, 2, 5, 3, 6, 6. There are 5 different colors in total.
  • On day 2, the color of each bird was 1, 2, 5, 3, 6, 6. There are 5 different colors in total.
  • On day 3, the color of each bird was 2, 2, 5, 5, 6, 6. There are 3 different colors in total.
  • On day 4, the color of each bird was 2, 2, 5, 5, 6, 6. There are 3 different colors in total.
  • On day 5, the color of each bird was 2, 2, 1, 5, 6, 6. There are 4 different colors in total.
  • On day 6, the color of each bird was 2, 5, 1, 5, 6, 6. There are 4 different colors in total.
  • On day 7, the color of each bird was 2, 5, 1, 5, 6, 6. There are 4 different colors in total.
D - Celester

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

配点 : 400

問題文

これから N 日間の天気が文字列 S として与えられます。
Si 文字目が S であるとき i 日目の天気は晴れ、 R であるとき i 日目の天気は雨です。
また、現時点では嬉しさは 0 です。

あなたは、以下の操作を 0 回以上何回でも行えます。

  • 1 以上 N 以下の整数 i を選ぶ。
  • i 日目の天気が晴れであれば雨、雨であれば晴れに変更する。
  • ただし、 i 日目の天気を変更した場合、嬉しさが X_i 減少する。

操作を行った後の最終的な天気に対して、以下の条件で嬉しさが増加します。

  • 1 \le i \le N-1 を満たす各整数 i について、変更後の i 日目の天気が雨、かつ i+1 日目の天気が晴れであるとき、嬉しさが Y_i 増加する。

操作を行った結果として、達成可能な嬉しさの最大値を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 10^4
  • N2 以上 2 \times 10^5 以下の整数
  • S は長さ NSR からなる文字列
  • X_i1 以上 10^9 以下の整数
  • Y_i1 以上 10^9 以下の整数
  • ひとつの入力における N の総和は 2 \times 10^5 以下

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 個目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N
S
X_1 X_2 \dots X_N
Y_1 Y_2 \dots Y_{N-1}

出力

T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。


入力例 1

5
6
SRRRSR
3 1 4 1 5 9
2 6 5 3 5
6
RSRSRS
10 10 10 10 10 10
1 1 1 1 1
2
RR
4 3
2
10
RSSRSSRSSR
75 49 79 37 16 9 38 49 69 54
23 100 73 63 66 23 51 65 67
20
SSSRSSSRRRRSSRSSRSSR
343191362 223147518 135066250 426658267 693515093 8023388 383375974 712283203 40447501 19870690 318452142 356265717 283999278 209219229 418603824 39363351 392058270 254796273 110117486 64951139
576697130 385986330 895027325 654885799 784214084 577658764 761714876 583039741 943991250 446493376 701505924 402891440 963636095 919408713 238125227 871191978 843843821 397910552 529447424

出力例 1

5
3
0
165
5201284760

この入力には 5 個のテストケースが含まれています。

1 個目のテストケースについて、例えば以下の通りに操作を行うことで嬉しさを最大とすることができます。

  • 3 日目の天気を雨から晴れに変更する。嬉しさが X_3=4 減少し、各日の天気は 晴れ、雨、晴れ、雨、晴れ、雨 となります。
  • この操作の結果、 2 日目が雨、 3 日目が晴れであることから嬉しさが Y_2=6 増加し、 4 日目が雨、 5 日目が晴れであることから嬉しさが Y_4=3 増加します。
  • 全体の嬉しさは (-4)+6+3 = 5 であり、これが達成可能な最大です。

2,3 個目のテストケースについて、操作を行わないことが最適である場合があります。

5 個目のテストケースについて、答えが 32bit 整数型に収まらない場合があることに注意してください。

Score : 400 points

Problem Statement

The weather for the upcoming N days is given as a string S.
If the i-th character of S is S, then the weather on day i is sunny; if it is R, then the weather on day i is rainy.
Also, your current happiness is 0.

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

  • Choose an integer i with 1 \le i \le N.
  • If the weather on day i is sunny, change it to rainy; if it is rainy, change it to sunny.
  • However, if you change the weather on day i, your happiness decreases by X_i.

After performing the operations, your happiness increases based on the final weather according to the following condition:

  • For each integer i with 1 \le i \le N-1, if the weather on day i after modification is rainy and the weather on day i+1 is sunny, your happiness increases by Y_i.

Find the maximum value of happiness achievable by performing operations.

T test cases are given; solve each.

Constraints

  • 1 \le T \le 10^4
  • N is an integer between 2 and 2 \times 10^5, inclusive.
  • S is a string of length N consisting of S and R.
  • X_i is an integer between 1 and 10^9, inclusive.
  • Y_i is an integer between 1 and 10^9, inclusive.
  • The sum of N in a single input is at most 2 \times 10^5.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
S
X_1 X_2 \dots X_N
Y_1 Y_2 \dots Y_{N-1}

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

5
6
SRRRSR
3 1 4 1 5 9
2 6 5 3 5
6
RSRSRS
10 10 10 10 10 10
1 1 1 1 1
2
RR
4 3
2
10
RSSRSSRSSR
75 49 79 37 16 9 38 49 69 54
23 100 73 63 66 23 51 65 67
20
SSSRSSSRRRRSSRSSRSSR
343191362 223147518 135066250 426658267 693515093 8023388 383375974 712283203 40447501 19870690 318452142 356265717 283999278 209219229 418603824 39363351 392058270 254796273 110117486 64951139
576697130 385986330 895027325 654885799 784214084 577658764 761714876 583039741 943991250 446493376 701505924 402891440 963636095 919408713 238125227 871191978 843843821 397910552 529447424

Sample Output 1

5
3
0
165
5201284760

This input contains five test cases.

For the first test case, for example, you can maximize happiness by performing the following operations:

  • Change the weather on day 3 from rainy to sunny. Happiness decreases by X_3 = 4, and the weather on each day becomes sunny, rainy, sunny, rainy, sunny, rainy.
  • As a result of this operation, since day 2 is rainy and day 3 is sunny, happiness increases by Y_2 = 6, and since day 4 is rainy and day 5 is sunny, happiness increases by Y_4 = 3.
  • The total happiness is (-4) + 6 + 3 = 5, which is the achievable maximum.

For the second and third test cases, performing no operations may be optimal.

For the fifth test case, note that the answer may not fit in a 32-bit integer type.

E - Fill-Rect Query

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

配点 : 450

問題文

H \times W のグリッドがあります。はじめ、すべてのマスに A が書かれています。上から i 行目、左から j 列目のマスを (i,j) で表します。

これから Q 回の操作を順に行います。
i 回目の操作では、左上のマスを (1,1)、 右下のマスを (R_i,C_i) とする長方形に含まれるすべてのマスを、英大文字 X_i で上書きします。

すべての操作を行った後のグリッドを出力してください。

制約

  • 1 \le H, W
  • H \times W \le 10^6
  • 1 \le Q \le 2 \times 10^5
  • 1 \le R_i \le H
  • 1 \le C_i \le W
  • X_i は英大文字
  • 入力される数値はすべて整数

入力

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

H W Q
R_1 C_1 X_1
R_2 C_2 X_2
\vdots
R_Q C_Q X_Q

出力

H 行出力せよ。i 行目には長さ W の文字列であって、j 文字目が操作後のグリッドにおいて (i, j) に書かれている英大文字であるものを出力せよ。


入力例 1

2 3 3
2 2 B
1 3 C
2 1 D

出力例 1

DCC
DBA

図のように操作が進みます。


入力例 2

1 7 7
1 7 E
1 6 C
1 5 N
1 4 A
1 3 V
1 2 D
1 1 A

出力例 2

ADVANCE

入力例 3

10 10 15
8 9 B
6 7 C
5 8 D
10 6 E
8 5 F
3 10 G
7 3 H
4 6 I
3 1 J
10 2 K
3 6 L
3 3 M
2 5 N
9 1 O
1 4 P

出力例 3

PPPPNLGGGG
ONNNNLGGGG
OMMLLLGGGG
OKIIIIDDBA
OKHFFEDDBA
OKHFFECBBA
OKHFFEBBBA
OKFFFEBBBA
OKEEEEAAAA
KKEEEEAAAA

Score : 450 points

Problem Statement

There is an H \times W grid. Initially, every cell has A written on it. The cell at the i-th row from the top and j-th column from the left is denoted by (i, j).

We will perform Q operations in order.
In the i-th operation, overwrite all cells in the rectangle with upper-left cell (1, 1) and lower-right cell (R_i, C_i) with the uppercase English letter X_i.

Output the grid after all operations have been performed.

Constraints

  • 1 \le H, W
  • H \times W \le 10^6
  • 1 \le Q \le 2 \times 10^5
  • 1 \le R_i \le H
  • 1 \le C_i \le W
  • X_i is an uppercase English letter.
  • All input numbers are integers.

Input

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

H W Q
R_1 C_1 X_1
R_2 C_2 X_2
\vdots
R_Q C_Q X_Q

Output

Print H lines. The i-th line should contain a string of length W, whose j-th character is the uppercase English letter written on (i, j) in the grid after the operations.


Sample Input 1

2 3 3
2 2 B
1 3 C
2 1 D

Sample Output 1

DCC
DBA

The operations proceed as shown in the figure.


Sample Input 2

1 7 7
1 7 E
1 6 C
1 5 N
1 4 A
1 3 V
1 2 D
1 1 A

Sample Output 2

ADVANCE

Sample Input 3

10 10 15
8 9 B
6 7 C
5 8 D
10 6 E
8 5 F
3 10 G
7 3 H
4 6 I
3 1 J
10 2 K
3 6 L
3 3 M
2 5 N
9 1 O
1 4 P

Sample Output 3

PPPPNLGGGG
ONNNNLGGGG
OMMLLLGGGG
OKIIIIDDBA
OKHFFEDDBA
OKHFFECBBA
OKHFFEBBBA
OKFFFEBBBA
OKEEEEAAAA
KKEEEEAAAA
F - Random Vault Heist

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

配点 : 550

問題文

コワスギ銀行には N 個の金庫があります。金庫 i には A_i 円が入っています。

ある日、強盗がコワスギ銀行に入りました。強盗は盗んだ金額の合計が X 円以上になるまで、以下の操作を繰り返します。

  • まだ開けていない金庫の中から一様ランダムに 1 つ選んで開けて、中のお金をすべて盗む。

強盗によって盗まれた金額の合計の期待値を \text{mod }998244353 で求めてください。

期待値 \text{mod }998244353 の定義

求める期待値は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q {{}\not\equiv{}} 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 1 \le N \le 40
  • 1 \le A_i \le 10^{16}
  • 1 \le X \le \sum_{i=1}^{N} A_i
  • 入力される値はすべて整数

入力

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

N X
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

2 5
3 10

出力例 1

499122188

はじめに開ける金庫は 2 通りです。
金庫 1 をはじめに開けた場合、続けて金庫 2 も開けるので盗まれた金額の合計は 13 円です。
金庫 2 をはじめに開けた場合、その時点で X 円以上になるので盗まれた金額の合計は 10 円です。
したがって期待値は \frac{13+10}{2}=\frac{23}{2} です。


入力例 2

3 2
1 1 1

出力例 2

2

どの順番で開けても、2 個の金庫を開けた時点で盗まれた金額の合計が 2 円になります。したがって期待値は 2 です。


入力例 3

11 60
2 3 5 7 11 13 17 19 23 29 31

出力例 3

525950964

Score : 550 points

Problem Statement

Kowasugi Bank has N safes. Safe i contains A_i yen.

One day, a robber entered the bank. The robber repeats the following operation until the total amount stolen reaches at least X yen:

  • Choose one safe uniformly at random from the safes not yet opened, open it, and steal all the money inside.

Find the expected value, modulo 998244353, of the total amount stolen by the robber.

Definition of expected value modulo 998244353

It can be proved that the expected value sought is always a rational number. Moreover, under the constraints of this problem, it can also be proved that when this value is expressed as an irreducible fraction \frac{P}{Q}, it satisfies Q {{}\not\equiv{}} 0 \pmod{998244353}. Therefore, there exists a unique integer R satisfying R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Find this R.

Constraints

  • 1 \le N \le 40
  • 1 \le A_i \le 10^{16}
  • 1 \le X \le \sum_{i=1}^{N} A_i
  • All input values are integers.

Input

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

N X
A_1 A_2 \ldots A_N

Output

Output the answer.


Sample Input 1

2 5
3 10

Sample Output 1

499122188

There are two choices for the first safe to open.
If safe 1 is opened first, the robber also opens safe 2, so the total amount stolen is 13 yen.
If safe 2 is opened first, the total already reaches at least X yen at that point, so the total amount stolen is 10 yen.
Thus, the expected value is \frac{13 + 10}{2} = \frac{23}{2}.


Sample Input 2

3 2
1 1 1

Sample Output 2

2

Regardless of the order in which they are opened, the total amount stolen when two safes have been opened is 2 yen. Thus, the expected value is 2.


Sample Input 3

11 60
2 3 5 7 11 13 17 19 23 29 31

Sample Output 3

525950964
G - Celester 2

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

配点 : 625

問題文

これから N 日間の天気が文字列 S として与えられます。
Si 文字目が S であるとき i 日目の天気は晴れ、 R であるとき i 日目の天気は雨です。

あなたは、以下の操作を 0 回以上 k 回まで行うことができます。

  • 1 以上 N 以下の整数 i を選択する。
  • i 日目の天気が晴れであれば雨、雨であれば晴れに変更する。

操作を行った後の最終的な天気に対して、以下の条件で嬉しさを得ます。

  • 1 \le i \le N-1 を満たす各整数 i について、変更後の i 日目の天気が雨、かつ i+1 日目の天気が晴れであるとき、嬉しさを 1 得る。

k=0,1,\dots,N について、操作を高々 k 回行った時に得る嬉しさの合計の最大値を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 10^4
  • N2 以上 10^6 以下の整数
  • S は長さ NSR からなる文字列
  • ひとつの入力における N の総和は 10^6 以下

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 個目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N
S

出力

T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。

各テストケースについて、 k=i とした場合の答えを A_i とする。このとき、以下の形式で答えを出力せよ。

A_0 A_1 \dots A_N

入力例 1

5
4
SSSR
2
SR
6
RSRSRS
10
RSRSRRRRRR
20
SSRRSSSSRRRSSRRSRSRS

出力例 1

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

この入力には 5 個のテストケースが含まれています。

1 個目のテストケースについて:

  • 操作前の各日の天気は順に 晴れ、晴れ、晴れ、雨です。
  • k=0 である場合、操作は行えません。
    • 操作後の各日の天気は 晴れ、晴れ、晴れ、雨 となり、得る嬉しさの合計は 0 であり、これが達成可能な最大です。
  • k=1 である場合、例えば 2 日目の天気を変更することが最適です。
    • 操作後の各日の天気は 晴れ、雨、晴れ、雨 となり、得る嬉しさの合計は 1 であり、これが達成可能な最大です。
  • k=2 である場合、例えば 1,2 日目の天気を変更することが最適です。
    • 操作後の各日の天気は 雨、雨、晴れ、雨 となり、得る嬉しさの合計は 1 であり、これが達成可能な最大です。
  • k=3 である場合、例えば 1,3,4 日目の天気を変更することが最適です。
    • 操作後の各日の天気は 雨、晴れ、雨、晴れ となり、得る嬉しさの合計は 2 であり、これが達成可能な最大です。
  • k=4 である場合、例えば 1,3,4 日目の天気を変更することが最適です。
    • 操作後の各日の天気は 雨、晴れ、雨、晴れ となり、得る嬉しさの合計は 2 であり、これが達成可能な最大です。

Score : 625 points

Problem Statement

The weather for the upcoming N days is given as a string S.
If the i-th character of S is S, then the weather on day i is sunny; if it is R, then the weather on day i is rainy.

You can perform the following operation between 0 and k times, inclusive:

  • Choose an integer i with 1 \le i \le N.
  • If the weather on day i is sunny, change it to rainy; if it is rainy, change it to sunny.

After performing the operations, you gain happiness based on the final weather according to the following condition:

  • For each integer i with 1 \le i \le N-1, if the weather on day i after modification is rainy and the weather on day i+1 is sunny, your happiness increases by 1.

For each k = 0, 1, \dots, N, find the maximum total happiness you can gain by performing the operation at most k times.

T test cases are given; solve each.

Constraints

  • 1 \le T \le 10^4
  • N is an integer between 2 and 10^6, inclusive.
  • S is a string of length N consisting of S and R.
  • The sum of N in a single input is at most 10^6.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
S

Output

Output T lines. The i-th line should contain the answer for the i-th test case.

For each test case, let A_i be the answer for k = i. Then, output the answer in the following format:

A_0 A_1 \dots A_N

Sample Input 1

5
4
SSSR
2
SR
6
RSRSRS
10
RSRSRRRRRR
20
SSRRSSSSRRRSSRRSRSRS

Sample Output 1

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

This input contains five test cases.

For the 1st test case:

  • The weather on each day before any operations is sunny, sunny, sunny, rainy in order.
  • For k = 0, no operations can be performed.
    • The weather on each day after operations is sunny, sunny, sunny, rainy, and the total happiness is 0, which is the achievable maximum.
  • For k = 1, for example, changing the weather on day 2 is optimal.
    • The weather on each day after operations is sunny, rainy, sunny, rainy, and the total happiness is 1, which is the achievable maximum.
  • For k = 2, for example, changing the weather on days 1 and 2 is optimal.
    • The weather on each day after operations is rainy, rainy, sunny, rainy, and the total happiness is 1, which is the achievable maximum.
  • For k = 3, for example, changing the weather on days 1, 3, and 4 is optimal.
    • The weather on each day after operations is rainy, sunny, rainy, sunny, and the total happiness is 2, which is the achievable maximum.
  • For k = 4, for example, changing the weather on days 1, 3, and 4 is optimal.
    • The weather on each day after operations is rainy, sunny, rainy, sunny, and the total happiness is 2, which is the achievable maximum.