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 p_{i}.

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 n character 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·10^5) — the number of game pieces.

The second line contains n integers p_{i} (1 ≤ p_{i} ≤ 10^9) — 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.

Difficulty: interview

Starter code:

Solutions:
n = input()
s = [int(x) for x in input().split(' ')]
g = input()
A = 0
B = 0
for i in range(len(g)):
	if g[i] == 'A':
		A += s[i]
	else:
		B += s[i]

startA, startB = A, B
maximum = B

for i in range(len(g)):
	if g[i] == 'A':
		A -= s[i]
		B += s[i]
	else:
		A += s[i]
		B -= s[i]
	if B > maximum:
		maximum = B

A, B = startA, startB

for i in reversed(range(len(g))):
	if g[i] == 'A':
		A -= s[i]
		B += s[i]
	else:
		A += s[i]
		B -= s[i]
	if B > maximum:
		maximum = B

print(maximum)

read = lambda: list(map(int, input().split()))
n = int(input())
p = list(read())
a = [{'B': 1, 'A': 0}[i] for i in input()]
cur = sum(p[i] for i in range(n) if a[i])
ans = cur
b = a[:]
for i in range(n):
    b[i] = int(not a[i])
    if b[i]: cur += p[i]
    else: cur -= p[i]
    ans = max(ans, cur)
cur = sum(p[i] for i in range(n) if a[i])
b = a[:]
for i in range(n - 1, -1, -1):
    b[i] = int(not a[i])
    if b[i]: cur += p[i]
    else: cur -= p[i]
    ans = max(ans, cur)
print(ans)


n = int(input())
strength = list(map(int, input().split()))
pieces = input()

bob = 0

for pos, p in enumerate(pieces):
    if p == "B":
        bob += strength[pos]


best_flip = 0
cur_flip = 0

for st, p in zip(strength, pieces):
    if p == "A":
        cur_flip += st
    else:
        cur_flip -= st

    best_flip = max(best_flip, cur_flip)

cur_flip = 0
for st, p in zip(reversed(strength), reversed(pieces)):
    if p == "A":
        cur_flip += st
    else:
        cur_flip -= st

    best_flip = max(best_flip, cur_flip)

print(bob + best_flip)

from itertools import accumulate
import math

n = int(input())
p = [int(x) for x in input().split()]
S1 = sum(p)
d = [x == 'A' for x in input()]
for i in range(n):
    if d[i]: p[i] = -p[i]
