estie プログラミングコンテスト2023 (AtCoder Regular Contest 169)
E
Avoid Boring Matches
AtCoder
⌀ 3333
原题
← DF →
翻译中…

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$ is R, and a blue hat if it is B.
  • 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 of R and B.
  • 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.

翻译中…

样例

Input #1
2
RRBB
Output #1
1
Without performing any operations, you cannot achieve the goal. If you swap the second and third characters of $S$, making $S=$`RBRB`, you can achieve the goal as follows: - Give red, blue, red, and blue hats to participants $1$, $2$, $3$, and $4$, respectively. - Divide the four participants into two pairs $(1,4),(2,3)$. No boring matches occur here. After the matches, participants $1$ and $2$ remain. - Divide the two participants into one pair $(1,2)$. No boring matches occur here. After the match, participant $1$ remains. Therefore, the answer is $1$.
Input #2
1
RR
Output #2
-1
Input #3
4
RBBRRBRBBRBBBRBR
Output #3
0
Input #4
5
RBRRBRRRBRRRRRRRRRBBBBBBBBBBBBBB
Output #4
11
题目信息
题号
arc169_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。