Problem p01842 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using PLL = pair<ll, ll>;
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;

ll memo[2][50][50][50][50][2];
bool calced[2][50][50][50][50][2];
V<ll> decka, deckb;

ll pass_turn(ll player, ll rest_a, ll rest_b, ll stack_a, ll stack_b) {
  assert(abs(stack_a - stack_b) <= 1);
  if (stack_a + stack_b == 0)
    return 0;
  ll aidx = decka.size() - rest_a - 1;
  ll bidx = deckb.size() - rest_b - 1;
  bool which;
  if (stack_a != stack_b)
    which = stack_a < stack_b;
  else
    which = (player == 0);
  V<pair<ll, bool>> st;
  for (ll i = 0; i < stack_a + stack_b; i++) {
    auto &idx = (!which ? aidx : bidx);
    const auto &deck = (!which ? decka : deckb);
    st.emplace_back(deck[idx], which);
    // cerr << deck[idx] << ", ";
    idx--;
    which = !which;
  }
  // cerr << endl;

  ll scorea = 0, scoreb = 0;
  for (const auto &e : st) {
    ll n;
    bool w;
    tie(n, w) = e;
    if (n == -1 && w == true)
      break;
    if (n != -1)
      scorea += (w == false ? n : 0);
  }
  for (const auto &e : st) {
    ll n;
    bool w;
    tie(n, w) = e;
    if (n == -1 && w == false)
      break;
    if (n != -1)
      scoreb += (w == true ? n : 0);
  }
  return scorea - scoreb;
}

ll calc(ll player, ll rest_a, ll rest_b, ll stack_a, ll stack_b, bool passed) {
  if (calced[player][rest_a][rest_b][stack_a][stack_b][(ll)passed])
    return memo[player][rest_a][rest_b][stack_a][stack_b][(ll)passed];
  ll ret;
  bool npassed = (stack_a + stack_b == 0);
  if (rest_a == 0 && rest_b == 0) {
    ret = pass_turn(player, rest_a, rest_b, stack_a, stack_b);
  } else if (player == 0 && rest_a == 0) {
    ret = pass_turn(player, rest_a, rest_b, stack_a, stack_b);
    if (!(passed && (stack_a + stack_b == 0)))
      ret += calc(1, rest_a, rest_b, 0, 0, npassed);
  } else if (player == 1 && rest_b == 0) {
    ret = pass_turn(!player, rest_a, rest_b, stack_a, stack_b);
    if (!(passed && (stack_a + stack_b == 0)))
      ret += calc(0, rest_a, rest_b, 0, 0, npassed);
  } else {
    ll r1 = pass_turn(player, rest_a, rest_b, stack_a, stack_b);
    if (!(passed && (stack_a + stack_b == 0)))
      r1 += calc(!player, rest_a, rest_b, 0, 0, npassed);
    ll r2;
    if ((player == 0 && rest_a > 0) || (player == 1 && rest_b > 0)) {
      ll nrest_a = rest_a - (player == 0);
      ll nrest_b = rest_b - (player == 1);
      ll nstack_a = stack_a + (player == 0);
      ll nstack_b = stack_b + (player == 1);
      r2 = calc(!player, nrest_a, nrest_b, nstack_a, nstack_b, false);
    } else {
      r2 = r1;
    }
    if (player == 0)
      ret = max(r1, r2);
    else
      ret = min(r1, r2);
    // cerr << r1 << ", " << r2 << endl;
  }
  calced[player][rest_a][rest_b][stack_a][stack_b][(ll)passed] = true;
  // cerr << player << ", " << rest_a << ", " << rest_b << ", " << stack_a << ",
  // " << stack_b << ", " << ret << endl;
  return memo[player][rest_a][rest_b][stack_a][stack_b][(ll)passed] = ret;
}

