Problem Statement
There is a tournament with $2^N$ participants, numbered $1$ to $2^N$.
The tournament proceeds as follows:
- First, each participant is given a red or blue hat. You are given the color of the hat for each participant as the string
$S$. Specifically, participant$i$is given a red hat if the$i$-th character ($1 \leq i \leq 2^N$) of$S$isR, and a blue hat if it isB. - Then, the following operation is repeated until there is only one participant remaining:
- Let
$2k$be the current number of participants. Divide the participants into$k$pairs. You can freely choose how to pair them. Then, a match is held for each pair; the winner remains, and the loser leaves the tournament. The participants are numbered in descending order of strength, so the participant with the smaller number always wins.
A match between two participants wearing red hats is called a boring match. Your goal is to arrange the pairings so that no boring matches occur during the tournament.
Whether the goal is achievable or not depends on the string $S$. Hence, you have decided to tamper with $S$ before the start of the tournament. Specifically, you can perform the following operation zero or more times:
- Choose two adjacent characters in
$S$and swap them.
Determine whether it is possible to achieve the goal after operations. If it is, find the minimum number of operations required.
Constraints
$1 \leq N \leq 18$$S$is a string of length$2^N$consisting ofRandB.- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
S
Output
If it is impossible to achieve the goal, print $-1$. Otherwise, print the minimum number of operations required.
样例
2
RRBB
1
1
RR
-1
4
RBBRRBRBBRBBBRBR
0
5
RBRRBRRRBRRRRRRRRRBBBBBBBBBBBBBB
11
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。