Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define int long long
#define fi first
#define se second
using namespace std;
const long long INF = 1e18;
const long long MD = 1e9 + 7;
int n;
string s;
int a[1000006], pref[100006][4], dp[100006][4];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> s;
  cin >> n;
  vector<int> k;
  for (int i = 0; i < n; i++) {
    int n1;
    cin >> n1;
    k.push_back(n1);
  }
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'D') {
      a[i] = 1;
    } else if (s[i] == 'M') {
      a[i] = 2;
    } else if (s[i] == 'C') {
      a[i] = 3;
    } else
      a[i] = 0;
  }
  /*cout<<"OK"<<endl;
  cout<<s.size()<<endl;
   */
  for (int i = 0; i < s.size(); i++) {
    if (a[i] == 0 && i != 0) {
      pref[i][1] += pref[i - 1][1];
      pref[i][2] += pref[i - 1][2];
      continue;
    }
    pref[i][a[i]]++;
    if (i != 0) {
      pref[i][1] += pref[i - 1][1];
      pref[i][2] += pref[i - 1][2];
    }
  }
  for (int i = 0; i < s.size(); i++) {
    if (a[i] == 1)
      dp[i][1]++;
    if (a[i] == 2 && i != 0)
      dp[i][2] += dp[i - 1][1];
    if (a[i] == 3 && i != 0)
      dp[i][3] += dp[i - 1][2];
    if (i != 0) {
      dp[i][1] += dp[i - 1][1];
      dp[i][2] += dp[i - 1][2];
      dp[i][3] += dp[i - 1][3];
    }
  }
  for (auto kk : k) {
    int ans = 0;
    for (int i = kk; i < s.size(); i++) {
      if (a[i] == 3 && i != 0) {

        int p1 = pref[i - 1][2] - pref[i - kk][2];
        int D = pref[i - kk][1];
        // cout << p1 << " " << D << endl;
        ans += dp[i - kk][2] + D * p1;
      }
    }
    // cout<<ans<<endl;
    cout << dp[s.size() - 1][3] - ans << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
#define int long long
#define fi first
#define se second
using namespace std;
const long long INF = 1e18;
const long long MD = 1e9 + 7;
int n;
string s;
int a[1000006], pref[1000006][4], dp[1000006][4];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> s;
  cin >> n;
  vector<int> k;
  for (int i = 0; i < n; i++) {
    int n1;
    cin >> n1;
    k.push_back(n1);
  }
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'D') {
      a[i] = 1;
    } else if (s[i] == 'M') {
      a[i] = 2;
    } else if (s[i] == 'C') {
      a[i] = 3;
    } else
      a[i] = 0;
  }
  /*cout<<"OK"<<endl;
  cout<<s.size()<<endl;
   */
  for (int i = 0; i < s.size(); i++) {
    if (a[i] == 0 && i != 0) {
      pref[i][1] += pref[i - 1][1];
      pref[i][2] += pref[i - 1][2];
      continue;
    }
    pref[i][a[i]]++;
    if (i != 0) {
      pref[i][1] += pref[i - 1][1];
      pref[i][2] += pref[i - 1][2];
    }
  }
  for (int i = 0; i < s.size(); i++) {
    if (a[i] == 1)
      dp[i][1]++;
    if (a[i] == 2 && i != 0)
      dp[i][2] += dp[i - 1][1];
    if (a[i] == 3 && i != 0)
      dp[i][3] += dp[i - 1][2];
    if (i != 0) {
      dp[i][1] += dp[i - 1][1];
      dp[i][2] += dp[i - 1][2];
      dp[i][3] += dp[i - 1][3];
    }
  }
  for (auto kk : k) {
    int ans = 0;
    for (int i = kk; i < s.size(); i++) {
      if (a[i] == 3 && i != 0) {

        int p1 = pref[i - 1][2] - pref[i - kk][2];
        int D = pref[i - kk][1];
        // cout << p1 << " " << D << endl;
        ans += dp[i - kk][2] + D * p1;
      }
    }
    // cout<<ans<<endl;
    cout << dp[s.size() - 1][3] - ans << endl;
  }
}