#include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; ll calc(string s, vector a){ int n = a.size(); vector cumsum(n+1); for(int i = 0; i < n; i++){ if(s[i] == 'R') cumsum[i+1] = cumsum[i]+a[i]; else if(s[i] == 'B') cumsum[i+1] = cumsum[i]-a[i]; } ll ans = 0; ll cur = 0; for(int i = 1; i <= n; i++){ chmax(ans, cumsum[i]-cur); chmin(cur, cumsum[i]); } return ans; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n; cin >> n; string s; cin >> s; vector a(n); for(int i = 0; i < n; i++){ cin >> a[i]; } ll ans1 = calc(s, a); for(int i = 0; i < n; i++) a[i] = -a[i]; ll ans2 = calc(s, a); cout << max(ans1, ans2) << endl; }