def min(a,b); a < b ? a : b; end def max(a,b); a > b ? a : b; end N = gets.to_i S = gets.chomp A = gets.split.map(&:to_i) Q = gets.to_i K = gets.split.map(&:to_i) E = Array.new(N + 1, 0) H = Array.new(N + 1, 0) N.times do |i| E[i + 1] = E[i] + (S[i] == 'E' ? 1 : 0) H[i + 1] = H[i] + A[i] end dp = Array.new(E[N] + 10, Float::INFINITY) dp[0] = 0 (1 .. N).each do |i| (0 ... i).each do |j| e = E[i] - E[j] k = H[i] - H[j] dp[e] = min(dp[e], k) end end puts K.map{|k| (0 .. N).bsearch{|n| dp[n + 1] > k } }