1. 程式人生 > >【CodeForces - 632B】Alice, Bob, Two Teams (預處理,思維,字首和字尾和)

【CodeForces - 632B】Alice, Bob, Two Teams (預處理,思維,字首和字尾和)

題幹:

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:

  1. 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 n character string, where each character is A or B.
  2. 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.
  3. 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.

題目大意:

     A和B兩個人玩遊戲,給n個數,每個數對應一個 ' A ' 或 ' B ' , 現在B玩家可以選一個字首或者一個字尾,把A變B,B變A,操作結束後,B玩家可以取走所有標號為 ' B ' 的數字,問B玩家可以取到的數字之和最大是多少? 

解題報告:

     看題目名稱,猜是博弈?然而讓你失望了,這題和A沒有任何卵關係,,,先預處理出字首A和B,字尾A和B,這四個陣列。然後列舉每一個元素,維護翻這一位或者不翻這一位的最大值maxx,並且維護和ans的最大值,輸出ans即可。

AC程式碼:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 5e5 + 5; 
ll val[MAX];
ll a[MAX],b[MAX],A[MAX],B[MAX];
ll ans,maxx;
char s[MAX];
int main()
{
	int n;
	cin>>n;
	for(int i = 1; i<=n; i++) scanf("%lld",val+i);
	cin>>s+1;
	//prefix預處理 
	for(int i = 1; i<=n; i++) {
		if(s[i] == 'A') {
			a[i] = a[i-1] + val[i];
			b[i] = b[i-1];
		}
		else {
			a[i] = a[i-1];
			b[i] = b[i-1] + val[i];
		}
	}
	//suffix預處理
	for(int i = n; i>=1; i--) {
		if(s[i] == 'A') {
			A[i] = A[i+1] + val[i];
			B[i] = B[i+1];
		}
		else {
			A[i] = A[i+1];
			B[i] = B[i+1] + val[i];
		}
	} 
	for(int i = 1; i<=n; i++) {
		maxx = max(b[i]+B[i+1],a[i] + B[i+1]);
		ans = max(ans,maxx);
	}
	for(int i = n; i>=1; i--) {
		maxx = max(b[i] + B[i+1],b[i] + A[i+1]);
		ans = max(ans,maxx);
	}
	printf("%lld\n",ans);
	return 0 ;
}