#include // #include // NOTE: AtCoderライブラリ using namespace std; // using namespace atcoder; // NOTE: AtCoderライブラリ #define fi first #define se second #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; i--) #define dbg(x) cerr << #x << ": " << x << endl; #define dbg2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl; #define dbg3(x, y, z) cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << endl; #define nl() cout << "========================================" << endl; #define yn { puts("Yes"); } else{ puts("No"); } // 使い方: if (条件) yn; typedef long long ll; typedef long double ld; typedef pair pii; typedef pair pll; typedef vector vi; typedef vector vl; typedef vector vb; typedef vector vc; typedef vector vs; typedef vector> vvi; typedef vector> vvl; typedef vector> vvb; typedef vector> vvc; typedef vector> vpii; typedef vector> vpll; const long long LINF = 1001002003004005006ll; const int INF = 1001001001; template void chmin(T &a, T b) { if (a > b) a = b; } template void chmax(T &a, T b) { if (a < b) a = b; } int main() { int N; cin >> N; string S; cin >> S; vl A(N); rep(i, N) cin >> A[i]; rep(i, N) { if (S[i] == 'B') A[i] *= -1; } ll res = -LINF; vl dp(N); dp[0] = A[0]; chmax(res, dp[0]); for (int i = 1; i < N; i++) { dp[i] = max(A[i], dp[i - 1] + A[i]); chmax(res, dp[i]); } rep(i, N) A[i] *= -1; vl dp2(N); dp2[0] = A[0]; chmax(res, dp2[0]); for (int i = 1; i < N; i++) { dp2[i] = max(A[i], dp2[i - 1] + A[i]); chmax(res, dp2[i]); } cout << res << endl; }