#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i, m, n) for(int i=int(m);i inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } typedef long long int ll; using ll = long long int; using ull = long long unsigned int; using Int = long long int; using namespace std; #define INF (1 << 30) - 1 #define INFl (ll)5e15 #define DEBUG 0 #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 //edit class Solve { public: void solve() { Int N; cin >> N; string S; cin >> S; vector A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } vector X(N + 1, INFl); // i個倒す最小値 for (int i = 0; i < N; ++i) { Int cost = 0; Int val = 0; for (int j = i; j < N; ++j) { if (S[j] == 'E') val++; cost += A[j]; chmin(X[val], cost); } } for (int i = N - 1; i > 0; --i) { chmin(X[i - 1], X[i]); } X[0] = 0; Int Q; cin >> Q; while (Q--) { Int k; cin >> k; Int ans = static_cast(upper_bound(all(X), k) - X.begin() - 1); cout << ans << endl; } } }; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); Solve().solve(); return 0; }