int main() {
  ll N, M;
  cin >> N >> M;
  decka.resize(N);
  deckb.resize(M);
  for (ll &e : decka)
    cin >> e;
  for (ll &e : deckb)
    cin >> e;
  calc(0, N, M, 0, 0, false);
  ll ans = memo[0][N][M][0][0][0];
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using PLL = pair<ll, ll>;
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;

ll memo[2][55][55][55][55][2];
bool calced[2][55][55][55][55][2];
V<ll> decka, deckb;

ll pass_turn(ll player, ll rest_a, ll rest_b, ll stack_a, ll stack_b) {
  assert(abs(stack_a - stack_b) <= 1);
  if (stack_a + stack_b == 0)
    return 0;
  ll aidx = decka.size() - rest_a - 1;
  ll bidx = deckb.size() - rest_b - 1;
  bool which;
  if (stack_a != stack_b)
    which = stack_a < stack_b;
  else
    which = (player == 0);
  V<pair<ll, bool>> st;
  for (ll i = 0; i < stack_a + stack_b; i++) {
    auto &idx = (!which ? aidx : bidx);
    const auto &deck = (!which ? decka : deckb);
    st.emplace_back(deck[idx], which);
    // cerr << deck[idx] << ", ";
    idx--;
    which = !which;
  }
  // cerr << endl;

  ll scorea = 0, scoreb = 0;
  for (const auto &e : st) {
    ll n;
    bool w;
    tie(n, w) = e;
    if (n == -1 && w == true)
      break;
    if (n != -1)
      scorea += (w == false ? n : 0);
  }
  for (const auto &e : st) {
    ll n;
    bool w;
    tie(n, w) = e;
    if (n == -1 && w == false)
      break;
    if (n != -1)
      scoreb += (w == true ? n : 0);
  }
  return scorea - scoreb;
}

ll calc(ll player, ll rest_a, ll rest_b, ll stack_a, ll stack_b, bool passed) {
  if (calced[player][rest_a][rest_b][stack_a][stack_b][(ll)passed])
    return memo[player][rest_a][rest_b][stack_a][stack_b][(ll)passed];
  ll ret;
  bool npassed = (stack_a + stack_b == 0);
  if (rest_a == 0 && rest_b == 0) {
    ret = pass_turn(player, rest_a, rest_b, stack_a, stack_b);
  } else if (player == 0 && rest_a == 0) {
    ret = pass_turn(player, rest_a, rest_b, stack_a, stack_b);
    if (!(passed && (stack_a + stack_b == 0)))
      ret += calc(1, rest_a, rest_b, 0, 0, npassed);
  } else if (player == 1 && rest_b == 0) {
    ret = pass_turn(!player, rest_a, rest_b, stack_a, stack_b);
    if (!(passed && (stack_a + stack_b == 0)))
      ret += calc(0, rest_a, rest_b, 0, 0, npassed);
  } else {
    ll r1 = pass_turn(player, rest_a, rest_b, stack_a, stack_b);
    if (!(passed && (stack_a + stack_b == 0)))
      r1 += calc(!player, rest_a, rest_b, 0, 0, npassed);
    ll r2;
    if ((player == 0 && rest_a > 0) || (player == 1 && rest_b > 0)) {
      ll nrest_a = rest_a - (player == 0);
      ll nrest_b = rest_b - (player == 1);
      ll nstack_a = stack_a + (player == 0);
      ll nstack_b = stack_b + (player == 1);
      r2 = calc(!player, nrest_a, nrest_b, nstack_a, nstack_b, false);
    } else {
      r2 = r1;
    }
    if (player == 0)
      ret = max(r1, r2);
    else
      ret = min(r1, r2);
    // cerr << r1 << ", " << r2 << endl;
  }
  calced[player][rest_a][rest_b][stack_a][stack_b][(ll)passed] = true;
  // cerr << player << ", " << rest_a << ", " << rest_b << ", " << stack_a << ",
  // " << stack_b << ", " << ret << endl;
  return memo[player][rest_a][rest_b][stack_a][stack_b][(ll)passed] = ret;
}

int main() {
  ll N, M;
  cin >> N >> M;
  decka.resize(N);
  deckb.resize(M);
  for (ll &e : decka)
    cin >> e;
  for (ll &e : deckb)
    cin >> e;
  calc(0, N, M, 0, 0, false);
  ll ans = memo[0][N][M][0][0][0];
  cout << ans << endl;
  return 0;
}