您的位置:首页 > 其它

Codeforces 632B Alice, Bob, Two Teams 【水题】

2016-03-04 22:09 453 查看
B. Alice, Bob, Two Teams

time limit per test
1.5 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th
piece has a strength pi.

The way to split up game pieces is split into several steps:

First, Alice will split the pieces into two different groups A and B.
This can be seen as writing the assignment of teams of a piece in an ncharacter string, where each character is A or B.

Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A).
He can do this step at most once.

Alice will get all the pieces marked A and Bob will get all the pieces marked B.

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

The first line contains integer n (1 ≤ n ≤ 5·105)
— the number of game pieces.

The second line contains n integers pi (1 ≤ pi ≤ 109)
— the strength of the i-th piece.

The third line contains n characters A or B —
the assignment of teams after the first step (after Alice's step).

Output

Print the only integer a — the maximum strength Bob can achieve.

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.

In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.

In the third sample Bob should do nothing.

题意:给定n个数和一个字符串,每个数对应一个字符,A表示属于Alice,B表示属于Bob。Bob可以选出字符串的前缀 或者 一个后缀来翻转,A -> B, B -> A,只能使用一次。问Bob可以获得的最大值。(所有属于Bob的数之和)。

思路:维护每一位的前缀和 和 后缀和,最后再是不翻转的情况。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define ll o<<1#define rr o<<1|1using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 5*1e5+10;
const int INF = 0x3f3f3f3f;
void add(LL &x, LL y) {x += y; x %= MOD;}
int a[MAXN];
char str[MAXN];
LL sum[MAXN];
int main()
{
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
cin >> str+1; sum[0] = 0;
for(int i = 1; i <= n; i++)
{
if(str[i] == 'B')
sum[i] = sum[i-1] + a[i];
else
sum[i] = sum[i-1];
}
LL res = 0; LL ans = 0;
for(int i = 1; i <= n; i++)
{
if(str[i] == 'A')
res += a[i];
ans = max(ans, res + sum
- sum[i-1]);
}
res = 0;
for(int i = n; i >= 1; i--)
{
if(str[i] == 'A')
res += a[i];
ans = max(ans, res + sum[i-1]);
}
cout << max(ans, sum
) << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: