#include // #include // using namespace atcoder; #define rep(i,n) for(int i=0;i 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; } templateauto MAX(const T& a) { return *max_element(a.begin(),a.end()); } templateauto MIN(const T& a) { return *min_element(a.begin(),a.end()); } templateU SUM(const T& a, const U& v) { return accumulate(a.begin(),a.end(), v); } templateU COUNT(const T& a, const U& v) { return count(a.begin(),a.end(), v); } templateint LOWER(const T& a, const U& v) { return lower_bound(a.begin(),a.end(), v) - a.begin(); } templateint UPPER(const T& a, const U& v) { return upper_bound(a.begin(),a.end(), v) - a.begin(); } int GCD(int a, int b) { return b ? GCD(b, a%b) : a; } int LCM(int a, int b) { int g = GCD(a, b); return a / g * b; } typedef long double ld; typedef unsigned long long int ull; typedef pair P; typedef vector vi; typedef vector vc; typedef vector vb; typedef vector vd; typedef vector vs; typedef vector vll; typedef vector> vpii; typedef vector> vpll; typedef vector vvi; typedef vector vvvi; typedef vector vvc; typedef vector vvs; typedef vector vvll; typedef map mii; typedef set si; //--------------------------------------------------------------------------------------------------- int main(void){ // Your code here! int n; cin >> n; string s; cin >> s; vll a(n); rep(i,n) cin >> a[i]; rep(i,n) { if (s[i] == 'B') a[i] = -a[i]; } ll ans = 0; int right = 0; ll sum = 0; rep(left,n) { while(right < n && sum + a[right] >= 0) { sum += a[right]; ans = max(ans,sum); right++; } ans = max(ans,sum); if (right == left) ++right; else sum -= a[left]; } rep(i,n) a[i] = -a[i]; right = 0; sum = 0; rep(left,n) { while(right < n && sum + a[right] >= 0) { sum += a[right]; ans = max(ans,sum); right++; } ans = max(ans,sum); if (right == left) ++right; else sum -= a[left]; } // rep(i,n) { // if (now >= 0) { // if (a[i] == 0) continue; // if (a[i] > 0) { // now+= a[i]; // } else { // ans = max(ans,now); // now = a[i]; // } // } else { // if (a[i] == 0) continue; // if (a[i] < 0) { // now+= a[i]; // } else { // ans = max(ans,-now); // now = a[i]; // } // } // } cout << ans << endl; }