#include #define rep(i,n) for (int i = 0; i < n; ++i) #define ALL(c) (c).begin(), (c).end() #define SUM(x) std::accumulate(ALL(x), 0LL) #define MIN(v) *std::min_element(v.begin(), v.end()) #define MAX(v) *std::max_element(v.begin(), v.end()) #define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end()) using namespace std; using ll = long long; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline void dump(vector v) { for (auto& x : v) cerr << x << " "; cerr << endl; } template inline void dump(vector> v) { for (auto& p : v) cerr << p.first << " " << p.second << endl; } template inline void dump(vector> vv) { for (auto& v : vv) {for (auto& x : v) cerr << x << " "; cerr << endl;} } const int INF = 1e9; const long long INFL = 1LL<<60; template class SegmentTree { using Fx = function; Fx fx; const T init; int n; // 最下段のノード数 vector node; public: SegmentTree(const vector& v, Fx f, T ini) : fx(f), init(ini) { n = 1; while (n < v.size()) n <<= 1; // N: 最下段の横幅 node.resize(2 * n - 1, init); // init で初期化 // 最下段に値を挿入 rep(i, v.size()) node[(n - 1) + i] = v[i]; // 最下段以外を更新していく for (int i = n - 2; i >= 0; i--) { node[i] = fx(node[i * 2 + 1], node[i * 2 + 2]); } } void update(int i, T x) { i += n - 1; // i 番目の葉のノード番号 node[i] = x; while (i > 0) { i = (i - 1) / 2; // ノード i の親ノードの番号に変える node[i] = fx(node[i * 2 + 1], node[i * 2 + 2]); // 左右の子の min を計算し直す } } void add(int i, T x) { i += n - 1; // i 番目の葉のノード番号 node[i] += x; while (i > 0) { i = (i - 1) / 2; // ノード i の親ノードの番号に変える node[i] = fx(node[i * 2 + 1], node[i * 2 + 2]); } } // [a, b) の区間に対するクエリについてノード k (区間 [l, r) 相当) が答える // 注意 [a, b) は半区間なので 区間 [m, n] の最小値を求めたい場合は find(m, n+1) とする T find(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return init; // 区間がかぶらない場合は init を返す if (a <= l && r <= b) { return node[k]; // ノード k の担当範囲がクエリ区間 [a, b) に完全に含まれる } else { T c1 = find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子に値を聞く T c2 = find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子に値を聞く return fx(c1, c2); } } T operator[](const int& i) const { return node[i + n - 1]; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n = 0; cin >> n; string s; cin >> s; vector a(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; vector csum_r(n + 1, 0), csum_b(n + 1, 0); rep(i, n) { csum_r[i + 1] = csum_r[i]; csum_b[i + 1] = csum_b[i]; if (s[i] == 'R') csum_r[i + 1] += a[i + 1]; else csum_b[i + 1] += a[i + 1]; } vector csum_r_b(n+1), csum_b_r(n+1); rep(i, n+1) { csum_r_b[i] = csum_r[i] - csum_b[i]; csum_b_r[i] = csum_b[i] - csum_r[i]; } auto f_ma = [](ll x1, ll x2) { return max(x1, x2); }; SegmentTree seg_r_b(csum_r_b, f_ma, -INFL), seg_b_r(csum_b_r, f_ma, -INFL); ll ans = 0; rep(i, n) { ll tmp = seg_r_b.find(i+1, n + 1); chmax(ans, tmp - csum_r_b[i]); } rep(i, n) { ll tmp = seg_b_r.find(i+1, n + 1); chmax(ans, tmp - csum_b_r[i]); } cout << ans << endl; return 0; }