#include #include #include using namespace std; typedef long long ll; int main(){ int n; string s; cin >> n >> s; vector e(n, 0); for(int i = 0; i < n; i++){ if(i) e[i] += e[i-1]; e[i] += s[i]=='E'; } vector sum(n); for(int i = 0; i < n; i++){ cin >> sum[i]; if(i) sum[i] += sum[i-1]; } map m; for(int i = 0; i < n; i++){ for(int j = i; j < n; j++){ int tmp = e[j] - (i==0 ? 0 : e[i-1]); if(tmp == 0) continue; ll p = sum[j] - (i==0 ? 0 : sum[i-1]); if(m.count(p)) m[p] = max(m[p], tmp); else m[p] = tmp; } } auto bef = m.begin(); for(auto it = ++m.begin(); it != m.end(); it++){ m[it->first] = max(it->second, bef->second); bef = it; } m[0] = 0; int q; cin >> q; while(q-- > 0){ ll k; cin >> k; cout << (--m.upper_bound(k))->second << endl; } return 0; }