Hide

Problem A
Bunny Town Bonding

/problems/bunnytownbonding/file/statement/en/img-0001.jpg
Image by Paper House Productions; Used with permission

As long as anyone can remember, Bunny Town has been home to many happy and friendly bunnies living together peacefully. However, a recent misunderstanding has led to serious disputes and relationship issues. Now most of the bunnies have stopped talking to each other, and even try to avoid being near each other in public places. Deeply concerned by this unpleasant development, the King of Bunny Town has instituted several guidelines to restore community bonding. One of the guidelines states: “Bunnies should not allow any vacant seat(s) among themselves when two or more bunnies sit in a row in an entertainment venue such as a theater.” You are a Bunny Town volunteer who has been selected by the King to oversee the implementation of this guideline. Your job is to find bunnies sitting in a row of seats, and then move individual bunnies until there are no empty seats among them, while keeping overall movements to a minimum. That is, given a row of $N$ seats specified by a sequence of B’s and E’s, where B represents a seat that is occupied by a bunny and E represents an empty seat, find the minimum number of moves so that all the bunnies in the row are sitting together (i.e., occupy a contiguous portion of the row). Note that moving a bunny one position to its immediate left or right is considered a single move, and also note that while moving a bunny, it can temporarily be located on top of another bunny, but must ultimately end up in a seat by itself.

Input

The input is a single line containing a string of length $N$, where $2 \leq N \leq 2\, 000$. The string consists exclusively of the characters ‘E’ and ‘B’. The number of ‘B’ characters in the string is guaranteed to be at least $2$.

Output

Output a single integer, the minimum number of moves required to arrange the bunnies in the row so that there are no empty seats among them.

Explanation of Sample Input/Output 1

If we move the rightmost bunny leftward $5$ positions, i.e., into the $4^\mathrm {th}$ seat (counting from the left), all the bunnies are now sitting together, and this is the minimum number of moves possible to achieve this.

Explanation of Sample Input/Output 2

One solution is to move the bunny in the $1^\mathrm {st}$ seat (counting from the left) $8$ positions rightward into the the $9^\mathrm {th}$ seat, move the bunny in the $2^\mathrm {nd}$ seat $8$ positions rightward into the $10^\mathrm {th}$ seat, and move the bunny in the $3^\mathrm {rd}$ seat $8$ positions rightward into the $11^\mathrm {th}$ seat. Then the total number of moves is $8 + 8 + 8 = 24$, and this is the minimum possible.

Sample Input 1 Sample Output 1
EBBEBEEEB
5
Sample Input 2 Sample Output 2
BBBEEEEEEEEBBBBBB
24

Please log in to submit a solution to this problem

Log in