QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 1024 MB مجموع النقاط: 100 قابلة للهجوم ✓

#18488. Stones 1

الإحصائيات

$N$ stones labeled from $1$ to $N$ are arranged in a row in increasing order. Each stone is colored either white or black. The weight of the $i$-th stone is $A_i$.

You will remove the stones one at a time until all the stones are removed.

When removing a stone, if the stone is not the leftmost or rightmost of all remaining stones, and neither stone adjacent to the stone being removed matches it in color, your score increases by the weight of the stone being removed. Two stones are adjacent if there are no stones in between those two.

Find a way to remove the stones to maximize your score.

Input

The first line contains a single integer $N$ ($1 \le N \le 300\,000$).

The second line contains a string $S$ of length $N$ where each character is either B or W. The $i$-th character of $S$, $S_i$, is B if the $i$-th stone is black, otherwise, $S_i$ is W and the $i$-th stone is white.

The third line contains $N$ integers $A_1, A_2, \dots, A_N$ ($1 \le A_i \le 10^9$). $A_i$ represents the weight of stone $i$.

Output

Output the maximum score that can be obtained if you take the stones optimally.

Examples

Input 1

4
WBWB
6 4 5 3

Output 1

5

Input 2

8
WBBWBWBB
6 4 8 2 5 3 1 5

Output 2

13

Note

If you remove the stones in the order of the 5th, 6th, 2nd, 3rd, 4th, 7th, 8th, and 1st stones from the left (based on their initial positions), you will obtain points when removing the 3rd and 5th stones, resulting in a total of 13 points.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.