#include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

typedef long long ll;

int main() {
    int N, Q;
    string S;
    cin >> N >> S >> Q;
    vector<int> k(Q);
    rep(i, Q) cin >> k[i];
    vector<ll> Dsum(N + 1, 0), Msum(N + 1, 0), DM(N + 1, 0);
    rep(i, N) {
        Dsum[i + 1] = Dsum[i];
        if (S[i] == 'D') Dsum[i + 1]++;
        Msum[i + 1] = Msum[i];
        if (S[i] == 'M') Msum[i + 1]++;
    }
    rep(i, Q) {
        ll ans = 0, DM = 0;
        rep(j, N) {
            int j0 = j - k[i];
            if (j0 >= 0 && S[j0] == 'D') DM -= Msum[j + 1] - Msum[j0];
            if (S[j] == 'M') DM += Dsum[j] - Dsum[max(0, j0)];
            if (S[j] == 'C') ans += DM;
        }
        cout << ans << endl;
    }
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

void chmax(string &a, string b) {
  if (a == "-") { a = b; }
  else if (a.size() < b.size()) { a = b; }
  else if (a.size() == b.size()) {
    if (a < b) { a = b; }
  }
}

int main(){
  int match[] = {-1,2,5,5,4,5,6,3,7,6};
  int n, m;
  cin >> n >> m;

  vector<int> a(m);
  for (int i = 0; i < m; i++) { cin >> a[i]; }

  vector<string> dp(n+10, "-");
  dp[0] = "";
  for (int i = 0; i < n; i++) {
    if (dp[i] == "-") { continue; }
    for (int j = 0; j < m; j++) {
      chmax(dp[i + match[a[j]]], dp[i] + (char)('0'+a[j]));
    }
  }
  cout << dp[n] << endl;
  return 0;
}