#include using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(bool b) { return b ? "true" : "false"; } template string to_string(pair p); template string to_string(C c) { string res = "{"; for (auto e : c) res += to_string(e) + ", "; return res += "}"; } template string to_string(bitset bs) { string res; for (size_t i = 0; i < N; ++i) res += '0' + bs[i]; return res; } template string to_string(pair p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } void debug() { cerr << '\n'; } template void debug(Head head, Tail... tail) { cerr << ' ' << to_string(head), debug(tail...); } #ifdef LOCAL #define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__) #else #define DEBUG(...) #endif int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; string s; cin >> s; vector a(n); for (auto&& e : a) { cin >> e; } a.push_back(0); for (int i = n - 1; i >= 0; --i) { a[i] += a[i + 1]; } vector c(n + 1); for (int i = n - 1; i >= 0; --i) { c[i] = (s[i] == 'E') + c[i + 1]; } vector mn(n + 1, 1e18); for (int r = 0; r <= n; ++r) { for (int l = 0; l <= r; ++l) { int x = c[l] - c[r]; mn[x] = min(mn[x], a[l] - a[r]); } } DEBUG(mn); int q; cin >> q; while (q--) { int k; cin >> k; int res = prev(upper_bound(begin(mn), end(mn), k)) - begin(mn); cout << res << '\n'; } }