from atcoder.segtree import SegTree N = int(input()) S = input() A = list(map(int, input().split())) B = [0] * (N + 1) for i in range(N): if S[i] == "B": A[i] = -A[i] B[i + 1] = B[i] + A[i] INF = 10 ** 15 ST1 = SegTree(max, -INF, B) ST2 = SegTree(min, INF, B) ans = 0 for i in range(N + 1): ans = max(ans, abs(ST1.prod(i, N + 1) - B[i])) ans = max(ans, abs(ST2.prod(i, N + 1) - B[i])) print(ans)