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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 40
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;
int a[50], b[50];
int dp[102][51][51][2][3];

int dfs(int x, int y, int sx, int sy, int turn, int enc) {
  if (dp[x + y - sx - sy][sx][sy][turn][enc] != -INF)
    return dp[x + y - sx - sy][sx][sy][turn][enc];
  if (x == n && y == m && sx == n && sy == m)
    return 0;
  int ret;
  if (turn == 0) {
    ret = -1 * INF;
    if (x != n)
      ret = dfs(x + 1, y, sx, sy, turn ^ 1, 0);
    if (x == sx && y == sy) {
      if (enc <= 1)
        ret = max(ret, dfs(x, y, sx, sy, turn ^ 1, enc + 1));
      else
        ret = max(ret, 0);
    } else {
      bool stoa = false, stob = false;
      int xx = x - 1, yy = y - 1;
      int cntx = 0, cnty = 0;
      while (true) {
        int end = 0;
        if (yy < sy) {
          end++;
        } else {
          if (b[yy] == -1) {
            stoa = true;
          } else if (!stob) {
            cnty += b[yy];
          }
          yy--;
        }
        if (xx < sx) {
          end++;
        } else {
          if (a[xx] == -1) {
            stob = true;
          } else if (!stoa) {
            cntx += a[xx];
          }
          xx--;
        }
        if (end == 2)
          break;
      }
      ret = max(ret, cntx - cnty + dfs(x, y, x, y, turn ^ 1, 0));
    }
  } else {
    ret = INF;
    if (y != m)
      ret = dfs(x, y + 1, sx, sy, turn ^ 1, 0);
    if (x == sx && y == sy) {
      if (enc <= 1)
        ret = min(ret, dfs(x, y, sx, sy, turn ^ 1, enc + 1));
      else
        ret = min(ret, 0);
    } else {
      bool stoa = false, stob = false;
      int xx = x - 1, yy = y - 1;
      int cntx = 0, cnty = 0;
      while (true) {
        int end = 0;
        if (xx < sx) {
          end++;
        } else {
          if (a[xx] == -1) {
            stob = true;
          } else if (!stoa) {
            cntx += a[xx];
          }
          xx--;
        }
        if (yy < sy) {
          end++;
        } else {
          if (b[yy] == -1) {
            stoa = true;
          } else if (!stob) {
            cnty += b[yy];
          }
          yy--;
        }
        if (end == 2)
          break;
      }
      ret = min(ret, cntx - cnty + dfs(x, y, x, y, turn ^ 1, 0));
    }
  }
  dp[x + y - sx - sy][sx][sy][turn][enc] = ret;
  return ret;
}

int main() {
  cin >> n >> m;
  rep(i1, 102) rep(i2, 51) rep(i3, 51) rep(i4, 2) rep(i5, 3)
      dp[i1][i2][i3][i4][i5] = -INF;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  cout << dfs(0, 0, 0, 0, 0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 40
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;
int a[50], b[50];
int dp[102][51][51][2][3];

int dfs(int x, int y, int sx, int sy, int turn, int enc) {
  if (dp[x + y - sx - sy][sx][sy][turn][enc] != -1)
    return dp[x + y - sx - sy][sx][sy][turn][enc];
  if (x == n && y == m && sx == n && sy == m)
    return 0;
  int ret;
  if (turn == 0) {
    ret = -1 * INF;
    if (x != n)
      ret = dfs(x + 1, y, sx, sy, turn ^ 1, 0);
    if (x == sx && y == sy) {
      if (enc <= 1)
        ret = max(ret, dfs(x, y, sx, sy, turn ^ 1, enc + 1));
      else
        ret = max(ret, 0);
    } else {
      bool stoa = false, stob = false;
      int xx = x - 1, yy = y - 1;
      int cntx = 0, cnty = 0;
      while (true) {
        int end = 0;
        if (yy < sy) {
          end++;
        } else {
          if (b[yy] == -1) {
            stoa = true;
          } else if (!stob) {
            cnty += b[yy];
          }
          yy--;
        }
        if (xx < sx) {
          end++;
        } else {
          if (a[xx] == -1) {
            stob = true;
          } else if (!stoa) {
            cntx += a[xx];
          }
          xx--;
        }
        if (end == 2)
          break;
      }
      ret = max(ret, cntx - cnty + dfs(x, y, x, y, turn ^ 1, 1));
    }
  } else {
    ret = INF;
    if (y != m)
      ret = dfs(x, y + 1, sx, sy, turn ^ 1, 0);
    if (x == sx && y == sy) {
      if (enc <= 1)
        ret = min(ret, dfs(x, y, sx, sy, turn ^ 1, enc + 1));
      else
        ret = min(ret, 0);
    } else {
      bool stoa = false, stob = false;
      int xx = x - 1, yy = y - 1;
      int cntx = 0, cnty = 0;
      while (true) {
        int end = 0;
        if (xx < sx) {
          end++;
        } else {
          if (a[xx] == -1) {
            stob = true;
          } else if (!stoa) {
            cntx += a[xx];
          }
          xx--;
        }
        if (yy < sy) {
          end++;
        } else {
          if (b[yy] == -1) {
            stoa = true;
          } else if (!stob) {
            cnty += b[yy];
          }
          yy--;
        }
        if (end == 2)
          break;
      }
      ret = min(ret, cntx - cnty + dfs(x, y, x, y, turn ^ 1, 1));
    }
  }
  dp[x + y - sx - sy][sx][sy][turn][enc] = ret;
  return ret;
}

int main() {
  cin >> n >> m;
  memset(dp, -1, sizeof(dp));
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  cout << dfs(0, 0, 0, 0, 0, 0) << endl;
}