#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n,a) cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev            coyhhhhhhyoc    
#define next          sdNNNmNNNNNNNmds  
#define y0           hNNNNy_yNNNNNN_sNh
#define y1          mNNNNNdtdNNNNNNtsNNm
#define yn          mNNNNNNNNy___smNNNms
#define tm            oooooosyysooooot 
#define read               tyhyt        
#define rank              ytmNmo         
#define index              yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define MOD 1000000007
#define endl "\n"
#define int long long
typedef long long ll;
using namespace std;

const int N = 222222;
int a[N];
int n;

int kad() {
    int ans = -1e18;
    rep(i, 1, n) {
        ans = max(ans, a[i]);
    }
    int cur = 0, cnt = 0;
    rep(i, 1, n) {
        cur += a[i];
        cnt++;
        if(cur < 0) {
            cur = 0;
            cnt = 0;
        }
        if(cnt) {
            ans = max(ans, cur);
        }
    }
    return ans;
}

int solve() {
    string s;
    cin >> n >> s;
    rep(i, 1, n) {
        cin >> a[i];
        if(s[i - 1] == 'B') {
            a[i] *= -1;
        }
    }
    int ans = kad();
    rep(i, 1, n) {
        a[i] *= -1;
    }
    ans = max(ans, kad());
    cout << ans << endl;
    return 0;
}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    while(t--) {
        solve();
    }
    return 0;
}