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] print(abs(max(B)-min(B)))