시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
6 초 | 1024 MB | 77 | 12 | 7 | 10.294% |
On the stage, $2N$ beavers are standing in a row. They are members of the chorus. Each beaver sings either the alto part or the bass part of the chorus. This information is given by a string $S$. Precisely, the $i$-th beaver ($1 ≤ i ≤ 2N$) from the stage right (i.e., from left when seen from the audience seats) sings the alto part if the $i$-th character of $S$ is ‘A
’. The $i$-th beaver sings the bass part if the $i$-th character of $S$ is ‘B
’. There are $N$ beavers singing the alto part, and $N$ beavers singing the bass part.
From now, the beavers will sing $K$ songs. However, since all the songs are very difficult, each beaver sings exactly one song only, and does not sing other songs. Moreover, in order to make singing voice more beautiful, for every song, the following conditions should be satisfied.
Bitaro, the conductor, tried to find a way to allot songs to beavers which satisfies the conditions. Since Bitaro is bright, he notices that it might be impossible to allot songs to beavers.
In order to cope with this issue, Bitaro will perform the following operation several times so that there is a way to allot songs to beavers which satisfies the conditions.
Since Bitaro thinks efficiency is important, he wants to minimize the number of operations. However, it turns out to be a surprisingly difficult problem. Since you are a brilliant programmer, Bitaro asks you to solve this problem.
Write a program which, given information of the chorus and the number of songs $K$, calculates the minimum number of operations Bitaro needs to perform. Note that, under the constraints of this task, it is possible to perform the operations several times so that there is a way to allot songs to beavers which satisfies the conditions.
Read the following data from the standard input.
$N$ $K$
$S$
Write one line to the standard output. The output should contain the minimum number of operations Bitaro needs to perform.
A
’ appears $N$ times, and the character ‘B
’ appears $N$ times.번호 | 배점 | 제한 |
---|---|---|
1 | 16 | $N ≤ 10$. |
2 | 24 | $N ≤ 500$. |
3 | 21 | $N ≤ 5\,000$. |
4 | 26 | $N ≤ 100\,000$. |
5 | 13 | No additional constraints. |
5 2 AABABABBAB
2
In this sample input, for example, Bitaro can perform the following operations. Here, the underline denotes the positions of the two beavers swapped by Bitaro.
AAABBABBAB
”.AAABBABABB
”.After these operations, Bitaro can allot songs to beavers as follows.
This way to allot songs to beavers satisfies the conditions.
If the number of operations is less than $2$, there does not exist a way to allot songs to beavers which satisfies the conditions. Therefore, output $2$.
This sample input satisfies the constraints of all the subtasks.
5 3 AABABABBAB
0
Without performing operations, Bitaro can allot songs to beavers as follows.
This way to allot songs to beavers satisfies the conditions. Therefore, output $0$.
This sample input satisfies the constraints of all the subtasks.
3 1 BBBAAA
9
This sample input satisfies the constraints of all the subtasks.
10 3 ABABBBBABBABABABAAAA
37
This sample input satisfies the constraints of all the subtasks.