#!/usr/bin/env python3 import sys def main(): import itertools N = int(input()) S = input() A = list(map(int, input().split())) RmB = [] mRB = [] for i in range(N): if S[i] == "B": RmB.append(-A[i]) mRB.append(A[i]) else: RmB.append(A[i]) mRB.append(-A[i]) sRmB = [0] + list(itertools.accumulate(RmB)) smRB = [0] + list(itertools.accumulate(mRB)) # print(RmB) # print(mRB) # print(sRmB) # print(smRB) minS = 10 ** 10 maxS = -1 * 10 ** 10 ans = -1 * 10 ** 10 for i in range(N + 1): # print(sRmB[i], minS) if sRmB[i] <= minS: ans = max(ans, maxS - minS) minS = sRmB[i] maxS = -1 * 10 ** 10 maxS = max(sRmB[i], maxS) ans = max(ans, maxS - minS) minS = 10 ** 10 maxS = -1 * 10 ** 10 for i in range(N + 1): # print(smRB[i], minS) if smRB[i] <= minS: ans = max(ans, maxS - minS) minS = smRB[i] maxS = -1 * 10 ** 10 maxS = max(smRB[i], maxS) ans = max(ans, maxS - minS) print(ans) return if __name__ == '__main__': main()