#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

class Prints {
private:
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
        void operator()(Args&&... args) const { print(std::forward<Args>(args)...); }

    private:
        const char *sep, *term;
        void print() const { std::cout << term; }
        void print_rest() const { std::cout << term; }
        template <class T, class... Tail>
        void print(T&& head, Tail&&... tail) const { std::cout << head, print_rest(std::forward<Tail>(tail)...); }
        template <class T, class... Tail>
        void print_rest(T&& head, Tail&&... tail) const { std::cout << sep << head, print_rest(std::forward<Tail>(tail)...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
    template <class T>
    void print_vec(const std::vector<T>& vec, const char* sep = " ", const char* term = "\n") const {
        const size_t sz = vec.size();
        for (size_t i = 0; i < sz; i++) std::cout << vec[i] << (i == sz - 1 ? term : sep);
    }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
    ll N;
    cin >> N;
    Vl A(N);
    for (ll i = 0; i < N; i++) {
        cin >> A[i];
    }
    Vl cum(N + 1);
    for (ll i = 0; i < N; i++) {
        cum[i + 1] = cum[i] + A[i];
    }
    ll ans = L_INF;
    ll idx1 = 1, idx2 = 3;
    for (ll i = 2; i < N - 1; i++) {
        while (idx1 < i && cum[idx1] < cum[i] - cum[idx1]) {
            ++idx1;
        }
        while (idx2 < N - 1 && cum[idx2] - cum[i] < cum[N] - cum[idx2]) {
            ++idx2;
        }
        ll p1 = cum[idx1 - 1];
        ll p2 = cum[idx1];
        ll q1 = cum[i] - cum[idx1 - 1];
        ll q2 = cum[i] - cum[idx1];
        ll r1 = cum[idx2 - 1] - cum[i];
        ll r2 = cum[idx2] - cum[i];
        ll s1 = cum[N] - cum[idx2 - 1];
        ll s2 = cum[N] - cum[idx2];
        ll t = L_INF;
        t = min(t, max({p1, q1, r1, s1}) - min({p1, q1, r1, s1}));
        t = min(t, max({p2, q2, r1, s1}) - min({p2, q2, r1, s1}));
        t = min(t, max({p1, q1, r2, s2}) - min({p1, q1, r2, s2}));
        t = min(t, max({p2, q2, r2, s2}) - min({p2, q2, r2, s2}));
        ans = min(ans, t);
    }
    prints()(ans);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    ofstream ofs("./in_out/output.txt");
    cout.rdbuf(ofs.rdbuf());
#endif
    cout << setprecision(18) << fixed;
    solve();
    cout << flush;
    return 0;
}
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define repi(i,x) for(auto i=(x).begin(),i##_fin=(x).end();i!=i##_fin;i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int , int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
template <class T, class U>ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << "(" << p.first << "," << p.second << ")";
	return os;
}
template <class T>ostream& operator<<(ostream& os, const vector<T>& v) {
	os << "{";
	rep(i, v.size()) {
		if (i) os << ",";
		os << v[i];
	}
	os << "}";
	return os;
}
template <class T, class U>istream& operator>>(istream& is, pair<T, U>& p) {
	is >> p.first >>p.second ;
	return is;
}
template <class T>istream& operator>>(istream& is,  vector<T>& v) {
	rep(i, v.size()) {
		is >> v[i];
	}
	return is;
}
const long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
int main(){
	int n,q;
	string s;
//	int ans=0;
	int k;
	cin >>n>>s>>q;
	V data(2);//d,dm
	Vi sum(s.size()+1, 0);
	rep(i, s.size()) { 
		sum[i] += (s[i] == 'M'); 
	if(i!=0){
		sum[i] += sum[i - 1];
	}
	}
//	clog << sum << endl;
	ll ans = 0LL;
	rep(i, q) { 
		cin >> k;
		ans = 0LL;
		data[0] = 0LL;
		data[1] = 0LL;
		rep(i,s.size()){
			if (i >= k) {
				if (s[i - k] == 'D') {
					data[0]--;
					data[1] -= sum[i-1] - sum[i - k];
				
				}
			//	clog << "(" << s[i - k] << ") ";
			}
			switch (s[i])
			{
			case 'D':
				data[0]++;
				break;
			
			case 'M':
				data[1] += data[0];
				break;
			case 'C':
				ans += data[1];
				break;
			}
			
		//	clog << data << " "<<s[i]<<endl;
		}
		cout << ans << endl;
	}

}