#include #include #include using namespace std; int MINIMUM = -1000000001; int main() { int N; cin >> N; string S; cin >> S; vector a(N); for (int i = 0; i < N; i++) { cin >> a.at(i); } for (int i = 0; i < N; i++) { if (S.at(i) == 'B') { a.at(i) *= (-1); } } vector plusdp(N); vector minusdp(N); plusdp.at(0) = a.at(0); minusdp.at(0) = (-1) * a.at(0); for (int i = 0; i < N-1; i++) { plusdp.at(i + 1) = max(a.at(i+1), plusdp.at(i) + a.at(i+1)); minusdp.at(i + 1) = max((-1)*a.at(i + 1), minusdp.at(i) - a.at(i + 1)); } /*for (int i = 0; i < N; i++) { cout << plusdp.at(i) << " "; } cout << endl; for (int i = 0; i < N; i++) { cout << minusdp.at(i) << " "; } cout << endl;*/ int MAX = MINIMUM; for (int i = 0; i < N; i++) { if (plusdp.at(i) > MAX) { MAX = plusdp.at(i); } if (minusdp.at(i) > MAX) { MAX = minusdp.at(i); } } cout << abs(MAX); }