Problem p01842 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

constexpr int S = 51;
int N, M, a[60], b[60];
int suma[60], sumb[60];
// memo[s][t][u][v][turn][pass] :=
//      a ??????????????????????????? [s, t) ????????????
//      b ??????????????????????????? [u, v) ????????????
//      turn ??¨ pass ????????±
//      ???????????? point_a - point_b ??????
int memo[S][S][S][S][2][3];

// pass ?????£?¶?????????????????????°?????????
int dfs(int s, int t, int u, int v, int turn, int pass) {
  int &val = memo[s][t][u][v][turn][pass];
  if (val != -INF)
    return val;

  // ???????????????????????¶?????§??????????????????
  val = suma[t] - suma[s];
  val -= sumb[v] - sumb[u];

  // ???????????¶??§???????????° pass ??????
  // ?????????????????¨????????£??????????????????????????????
  // ??\\??????????????§????????????????????????????????£?????????
  if (!(s == t && u == v && pass == 1)) {
    if (s == t && u == v)
      val += dfs(t, t, v, v, turn ^ 1, pass + 1);
    else
      val += dfs(t, t, v, v, turn ^ 1, 0);
  }

  // take (first)
  if (turn == 0) {
    if (t < N) {
      // ??¨??? ->
      // ?????¨?????????????????????????????????????????¨????????????????????????
      //      -> ???????????????????????????????????????????????¨??????
      if (a[t] == -1)
        chmax(val, dfs(s, t + 1, v, v, turn ^ 1, 0));
      else
        chmax(val, dfs(s, t + 1, u, v, turn ^ 1, 0));
    }
  }
  // take (second)
  else {
    if (v < M) {
      if (b[v] == -1)
        chmin(val, dfs(t, t, u, v + 1, turn ^ 1, 0));
      else
        chmin(val, dfs(s, t, u, v + 1, turn ^ 1, 0));
    }
  }
  return val;
}

signed main() {
  rep(i, 0, S) rep(j, 0, S) rep(k, 0, S) rep(l, 0, S) rep(t, 0, 2) rep(p, 0, 3)
      memo[i][j][k][l][t][p] = -INF;

  cin >> N >> M;
  rep(i, 0, N) cin >> a[i];
  rep(i, 0, M) cin >> b[i];

  // 1-indexed
  rep(i, 0, N) suma[i + 1] = (a[i] == -1 ? suma[i] : suma[i] + a[i]);
  rep(i, 0, M) sumb[i + 1] = (b[i] == -1 ? sumb[i] : sumb[i] + b[i]);
  cout << dfs(0, 0, 0, 0, 0, 0) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, M, a[60], b[60];
int suma[60], sumb[60];
// memo[s][t][u][v][turn][pass] :=
//      a ??????????????????????????? [s, t) ????????????
//      b ??????????????????????????? [u, v) ????????????
//      turn ??¨ pass ????????±
//      ???????????? point_a - point_b ??????
int memo[60][60][60][60][2][3];

// pass ?????£?¶?????????????????????°?????????
int dfs(int s, int t, int u, int v, int turn, int pass) {
  int &val = memo[s][t][u][v][turn][pass];
  if (val != -INF)
    return val;

  // ???????????????????????¶?????§??????????????????
  val = suma[t] - suma[s];
  val -= sumb[v] - sumb[u];

  // ???????????¶??§???????????° pass ??????
  // ?????????????????¨????????£??????????????????????????????
  // ??\\??????????????§????????????????????????????????£?????????
  if (!(s == t && u == v && pass == 1)) {
    if (s == t && u == v)
      val += dfs(t, t, v, v, turn ^ 1, pass + 1);
    else
      val += dfs(t, t, v, v, turn ^ 1, 0);
  }

  // take (first)
  if (turn == 0) {
    if (t < N) {
      // ??¨??? ->
      // ?????¨?????????????????????????????????????????¨????????????????????????
      //      -> ???????????????????????????????????????????????¨??????
      if (a[t] == -1)
        chmax(val, dfs(s, t + 1, v, v, turn ^ 1, 0));
      else
        chmax(val, dfs(s, t + 1, u, v, turn ^ 1, 0));
    }
  }
  // take (second)
  else {
    if (v < M) {
      if (b[v] == -1)
        chmin(val, dfs(t, t, u, v + 1, turn ^ 1, 0));
      else
        chmin(val, dfs(s, t, u, v + 1, turn ^ 1, 0));
    }
  }
  return val;
}

signed main() {
  rep(i, 0, 60) rep(j, 0, 60) rep(k, 0, 60) rep(l, 0, 60) rep(t, 0, 2)
      rep(p, 0, 3) memo[i][j][k][l][t][p] = -INF;

  cin >> N >> M;
  rep(i, 0, N) cin >> a[i];
  rep(i, 0, M) cin >> b[i];

  // 1-indexed
  rep(i, 0, N) suma[i + 1] = (a[i] == -1 ? suma[i] : suma[i] + a[i]);
  rep(i, 0, M) sumb[i + 1] = (b[i] == -1 ? sumb[i] : sumb[i] + b[i]);
  cout << dfs(0, 0, 0, 0, 0, 0) << endl;
  return 0;
}