#include using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < (n); i++) #define P pair const ll INF = 1e18; int main() { int n; string s; cin >> n >> s; vector x(n), sum(n+1), mx(n+1), mn(n+1); rep(i, n) { cin >> x[i]; if (s[i] == 'B') x[i] *= -1; } rep(i, n) sum[i+1] = sum[i] + x[i]; rep(i, n) { mx[i+1] = max(mx[i], sum[i+1]); mn[i+1] = min(mn[i], sum[i+1]); } ll ans = -1e18; for (int i = 1; i <= n; i++) { ans = max(ans, sum[i]-mn[i]); ans = max(ans, abs(sum[i]-mx[i])); } cout << ans << endl; }