Problem p00549 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()

const ll mod = 1e9 + 7;
const ll INF = 1 << 30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n;
string s;
int cntj[100005], cnti[100005];
int dp[100005][4];
int ans;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  cin >> s;
  rep(i, n) {
    char c = s[i];
    if (c == 'J')
      cntj[i] = 1;
    else if (c == 'I')
      cnti[i] = 1;
  }
  rep(i, n - 1) {
    cntj[i + 1] += cntj[i];
    cnti[n - 2 - i] += cnti[n - 1 - i];
  }
  int idx = -1;
  int cnt = 0;
  rep(i, n - 1) {
    if (cnt < cntj[i] * cnti[i + 1]) {
      cnt = cntj[i] * cnti[i + 1];
      idx = i + 1;
    }
  }
  int ans = 0;
  rep(k, 3) {
    string tmps = s;
    if (k == 0)
      tmps = 'J' + tmps;
    else if (k == 1)
      tmps = tmps + 'I';
    else {
      if (idx == -1)
        break;
      tmps.insert(tmps.begin() + idx, 'O');
    }
    fill(dp[0], dp[n + 2], 0);
    dp[0][0] = 1;
    rep(i, n + 1) rep(j, 4) {
      char c = tmps[i];
      dp[i + 1][j] += dp[i][j];
      if (c == 'J') {
        if (j == 1)
          dp[i + 1][j] += dp[i][j - 1];
      } else if (c == 'O') {
        if (j == 2)
          dp[i + 1][j] += dp[i][j - 1];
      } else {
        if (j == 3)
          dp[i + 1][j] += dp[i][j - 1];
      }
    }
    ans = max(ans, dp[n + 1][3]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()

const ll mod = 1e9 + 7;
const ll INF = 1 << 30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n;
string s;
int cntj[100005], cnti[100005];
int dp[100005][4];
int ans;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  cin >> s;
  rep(i, n) {
    char c = s[i];
    if (c == 'J')
      cntj[i] = 1;
    else if (c == 'I')
      cnti[i] = 1;
  }
  rep(i, n - 1) {
    cntj[i + 1] += cntj[i];
    cnti[n - 2 - i] += cnti[n - 1 - i];
  }
  int idx = -1;
  int cnt = 0;
  rep(i, n - 1) {
    if (cnt < cntj[i] * cnti[i + 1]) {
      cnt = cntj[i] * cnti[i + 1];
      idx = i + 1;
    }
  }
  int ans = 0;
  dp[0][0] = 1;
  rep(k, 3) {
    string tmps = s;
    if (k == 0)
      tmps = 'J' + tmps;
    else if (k == 1)
      tmps = tmps + 'I';
    else
      tmps.insert(tmps.begin() + idx, 'O');
    fill(dp[0], dp[n + 2], 0);
    dp[0][0] = 1;
    rep(i, n + 1) rep(j, 4) {
      char c = tmps[i];
      dp[i + 1][j] += dp[i][j];
      if (c == 'J') {
        if (j == 1)
          dp[i + 1][j] += dp[i][j - 1];
      } else if (c == 'O') {
        if (j == 2)
          dp[i + 1][j] += dp[i][j - 1];
      } else {
        if (j == 3)
          dp[i + 1][j] += dp[i][j - 1];
      }
    }
    ans = max(ans, dp[n + 1][3]);
  }
  cout << ans << endl;
}