题目链接:点击打开链接
B. Alice, Bob, Two Teams
time limit per test
memory limit per test
input
output
n pieces, and the i-th piece has a strength pi.
The way to split up game pieces is split into several steps:
- AandB. This can be seen as writing the assignment of teams of a piece in anncharacter string, where each character isAorB.
- AtoBandBtoA). He can do this step at most once.
- Aand Bob will get all the pieces markedB.
The strength of a player is then the sum of strengths of the pieces in the group.
Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.
Input
n (1 ≤ n ≤ 5·105) — the number of game pieces.
n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.
n characters A or B
Output
a
Examples
input
5 1 2 3 4 5 ABABA
output
11
input
5 1 2 3 4 5 AAAAA
output
15
input
1 1 B
output
1
Note
In the first sample Bob should flip the suffix of length one.
5.
In the third sample Bob should do nothing.
题意:给定n个数和一个字符串,每个数对应一个字符,A表示属于Alice,B表示属于Bob。Bob可以选出字符串的前缀 或者 一个后缀来翻转,A -> B, B -> A,只能使用一次。问Bob可以获得的最大值。(所有属于Bob的数之和)。
思路:维护每一位的前缀和 和 后缀和,最后再是不翻转的情况。