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

#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[50], b[50];
int A[51], B[51];
int dp[51][51][51][51][2][2];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == -1)
      a[i] = 0;
    A[i + 1] = A[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (b[i] == -1)
      b[i] = 0;
    B[i + 1] = B[i] + b[i];
  }
  memset(dp, -1, sizeof(dp));
  for (int x = n; x >= 0; x--)
    for (int y = m; y >= 0; y--)
      for (int s = x; s >= 0; s--)
        for (int t = y; t >= 0; t--)
          for (int f = 1; f >= 0; f--)
            for (int p = 1; p >= 0; p--) {
              int &res = dp[x][y][s][t][f][p];
              if (p == 0) {
                res = -1e9;
                if (f == 1 && x == s && y == t)
                  res = max(res, 0);
                else
                  res = max(res, dp[x][y][x][y][x == s && y == t][1] + A[x] -
                                     A[s] - B[y] + B[t]);
                if (x < n && a[x] == 0)
                  res = max(res, dp[x + 1][y][s][y][0][1]);
                if (x < n && a[x] > 0)
                  res = max(res, dp[x + 1][y][s][t][0][1]);
              } else {
                res = 1e9;
                if (f == 1 && x == s && y == t)
                  res = min(res, 0);
                else
                  res = min(res, dp[x][y][x][y][x == s && y == t][0] + A[x] -
                                     A[s] - B[y] + B[t]);
                if (y < m && b[y] == 0)
                  res = min(res, dp[x][y + 1][x][t][0][0]);
                if (y < m && b[y] > 0)
                  res = min(res, dp[x][y + 1][s][t][0][0]);
              }
            }
  cout << dp[0][0][0][0][0][0] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int n, m;
int a[50], b[50];
int A[51], B[51];

int dp[51][51][51][51][2][2];

int solve(int x, int y, int s, int t, int f, int p) {
  if (dp[x][y][s][t][f][p] != -1)
    return dp[x][y][s][t][f][p];

  dp[x][y][s][t][f][p] = 0;
  int res = 0;
  if (p == 0) {
    res = -1e9;

    if (f == 1 && x == s && y == t) {
      res = max(res, 0);
    } else {
      int nf = (x == s && y == t);
      res = max(res, solve(x, y, x, y, nf, 1) + A[x] - A[s] - B[y] + B[t]);
    }

    if (x < n && a[x] == 0) {
      res = max(res, solve(x + 1, y, s, y, 0, 1));
    }
    if (x < n && a[x] > 0) {
      res = max(res, solve(x + 1, y, s, t, 0, 1));
    }

  } else {
    res = 1e9;

    if (f == 1 && x == s && y == t) {
      res = min(res, 0);
    } else {
      int nf = (x == s && y == t);
      res = min(res, solve(x, y, x, y, nf, 0) + A[x] - A[s] - B[y] + B[t]);
    }

    if (y < m && b[y] == 0) {
      res = min(res, solve(x, y + 1, x, t, 0, 0));
    }
    if (y < m && b[y] > 0) {
      res = min(res, solve(x, y + 1, s, t, 0, 0));
    }
  }

  return dp[x][y][s][t][f][p] = res;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == -1)
      a[i] = 0;
    A[i + 1] = A[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (b[i] == -1)
      b[i] = 0;
    B[i + 1] = B[i] + b[i];
  }
  memset(dp, -1, sizeof(dp));

  cout << solve(0, 0, 0, 0, 0, 0) << endl;
  return 0;
}