問題一覧 > 通常問題

No.38 Red, Blue, and White Blocks

レベル : / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 171
作問者 : nmnmnmnmnmnmnm
お気に入りにしたユーザー ProblemId : 31 / 自分の提出
問題文最終更新日: 2026-06-26 06:03:08
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 37 (順位表) の他の問題:

Problem Statement

There are 10 blocks each of red, blue, and white.
The blocks are arranged in a single row from left to right.
The arrangement must follow these rules.

There must not be a red block exactly Kr blocks to the left of a red block.
There must not be a red block exactly Kr blocks to the right of a red block.
There must not be a blue block exactly Kb blocks to the left of a blue block.
There must not be a blue block exactly Kb blocks to the right of a blue block.
There are no particular restrictions on white blocks.

First, the values of Kr and Kb and the arrangement of the blocks are given.
A red block is denoted by R, a blue block by B, and a white block by W.
The initial state may not satisfy the conditions above.
Therefore, the following operation is allowed.

・Remove some of the red or blue blocks and close the gaps in the row.

For example, from "RWBRWWB",
you can remove only the leftmost R to make the row "WBRWWB".
You can also remove the 4th R from the left and the rightmost B to make the row "RWBWW".
You may choose and remove any number of Rs and Bs.
However, W blocks cannot be removed.
By performing such an operation, you want to leave the longest possible row that satisfies the conditions.
You are free to choose how to remove the R and B blocks.
What is the length of the longest row that can be left?

Input

Kr Kb
S

1<=Kr,Kb<=29
S is a string consisting of 10 Rs, 10 Bs, and 10 Ws.

Output

Ans

Print Ans, the maximum number of remaining blocks.
Do not forget the newline at the end.

Sample

Sample 1
Input
1 10
RRRRRRRRRRWWWWWWWWWWBBBBBBBBBB
Output
21

The block 1 position next to an R must not be an R, so 9 Rs are removed.
No B has a B 10 positions away from it, so no B needs to be removed.
In the end, RWWWWWWWWWWBBBBBBBBBB remains.

Sample 2
Input
1 2
WRWWBBRRRWWBBRRRRWWWBBBRRWWBBB
Output
22

Sample 3
Input
7 11
BWBWRWRRWBRRWRRWWBBBRRWBBRWBBW
Output
27

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。