AtCoder Grand Contest 066
D
A Independent Set
AtCoder
⌀ 3531
原题
← CE →
翻译中…

Problem Statement

翻译中…

You are given a string $S$ of length $N$ consisting of A and B. It is guaranteed that the number of As in $S$ is at most $\frac{N+1}{2}$. Additionally, you are given a sequence of positive integers $(x_1, \ldots, x_{N-1})$.

翻译中…

On this string, you can repeatedly perform the following operation:

翻译中…
  • Choose an integer $i$ such that $1\leq i\leq N-1$, and swap the $i$-th and $(i+1)$-th characters of $S$. The cost of this operation is $x_i$.
翻译中…

Your goal is to rearrange $S$ so that no two As are adjacent. Find the minimum total cost required to achieve this goal.

翻译中…

$T$ test cases are given; solve each of them.

翻译中…

Constraints

翻译中…
  • $1\leq T\leq 10^5$
  • $2\leq N\leq 10^6$
  • $S$ is a string of length $N$ consisting of A and B.
  • The number of As in $S$ is at most $\frac{N+1}{2}$.
  • $1\leq x_i \leq 10^6$
  • The sum of $N$ over all test cases in a single input is at most $10^6$.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
\text{case}_1
\vdots
\text{case}_T

翻译中…

Each test case is given in the following format:

翻译中…

N
S
x_1 \ldots x_{N-1}

翻译中…

Output

翻译中…

Print $T$ lines. The $i$-th line should contain the minimum total cost required to rearrange $S$ so that no two As are adjacent for the $i$-th test case.

翻译中…

样例

Input #1
5
4
BAAB
3 4 5
5
BBBBB
1 2 3 4
7
BAAABBB
8 7 6 5 4 3
7
BAAABBB
100 7 6 5 4 3
20
BAABAABBBABAAABBBABB
12 85 37 44 25 14 36 29 71 53 15 47 13 80 14 74 53 76 19
Output #1
3
0
13
15
133
- For the first test case, performing the operation with $i=1$ changes $S$ as `BAAB` → `ABAB`, achieving the goal. The total cost in this case is $x_1=3$. - For the second test case, performing nothing achieves the goal. The total cost in this case is $0$. - For the third test case, performing the operation with $i=1$ and $i=4$ changes $S$ as `BAAABBB` → `ABAABBB` → `ABABABB`, achieving the goal. The total cost in this case is $x_1+x_4=13$. - For the fourth test case, performing the operation with $i=4$, $i=3$, and $i=5$ changes $S$ as `BAAABBB` → `BAABABB` → `BABAABB` → `BABABAB`, achieving the goal. The total cost in this case is $x_4+x_3+x_5=15$.
题目信息
题号
agc066_d
时间限制
2 s
内存限制
1024 MB

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