#pragma GCC optimize("O3,unroll-loops") #include #include #include using namespace std ; using namespace __gnu_pbds ; #define FAST_IO ios::sync_with_stdio(false); cin.tie(nullptr); template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; // Ordered Set (PBDS): Used when need indexing in sets for keys NOT duplicates. To use dups; replace 'T' with pair and use counter for 2nd with increasing value. // os.order_of_key(x) -> # elements strictly < x (like lower_bound index) // osind_by_order(k) -> iterator to k-th smallest (0-based) // Think of: // order_of_key(x) -> "index of x" if sorted // find_by_order(k) -> "element at index k" // Use for: rank queries, inversion count, kth smallest/largest, etc. #define int long long using pi = pair ; using ppi = pair ; using vi = vector ; using vb = vector ; using vs = vector ; using vpi = vector ; using vvi = vector ; template using ai = array; const int MOD = 1e9 + 7 ; const int INF = LLONG_MAX ; #define all(x) begin(x), end(x) #define nl "\n" #define f first #define ss second void yesNo( bool b ) { cout << ( b ? "YES" : "NO" ) << nl ; } int binaryExp( int base , int exp , int mod ) { int res = 1LL ; base %= mod ; while ( exp > 0 ) { if ( exp & 1 ) res = (res * base) % mod ; base = (base * base) % mod ; exp >>= 1LL ; } return res ; } vi sieve( int n ) { vb isPrime( n + 1 , true ) ; isPrime[0] = isPrime[1] = false ; vi primes ; for( int i = 2 ; i * i <= n ; i++ ) { if( isPrime[i] ) { primes.push_back( i ) ; for( int j = i * i ; j <= n ; j += i ) isPrime[j] = false ; } } return primes ; } // int check( int n ) { // if( n == 1 ) // return 0 ; // // else if( n < 1 ) return 1 // int cur = 0 ; // for( int i = 1 ; i <= n ; i++ ) { // if( !check( n - 2 ) || !check( n - 3 ) || !check( n - 5 ) ) { // return 1 ; // } // } // return 0 ; // } void solve() { int n ; cin >> n ; string s ; cin >> s ; vi a(n) ; vpi pref( n + 1 , { 0 , 0 } ) ; pref[0] = { 0 , 0 }; for( int i = 0 ; i < n ; i++ ) { cin >> a[i] ; if( s[i] == 'E' ) { pref[i + 1].f += pref[i].f + a[i] ; pref[i + 1].ss += pref[i].ss + 1 ; } else { pref[i + 1].f += pref[i].f + a[i] ; pref[i + 1].ss += pref[i].ss ; } } // for( int i = 0 ; i <= n ; i++ ) cout << pref[i].f << " \n"[i == n] ; // for( int i = 0 ; i <= n ; i++ ) cout << pref[i].ss << " \n"[i == n] ; int q ; cin >> q ; vi b(n) ; for( int i = 0 ; i < q ;i++ ) cin >> b[i] ; for( int i = 0 ; i < q ; i++ ) { int res = 0 ; for( int j = 0 ; j < n ; j++ ) { // int l = 1 , r = n ; // while( l <= r ) { // int md = l + (r - l) / 2 ; // if( pref[md].f <= b[i] ) { // l = md + 1 ; // res = pref[md].ss ; // } // else // r = md - 1 ; // } // cout << res << nl ; int tot = n , sum = 0 , cur = 0 ; for( int k = 1 ; k < n ; k++ ) { if( a[( j + k ) % n] + sum <= b[i] ) sum += a[ ( j + k ) % n ] ; else break ; if( s[ ( j + k ) % n ] == 'E' ) cur++ ; } res = max( res, cur ) ; } cout << res << nl ; } // int n , m ; // cin >> n >> m ; // vvi adj(n + 1) ; // for( int i = 1; i <= m ; i++ ) { // int u , v ; // cin >> u >> v ; // adj[u].push_back(v) ; // adj[v].push_back(u) ; // } // int cur = 0 ; // for( int i = 1 ; i <= n ; i++ ) cur += adj[i].size() ; // cur /= 2 ; // if( cur + 1 < n ) { // yesNo(0) ; // return ; // } // yesNo(1) ; } signed main() { FAST_IO if ( FILE* file = fopen("input.txt", "r") ) { freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout) ; fclose(file) ; } int t = 1 ; // cin >> t ; for( int i = 1 ; i <= t ; i++ ) { // cout << "Case " << i << ": " << nl ; solve() ; } return 0; }