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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[] = {{0, 1},  {-1, 1}, {-1, 0}, {-1, -1},
             {0, -1}, {1, -1}, {1, 0},  {1, 1}};
int dp[2][20002][2] = {};

signed main() {
  int n;
  Fill(dp, INT_MAX);
  scanf("%d", &n);
  vector<int> pat(n);

  REP(i, n - 1) { scanf("%d", &pat[i + 1]); }
  dp[0][10001][0] = 0;
  // dp[0][0][1] = 1;
  bool turn = 0;
  REP(i, n) {
    bool nt = 1 - turn;
    rep(j, 0, 20001) {
      if (dp[turn][j][0] != INT_MAX) {
        dp[nt][j + 1][0] = min(dp[nt][j + 1][0], dp[turn][j][0]);
        dp[nt][j - 1][1] = min(dp[nt][j - 1][1], dp[turn][j][0] + pat[i]);
      }
      if (dp[turn][j][1] != INT_MAX) {
        dp[nt][j - 1][1] = min(dp[nt][j - 1][1], dp[turn][j][1]);
        dp[nt][j + 1][0] = min(dp[nt][j + 1][0], dp[turn][j][1] + pat[i]);
      }
    }
    Fill(dp[turn], INT_MAX);
    turn = nt;
  }

  printf("%d\\n", min(dp[turn][10001][0], dp[turn][10001][1]));
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[] = {{0, 1},  {-1, 1}, {-1, 0}, {-1, -1},
             {0, -1}, {1, -1}, {1, 0},  {1, 1}};

int dp[10002][5002][2];
int n;
vector<int> pat;

int func(int pos, int ad, int turn) {
  if (ad > n / 2)
    return -1;
  if (pos == n) {
    return !ad ? 1 : -1;
  }
  if (dp[pos][ad][turn + 1])
    return dp[pos][ad][turn + 1];
  int res = INT_MAX;

  int buf;
  if (ad + turn < 0) {
    buf = func(pos + 1, -(ad + turn), -turn);
  } else {
    buf = func(pos + 1, ad + turn, turn);
  }
  if (buf != -1) {
    res = min(res, buf);
  }
  if (ad - turn < 0) {
    buf = func(pos + 1, -(ad - turn), turn);
  } else {
    buf = func(pos + 1, ad - turn, -turn);
  }
  if (buf != -1) {
    res = min(res, buf + pat[pos]);
  }
  if (res == INT_MAX)
    res = -1;
  dp[pos][ad][turn + 1] = res;
  return res;
}

signed main() {

  scanf("%d", &n);
  pat.resize(n);
  REP(i, n - 1) { scanf("%d", &pat[i + 1]); }
  printf("%d\\n", func(0, 0, 1) - 1);
  return 0;
}