#include int main() { int n; scanf("%d", &n); char s[200005]; scanf("%s", s); int i; long long int a[200005]; for (i = 0; i < n; i++) scanf("%lld", &a[i]); for (i = 0; i < n; i++) if (s[i] == 'B') a[i] *= -1; long long int ans, min, max, sum; ans = min = max = sum = 0; for (i = 0; i < n; i++) { sum += a[i]; if (ans < max - sum) ans = max - sum; if (ans < sum - min) ans = sum - min; if (max < sum) max = sum; if (sum < min) min = sum; } printf("%lld\n", ans); return 0; }