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

//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

// 1. dp?????????
//???J?????????O ?????????I
//???????????????????????????????????????
//??????????????????????????????????????
//??????????????????????????????????????
//??????????????????????????????????????
//?????????????????????????????????????
//?????????????????????????????????????
//?????????????????????????????????????
//????????????????????????????????????

int dp[9][1001];
// 2. n?????????
int N;
char D[1001];

// 7. calc?????????
int calc(int k, int m) {
  // 8. dp???????????°??????
  if (dp[k][m] >= 0)
    return dp[k][m];
  // 9. dp????¨????

  if (k == 1)
    dp[k][m] = calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) +
               calc(4, m - 1) + calc(5, m - 1) + calc(6, m - 1) +
               calc(7, m - 1);
  if (k == 2)
    dp[k][m] = calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) +
               calc(4, m - 1) + calc(6, m - 1) + calc(7, m - 1);
  if (k == 3)
    dp[k][m] = calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) +
               calc(4, m - 1) + calc(5, m - 1) + calc(6, m - 1);
  if (k == 4)
    dp[k][m] = calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) +
               calc(4, m - 1) + calc(5, m - 1) + calc(7, m - 1);
  if (k == 5)
    dp[k][m] =
        calc(1, m - 1) + calc(3, m - 1) + calc(4, m - 1) + calc(5, m - 1);
  if (k == 6)
    dp[k][m] =
        calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) + calc(6, m - 1);
  if (k == 7)
    dp[k][m] =
        calc(1, m - 1) + calc(2, m - 1) + calc(4, m - 1) + calc(7, m - 1);
  dp[k][m] = dp[k][m] % 10007;
  return dp[k][m];
}
int main() {
  cin >> N;

  // 3.?????????
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 1001; j++) {
      dp[i][j] = -1;
    }
  }
  // 4.??\\???
  for (int i = 1; i < N + 1; i++) {
    cin >> D[i];
    if (D[i] == 'J') {
      dp[4][i] = 0;
      dp[5][i] = 0;
      dp[7][i] = 0;
    }
    if (D[i] == 'O') {
      dp[3][i] = 0;
      dp[5][i] = 0;
      dp[6][i] = 0;
    }
    if (D[i] == 'I') {
      dp[2][i] = 0;
      dp[6][i] = 0;
      dp[7][i] = 0;
    }
  }

  // 5.???????????¨???
  dp[1][1] = 1;
  dp[2][1] = 1;
  dp[3][1] = 1;
  dp[6][1] = 1;
  dp[4][1] = 0;
  dp[5][1] = 0;
  dp[7][1] = 0;

  if (D[1] == 'O') {
    dp[3][1] = 0;
    dp[6][1] = 0;
  }
  if (D[1] == 'I') {
    dp[2][1] = 0;
    dp[6][1] = 0;
  }

  // 6.??????
  int total = 0;
  for (int i = 1; i < 8; i++) {
    total += calc(i, N);
    total = total % 10007;
  }
  cout << total << endl;

  return 0;
}

Error: -11

Fixed submission:

//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

// 1. dp?????????
//???J?????????O ?????????I
//???????????????????????????????????????
//??????????????????????????????????????
//??????????????????????????????????????
//??????????????????????????????????????
//?????????????????????????????????????
//?????????????????????????????????????
//?????????????????????????????????????
//????????????????????????????????????

int dp[9][1001];
// 2. n?????????
int N;
char D[1001];

// 7. calc?????????
int calc(int k, int m) {
  // 8. dp???????????°??????
  if (dp[k][m] >= 0)
    return dp[k][m];
  // 9. dp????¨????

  if (k == 1)
    dp[k][m] = calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) +
               calc(4, m - 1) + calc(5, m - 1) + calc(6, m - 1) +
               calc(7, m - 1);
  if (k == 2)
    dp[k][m] = calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) +
               calc(4, m - 1) + calc(6, m - 1) + calc(7, m - 1);
  if (k == 3)
    dp[k][m] = calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) +
               calc(4, m - 1) + calc(5, m - 1) + calc(6, m - 1);
  if (k == 4)
    dp[k][m] = calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) +
               calc(4, m - 1) + calc(5, m - 1) + calc(7, m - 1);
  if (k == 5)
    dp[k][m] =
        calc(1, m - 1) + calc(3, m - 1) + calc(4, m - 1) + calc(5, m - 1);
  if (k == 6)
    dp[k][m] =
        calc(1, m - 1) + calc(2, m - 1) + calc(3, m - 1) + calc(6, m - 1);
  if (k == 7)
    dp[k][m] =
        calc(1, m - 1) + calc(2, m - 1) + calc(4, m - 1) + calc(7, m - 1);
  dp[k][m] = dp[k][m] % 10007;
  return dp[k][m];
}
int main() {
  cin >> N;

  // 3.?????????
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 1001; j++) {
      dp[i][j] = -1;
    }
  }
  // 4.??\\???
  for (int i = 1; i < N; i++) {
    cin >> D[i];
    if (D[i] == 'J') {
      dp[4][i] = 0;
      dp[5][i] = 0;
      dp[7][i] = 0;
    }
    if (D[i] == 'O') {
      dp[3][i] = 0;
      dp[5][i] = 0;
      dp[6][i] = 0;
    }
    if (D[i] == 'I') {
      dp[2][i] = 0;
      dp[6][i] = 0;
      dp[7][i] = 0;
    }
  }

  // 5.???????????¨???
  if (D[1] != 'J') {
    dp[4][1] = 0;
    dp[5][1] = 0;
    dp[7][1] = 0;
  }

  // 6.??????
  int total = 0;
  for (int i = 1; i < 9; i++) {
    total += calc(i, N);
    total = total % 10007;
  }
  cout << total << endl;

  return 0;
}