n = int(input()) S = input() A = list(map(int, input().split())) minv = 0 maxv = 0 v = 0 ans = 0 for i in range(n): if S[i] == 'R': v += A[i] elif S[i] == 'B': v -= A[i] ans = max(ans, abs(v - minv)) ans = max(ans, abs(v - maxv)) minv = min(minv, v) maxv = max(maxv, v) print(ans)