#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N; string S; cin >> N >> S; vector a(N); rep(i,N) { cin >> a[i]; if(S[i] == 'B') a[i] *= -1; } vector a_sum(N + 1, 0); rep(i,N) a_sum[i + 1] += a_sum[i] + a[i]; ll mi = 0, ma = 0, ans = 0; rep(i,N+1) { ans = max({ans, abs(mi - a_sum[i]), abs(ma - a_sum[i])}); mi = min(mi, a_sum[i]); ma = max(ma, a_sum[i]); } cout << ans << endl; }