n = int(input()) s = input() a = list(map(int, input().split())) for i in range(n): if s[i] == 'B': a[i] = - a[i] s = [0 for _ in range(n + 1)] for i in range(n): s[i + 1] = s[i] + a[i] ub = s[0] lb = s[0] ans = - 1 << 60 for i in range(1, n + 1): ans = max(ans, s[i] - lb, abs(s[i] - ub)) ub = max(ub, s[i]) lb = min(lb, s[i]) print(ans)