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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int N, M;
vector<int> A, B;

map<pair<int, int>, pair<int, int>> DP[2][51][51][2];
map<pair<int, int>, bool> used[2][51][51][2];

pair<int, int> solve(int t, int a, int b, int ast, int bst, int as, int bs,
                     int pass) {
  if (used[t][a][b][pass][make_pair(ast, bst)]) {
    pair<int, int> res;
    res.first = DP[t][a][b][pass][make_pair(ast, bst)].first + as;
    res.second = DP[t][a][b][pass][make_pair(ast, bst)].second + bs;
    return res;
  }

  if (t == 0) {
    if (a == N) {
      int s = 0;
      for (int i = b; i < M; i++) {
        if (B[i] != -1)
          s += B[i];
        else
          bst = 0;
      }
      return make_pair(as + ast, bs + bst + s);
    }

    pair<int, int> p1, p2;
    if (A[a] != -1) {
      p1 = solve(t ^ 1, a + 1, b, ast + A[a], bst, as, bs, 0);
    } else {
      p1 = solve(t ^ 1, a + 1, b, ast, 0, as, bs, 0);
    }

    if (pass == 1) {
      p2 = make_pair(as, bs);
    } else if (ast == 0 && bst == 0) {
      p2 = solve(t ^ 1, a, b, 0, 0, as + ast, bs + bst, 1);
    } else {
      p2 = solve(t ^ 1, a, b, 0, 0, as + ast, bs + bst, 0);
    }

    if (p1.first - p1.second >= p2.first - p2.second) {
      used[t][a][b][pass][make_pair(ast, bst)] = true;
      DP[t][a][b][pass][make_pair(ast, bst)].first = p1.first - as;
      DP[t][a][b][pass][make_pair(ast, bst)].second = p1.second - bs;

      return p1;
    } else {
      used[t][a][b][pass][make_pair(ast, bst)] = true;
      DP[t][a][b][pass][make_pair(ast, bst)].first = p2.first - as;
      DP[t][a][b][pass][make_pair(ast, bst)].second = p2.second - bs;

      return p2;
    }
  } else {
    if (b == M) {
      int s = 0;
      for (int i = a; i < N; i++) {
        if (A[i] != -1)
          s += A[i];
        else
          ast = 0;
      }
      return make_pair(as + ast + s, bs + bst);
    }

    pair<int, int> p1, p2;
    if (B[b] != -1) {
      p1 = solve(t ^ 1, a, b + 1, ast, bst + B[b], as, bs, 0);
    } else {
      p1 = solve(t ^ 1, a, b + 1, 0, bst, as, bs, 0);
    }

    if (pass == 1) {
      p2 = make_pair(as, bs);
    } else if (ast == 0 && bst == 0) {
      p2 = solve(t ^ 1, a, b, 0, 0, as + ast, bs + bst, 1);
    } else {
      p2 = solve(t ^ 1, a, b, 0, 0, as + ast, bs + bst, 0);
    }

    if (p1.second - p1.first >= p2.second - p2.first) {
      used[t][a][b][pass][make_pair(ast, bst)] = true;
      DP[t][a][b][pass][make_pair(ast, bst)].first = p1.first - as;
      DP[t][a][b][pass][make_pair(ast, bst)].second = p1.second - bs;

      return p1;
    } else {
      used[t][a][b][pass][make_pair(ast, bst)] = true;
      DP[t][a][b][pass][make_pair(ast, bst)].first = p2.first - as;
      DP[t][a][b][pass][make_pair(ast, bst)].second = p2.second - bs;

      return p2;
    }
  }
}

int main() {
  cin >> N >> M;
  A.resize(N), B.resize(M);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }

  pair<int, int> ans = solve(0, 0, 0, 0, 0, 0, 0, 0);

  cout << ans.first - ans.second << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int N, M;
vector<int> A, B;

pair<int, int> DP[2][51][51];
bool used[2][51][51];

pair<int, int> solve(int t, int a, int b, int ast, int bst, int as, int bs,
                     int pass) {
  if (t == 0) {
    if (a == N) {
      int s = 0;
      for (int i = b; i < M; i++) {
        if (B[i] != -1)
          s += B[i];
        else
          bst = 0;
      }
      return make_pair(as + ast, bs + bst + s);
    }

    pair<int, int> p1, p2;
    if (A[a] != -1) {
      p1 = solve(t ^ 1, a + 1, b, ast + A[a], bst, as, bs, 0);
    } else {
      p1 = solve(t ^ 1, a + 1, b, ast, 0, as, bs, 0);
    }

    if (pass == 1) {
      p2 = make_pair(as, bs);
    } else if (ast == 0 && bst == 0) {
      p2 = solve(t ^ 1, a, b, 0, 0, as + ast, bs + bst, 1);
    } else {
      if (used[t][a][b] == false) {
        p2 = solve(t ^ 1, a, b, 0, 0, as + ast, bs + bst, 0);
        used[t][a][b] = true;
        DP[t][a][b].first = p2.first - as - ast;
        DP[t][a][b].second = p2.second - bs - bst;
      } else {
        p2.first = DP[t][a][b].first + as + ast;
        p2.second = DP[t][a][b].second + bs + bst;
      }
    }

    if (p1.first - p1.second >= p2.first - p2.second) {
      return p1;
    } else {
      return p2;
    }
  } else {
    if (b == M) {
      int s = 0;
      for (int i = a; i < N; i++) {
        if (A[i] != -1)
          s += A[i];
        else
          ast = 0;
      }
      return make_pair(as + ast + s, bs + bst);
    }

    pair<int, int> p1, p2;
    if (B[b] != -1) {
      p1 = solve(t ^ 1, a, b + 1, ast, bst + B[b], as, bs, 0);
    } else {
      p1 = solve(t ^ 1, a, b + 1, 0, bst, as, bs, 0);
    }

    if (pass == 1) {
      p2 = make_pair(as, bs);
    } else if (ast == 0 && bst == 0) {
      p2 = solve(t ^ 1, a, b, 0, 0, as + ast, bs + bst, 1);
    } else {
      if (used[t][a][b] == false) {
        p2 = solve(t ^ 1, a, b, 0, 0, as + ast, bs + bst, 0);
        used[t][a][b] = true;
        DP[t][a][b].first = p2.first - as - ast;
        DP[t][a][b].second = p2.second - bs - bst;
      } else {
        p2.first = DP[t][a][b].first + as + ast;
        p2.second = DP[t][a][b].second + bs + bst;
      }
    }

    if (p1.second - p1.first >= p2.second - p2.first) {
      return p1;
    } else {
      return p2;
    }
  }
}

int main() {
  cin >> N >> M;
  A.resize(N), B.resize(M);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }

  pair<int, int> ans = solve(0, 0, 0, 0, 0, 0, 0, 0);

  cout << ans.first - ans.second << endl;

  return 0;
}