prefix = list(accumulate([0] + p))
S = prefix[-1]
m, M = min(prefix), max(prefix)
print((S1 + max(S - 2*m, 2*M - S)) // 2)


n = int(input())
pi = list(map(int,input().split()))
ab = input()

numi = [0]*n
numi2 = [0]*n
if ab[0] == "B":
    numi[0] = pi[0]
else:
    numi2[0] = pi[0]
    
for i in range(1,n):
    if ab[i] == "B":
        numi[i] = numi[i-1] + pi[i]
        numi2[i] = numi2[i-1]
    else:
        numi2[i] = numi2[i-1] + pi[i]
        numi[i] = numi[i-1]
n2 = n-1
maxi = numi[n2] 
for i in range(n):
    maxi = max(maxi,numi[n2] - numi[i] + numi2[i])
for i in range(n-2,-1,-1):
    maxi = max(maxi,numi[i] + numi2[n2] - numi2[i])
print(maxi)


n = int(input())
p = list(map(int, input().split()))
l = input()
change = 0
max_change = 0
init_str = 0
for i in range(len(l)):
    if l[i] == 'A':
        change += p[i]
    else:
        change -= p[i]
        init_str += p[i]
    if max_change < change:
        max_change = change
max_change_back = 0
change = 0
for i in range(len(l) - 1, -1, -1):
    if l[i] == 'A':
        change += p[i]
    else:
        change -= p[i]
    if max_change_back < change:
        max_change_back = change
print(max(max_change_back, max_change) + init_str)


n = int(input())
prices = list(map(int, input().split()))
who = input()
basic = 0
for i in range(len(who)):
    if who[i] == "B":
        basic += prices[i]
max = basic
now = basic
for i in range(len(who)):
    if who[i] == "A":
        now += prices[i]
    else:
        now -= prices[i]
    if now > max:
        max = now
now = basic
for i in range(len(who) - 1, -1, -1):
    if who[i] == "A":
        now += prices[i]
    else:
        now -= prices[i]
    if now > max:
        max = now
print(max)

import collections
import math

#n, p = map(int, input().split())
n = int(input())
p = list(map(int, input().split()))
s = input()

def solve(p, s):
    a, b = [0] * n, [0] * n
    m = 0
    if s[0] == 'B':
        b[0] = p[0]
        m += p[0]
    else:
        a[0] = p[0]
    for i in range(1, n):
        if s[i] == 'B':
            b[i] += p[i]
            m += p[i]
        else:
            a[i] += p[i]
        a[i] += a[i - 1]
        b[i] += b[i - 1]
    ans = m
    for i in range(n):
        if m - b[i] + a[i] > ans:
            ans = m - b[i] + a[i]
    return ans
print(max(solve(p, s), solve(p[::-1], s[::-1])))

n = int(input())
ps = list(map(int, input().split()))
qs = input()

B = 0
for i in range(n):
    if qs[i] == 'B':
        B += ps[i]

maxi = B
b = B
for i in range(n):
    if qs[i] == 'A':
        b += ps[i]
    elif qs[i] == 'B':
        b -= ps[i]
    maxi = max(maxi, b)

b = B
for i in range(n-1, -1, -1):
    if qs[i] == 'A':
        b += ps[i]
    elif qs[i] == 'B':
        b -= ps[i]
    maxi = max(maxi, b)

print(maxi)


def cnt(step):
    temp_total, ans = total, 0
    for i in range(N)[::step]:
        if chars[i] == 'A':
            temp_total += strengths[i]
        else:
            temp_total -= strengths[i]
        ans = max(temp_total, ans)
    return ans

N = int(input())
strengths = [int(_) for _ in input().split()]
chars = list(input())


total = 0
for i, c in enumerate(chars):
    if c == 'B':
        total += strengths[i]

print(max([total, cnt(1), cnt(-1)]))

#!/usr/bin/env python3

N = int(input())
arr = list(map(int, input().split()))
teams = input()

initial = sum(arr[i] for i in range(N) if teams[i] == 'B')
best = initial

# prefixes
cur = initial
for i in range(N):
    if teams[i] == 'B':
        cur -= arr[i]
    else:
        cur += arr[i]
        if cur > best:
            best = cur

# suffixes
cur = initial
for i in range(N-1, -1, -1):
    if teams[i] == 'B':
        cur -= arr[i]
    else:
        cur += arr[i]
        if cur > best:
            best = cur

print(best)


n = int(input())
A = list(map(int, input().split()))
b = input()
ans = 0
per = 0
start = 0
for i in range(n):
    if b[i] == 'B':
        start += A[i]
for i in range(n):
    if b[i] == 'A':
        per += A[i]
    else:
        per -= A[i]
    ans = max(per, ans)

per = 0
for i in range(n-1, -1,-1):
    if b[i] == 'A':
        per += A[i]
    else:
        per -= A[i]
    ans = max(per, ans)
print(start+ans)

3

n = int(input())
strength = [int(i) for i in input().split()]
s = input()

total = 0
for i in range(n):
	if s[i] == 'B':
		total += strength[i]
prefix = [0]*n
suffix = [0]*n
for i in range(n):
	if s[i] == 'A':
		if i == 0:
			prefix[i] = 0 + strength[i]
		else:
			prefix[i] = prefix[i-1] + strength[i]
	else:
		if i == 0:
			prefix[i] = 0 - strength[i]
		else:
			prefix[i] = prefix[i-1] - strength[i]

for i in range(n-1, -1, -1):
	if s[i] == 'A':
		if i == n-1:
			suffix[i] = 0 + strength[i]
		else:
			suffix[i] = suffix[i+1] + strength[i]
	else:
		if i == n-1:
			suffix[i] = 0 - strength[i]
		else:
			suffix[i] = suffix[i+1] - strength[i]
x = 0
for i in range(n):
	if prefix[i] > x:
		x = prefix[i]
for i in range(n):
	if suffix[i] > x:
		x = suffix[i]
#print(str(total))
#print(str(x))
total += x
print(str(total))


n = int(input())
power = list(map(int, input().split()))
s = input()
cnt = 0
for i in range(n):
    if s[i] == 'B':
        cnt += power[i]
ans = cnt
k = cnt
for i in range(n):
    if s[i] == 'B':
        k -= power[i]
    else:
        k += power[i]
    ans = max(ans, k)
    
k = cnt
for i in range(n - 1, -1, -1):
    if s[i] == 'B':
        k -= power[i]
    else:
        k += power[i]
    ans = max(ans, k)
print(ans)

n = int(input())
power = list(map(int, input().split()))
s = input()
cnt = 0
for i in range(n):
    if s[i] == 'B':
        cnt += power[i]
ans = cnt
k = cnt
for i in range(n):
    if s[i] == 'B':
        k -= power[i]
    else:
        k += power[i]
    ans = max(ans, k)
    
k = cnt
for i in range(n - 1, -1, -1):
    if s[i] == 'B':
        k -= power[i]
    else:
        k += power[i]
    ans = max(ans, k)
print(ans)

n = int(input())
P = list(map(int, input().split()))
S = list(input())
a_sum = 0
b_sum = 0
for i in range(n):
    if S[i] == 'A':
        a_sum += P[i]
    else:
        b_sum += P[i]

max_b = b_sum
b_sum_copy = b_sum
for i in range(n):
    if S[i] == 'A':
        b_sum += P[i]
    else:
        b_sum -= P[i]
    max_b = max(max_b, b_sum)

b_sum = b_sum_copy
for i in range(n-1,-1,-1):
    if S[i] == 'A':
        b_sum += P[i]
    else:
        b_sum -= P[i]
    max_b = max(max_b, b_sum)
print(max_b)

n = int(input())
p = [int(_) for _ in input().split()]
s = input()
ans = 0
for i in range(n):
    if s[i] == 'B':
        ans += p[i]
tmp = 0
mx = 0
for i in range(n):
    tmp += p[i] if s[i] == 'A' else -p[i]
    mx = max(tmp, mx)
tmp = 0
for i in range(n)[::-1]:
    tmp += p[i] if s[i] == 'A' else -p[i]
    mx = max(tmp, mx)
ans += mx
print(ans)


from sys import stdin
import operator

N = int(stdin.readline())

p = list(map(int, stdin.readline().strip().split(" ")))
teams = list([1 if x == "B" else -1 for x in stdin.readline().strip()])

values = list(map(operator.mul, p, teams))

start = sum([x for x in values if max(x, 0)])
# print(start)

total = start
best = start
for i in values:
	total -= i
	best = max(total, best)

total = start
for i in reversed(values):
	total -= i
	best = max(total, best)

# print(p, teams, values, best)
print(best)


power = 0

n = int(input())
powers = [ int(x) for x in (input().split(' ')) ]
alisa_move = input()



def power_at(i,powers):
    p = powers[i]
    return p
    


move = list(alisa_move)
move1 = []
move1.extend(reversed(list(alisa_move)))

p_max = 0
for i in range(0,n):
    if move[i] == 'B':
        p_max+=power_at(i,powers)


p_temp = p_max
for i in range(0,n):
    if move[i] == 'A':
        move[i] = 'B'
        p_temp += power_at(i,powers)
        if (p_max < p_temp):
            p_max = p_temp
    elif move[i] == 'B':
        move[i] = 'A'
        p_temp -= power_at(i,powers)
        

powers1=[]
powers1.extend(reversed(powers))



p_temp=0
for i in range(0,n):
    if move1[i] == 'B':
        p_temp+=power_at(i,powers1)



for i in range(0,n):
    if move1[i] == 'A':
        move1[i] = 'B'
        p_temp += power_at(i,powers1)
        if (p_max < p_temp):
            p_max = p_temp
    elif move1[i] == 'B':
        move1[i] = 'A'
        p_temp -= power_at(i,powers1)
            
print(p_max)

n = input()
s = [int(x) for x in input().split(' ')]
g = input()
A = 0
B = 0
for i in range(len(g)):
    if g[i] == 'A':
        A += s[i]
    else:
        B += s[i]

startA, startB = A, B
maximum = B

for i in range(len(g)):
    if g[i] == 'A':
        A -= s[i]
        B += s[i]
    else:
        A += s[i]
        B -= s[i]
    if B > maximum:
        maximum = B

A, B = startA, startB

for i in reversed(range(len(g))):
    if g[i] == 'A':
        A -= s[i]
        B += s[i]
    else:
        A += s[i]
        B -= s[i]
    if B > maximum:
        maximum = B

print(maximum)

n = int(input())
P = list(map(int, input().split()))
s = input()
res = sum(P[i] for i in range(n) if s[i] == 'B')
maxdiff = 0
suma = 0
sumb = 0
for i in range(n):
    if(s[i] == 'A'):
        suma += P[i]
    else:sumb += P[i]
    if(suma - sumb > maxdiff):
        maxdiff = suma - sumb

suma = 0
sumb = 0
for i in range(n)[::-1]:
    if(s[i] == 'A'):
        suma += P[i]
    else:sumb += P[i]
    if(suma - sumb > maxdiff):
        maxdiff = suma - sumb

print(res + maxdiff)


import sys
import math
# sys.stdin = open('input.txt')
# sys.stdout = open('output.txt', 'w')

def main():
    n = int(input())
    p = list(map(int, input().split()))
    s = input()
    prefa = [0] * n
    prefb = [0] * n
    for i in range(n):
        if i != 0:
            prefa[i] = prefa[i - 1]
            prefb[i] = prefb[i - 1]
        if s[i] == 'A':
            prefa[i] += p[i]
        else:
            prefb[i] += p[i]
    suffa = [0] * n
    suffb = [0] * n
    for i in reversed(range(n)):
        if i != n - 1:
            suffa[i] = suffa[i + 1]
            suffb[i] = suffb[i + 1]
        if s[i] == 'A':
            suffa[i] += p[i]
        else:
            suffb[i] += p[i]
    maxa = suffb[0]
    for i in range(n):
        t1 = prefa[i]
        if i != n - 1:
            t1 += suffb[i + 1]
        t2 = suffa[i]
        if i != 0:
            t2 += prefb[i - 1]
        maxa = max([maxa, t1, t2])
    print(maxa)

main()

n=int(input())
p=list(map(int,input().split()))
s=input()
maxi=0
temp=0
local=-1
for i in range(n):
    if s[i]=='A':
        temp+=p[i]
    else:
        temp-=p[i]
    if temp>maxi:
        local=i
        maxi=temp
ans=0

for i in range(local+1):
    if s[i]=='A':
        ans+=p[i]


for i in range(local+1,n):
    if s[i]=='B':
        ans+=p[i]

maxi=0
temp=0
local=-1
p=list(reversed(p))
s=s[::-1]
for i in range(n):
    if s[i]=='A':
        temp+=p[i]
    else:
        temp-=p[i]
    if temp>maxi:
        local=i
        maxi=temp
ans1=0
for i in range(local+1):
    if s[i]=='A':
        ans1+=p[i]
for i in range(local+1,n):
    if s[i]=='B':
        ans1+=p[i]

print(max(ans,ans1))


n = int(input())
powers = [int(n) for n in  input().split()]
marks = input()
Bob_power = sum([powers[i] for i in range(n) if marks[i] == 'B'])
max_Bob_power = Bob_power
start_Bob_power = Bob_power
for i in range(n):
    Bob_power += powers[i]*(1 if marks[i] == 'A' else -1)
    if max_Bob_power < Bob_power:
        max_Bob_power = Bob_power
Bob_power = start_Bob_power
for i in range(1, n + 1):
    Bob_power += powers[-i]*(1 if marks[-i] == 'A' else -1)
    if max_Bob_power < Bob_power:
        max_Bob_power = Bob_power
print(max_Bob_power)

def main():
    input()
    pp = list(map(int, input().split()))
    mask = [c == 'B' for c in input()]
    s = t = sum(p for p, m in zip(pp, mask) if m)
    res = [s]
    for p, m in zip(pp, mask):
        if m:
            s -= p
        else:
            s += p
            res.append(s)
    pp.reverse()
    mask.reverse()
    for p, m in zip(pp, mask):
        if m:
            t -= p
        else:
            t += p
            res.append(t)
    print(max(res))


def __starting_point():
    main()

__starting_point()