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

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

int n[2];
int card[2][55];
int dp[51][51][51][51][4][2];

int dfs(ll ai, ll aj, ll bi, ll bj, ll pass, ll turn) {
  if (pass == 2)
    return 0;
  if (dp[ai][aj][bi][bj][pass][turn] != -INF)
    return dp[ai][aj][bi][bj][pass][turn];
  int res = -INF;
  if (aj < n[turn]) {
    if (card[turn][aj] == -1) {
      maxch(res, -dfs(bj, bj, ai, aj + 1, 0, 1 - turn));
    } else {
      maxch(res, -dfs(bi, bj, ai, aj + 1, 0, 1 - turn));
    }
  }
  if (ai == aj && bi == bj) {
    maxch(res, -dfs(bi, bj, ai, aj, pass + 1, 1 - turn));
  } else {
    ll s1 = 0, s2 = 0;
    repl(i, ai, aj) {
      if (card[turn][i] != -1)
        s1 += card[turn][i];
    }
    repl(i, bi, bj) {
      if (card[1 - turn][i] != -1)
        s2 += card[1 - turn][i];
    }
    maxch(res, -dfs(bj, bj, aj, aj, 0, 1 - turn) + (s1 - s2));
  }
  return dp[ai][aj][bi][bj][pass][turn] = res;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> n[0] >> n[1];
  rep(k, 2) rep(i, n[k]) cin >> card[k][i];
  rep(i, 51) rep(j, 51) rep(k, 51) rep(l, 51) rep(x, 4) rep(y, 2)
      dp[i][j][k][l][x][y] = -INF;
  cout << dfs(0, 0, 0, 0, 0, 0) << endl;
  return 0;
}

Error: -11

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

int n[2];
int card[2][55];
int dp[50][50][50][50][4][2];

ll dfs(ll ai, ll aj, ll bi, ll bj, ll pass, ll turn) {
  if (pass == 2)
    return 0;
  if (dp[ai][aj][bi][bj][pass][turn] != -INF)
    return dp[ai][aj][bi][bj][pass][turn];
  ll res = -INF;
  if (aj < n[turn]) {
    if (card[turn][aj] == -1) {
      maxch(res, -dfs(bj, bj, ai, aj + 1, 0, 1 - turn));
    } else {
      maxch(res, -dfs(bi, bj, ai, aj + 1, 0, 1 - turn));
    }
  }
  if (ai == aj && bi == bj) {
    maxch(res, -dfs(bi, bj, ai, aj, pass + 1, 1 - turn));
  } else {
    ll s1 = 0, s2 = 0;
    repl(i, ai, aj) {
      if (card[turn][i] != -1)
        s1 += card[turn][i];
    }
    repl(i, bi, bj) {
      if (card[1 - turn][i] != -1)
        s2 += card[1 - turn][i];
    }
    maxch(res, -dfs(bj, bj, aj, aj, 0, 1 - turn) + (s1 - s2));
  }
  return dp[ai][aj][bi][bj][pass][turn] = res;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> n[0] >> n[1];
  rep(k, 2) rep(i, n[k]) cin >> card[k][i];
  rep(i, 50) rep(j, 50) rep(k, 50) rep(l, 50) rep(x, 4) rep(y, 2)
      dp[i][j][k][l][x][y] = -INF;
  cout << dfs(0, 0, 0, 0, 0, 0) << endl;
  return 0;
}