#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } signed main(){ int N;cin>>N; string S;cin>>S; vector A(N); cin>>A; ll res=0; { vector sum(N+1,0); rep(i,N){ if(S[i]=='R') sum[i+1]=sum[i]+A[i]; else sum[i+1]=sum[i]-A[i]; } ll mi=0; rep(i,N){ chmax(res,sum[i+1]-mi); chmin(mi,sum[i+1]); } } { vector sum(N+1,0); rep(i,N){ if(S[i]=='B') sum[i+1]=sum[i]+A[i]; else sum[i+1]=sum[i]-A[i]; } ll mi=0; rep(i,N){ chmax(res,sum[i+1]-mi); chmin(mi,sum[i+1]); } } cout<