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

#include <iostream>
#include <map>
using namespace std;
const int INF = (1e9);

int main() {
  int N;
  const string g = "000";
  while (cin >> N, N) {
    string d, t, e, id, D;
    map<string, int> T, I, O;
    for (int i = 0; i < N; ++i) {
      cin >> d >> t >> e >> id;
      if (D != d) {
        D = d;
        for (auto itr = I.begin(); itr != I.end(); ++itr)
          itr->second = INF;
        for (auto itr = O.begin(); itr != O.end(); ++itr)
          itr->second = -1;
      }
      int m = stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3, 2));
      if (e == "I") {
        I[id] = m;
        O[id] = INF;
      } else {
        O[id] = m;
        if (id != g && O[g] > m) {
          T[id] += max(0, m - max(I[g], I[id]));
        } else if (id == g) {
          for (auto itr = O.begin(); itr != O.end(); ++itr) {
            id = itr->first;
            int o = itr->second;
            if (o > m) {
              T[id] += max(0, m - max(I[id], I[g]));
            }
          }
        }
      }
    }
    int ans = 0;
    for (auto itr = T.begin(); itr != T.end(); ++itr) {
      if (itr->first == g)
        continue;
      // cout << itr->first << " " << itr->second << endl;
      ans = max(ans, itr->second);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <cassert>
#include <cstdio>
#include <iostream>
using namespace std;
int dp[51][51][51][51][2][3], A[51], B[51], C[51], D[51];
int N, M, INF = (1e9);
bool done[51][51][51][51][2][3];

int dfs(int a, int b, int c, int d, int p, int t) {
  if (done[a][b][c][d][p][t])
    return dp[a][b][c][d][p][t];
  done[a][b][c][d][p][t] = true;

  if (a == N && b == M && c == N && d == M)
    return dp[a][b][c][d][p][t];

  if (t > 1)
    dp[a][b][c][d][p][t] = 0;
  else if (p)
    dp[a][b][c][d][p][t] = INF;
  else
    dp[a][b][c][d][p][t] = -INF;

  if (p) {
    if (b < M) {
      if (B[b] >= 0)
        dp[a][b][c][d][p][t] =
            min(dp[a][b][c][d][p][t], dfs(a, b + 1, c, d, 1 - p, 0));
      else
        dp[a][b][c][d][p][t] =
            min(dp[a][b][c][d][p][t], dfs(a, b + 1, a, d, 1 - p, 0));
    }
    if (t < 2)
      dp[a][b][c][d][p][t] =
          min(dp[a][b][c][d][p][t], C[a] - C[c] - D[b] + D[d] +
                                        dfs(a, b, a, b, 1 - p, t + 1)); // pass
  } else {
    if (a < N) {
      if (A[a] >= 0)
        dp[a][b][c][d][p][t] =
            max(dp[a][b][c][d][p][t], dfs(a + 1, b, c, d, 1 - p, 0));
      else
        dp[a][b][c][d][p][t] =
            max(dp[a][b][c][d][p][t], dfs(a + 1, b, c, b, 1 - p, 0));
    }
    if (t < 2)
      dp[a][b][c][d][p][t] =
          max(dp[a][b][c][d][p][t], C[a] - C[c] - D[b] + D[d] +
                                        dfs(a, b, a, b, 1 - p, t + 1)); // pass
  }
  // cerr << a << " " << b << " " << c << " " << d << " " << p << " " <<
  // dp[a][b][c][d][p] << endl;
  return dp[a][b][c][d][p][t];
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  for (int i = 0; i < M; ++i)
    cin >> B[i];
  for (int i = 0; i < N; ++i)
    C[i + 1] = C[i] + max(0, A[i]);
  for (int i = 0; i < M; ++i)
    D[i + 1] = D[i] + max(0, B[i]);
  cout << dfs(0, 0, 0, 0, 0, 0) << endl;
  return 0;
}