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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
  os << "(" << p.fst << ", " << p.scd << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  rep(i, v.size()) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os;
}
template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int in() {
  int a;
  scanf("%d", &a);
  return a;
}
ll lin() {
  ll a;
  scanf("%lld", &a);
  return a;
}

constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;

int N;
int c[100010];

int dp[2][2][5002];

int main() {
  N = in();

  rep(i, N - 1) { c[i + 1] = in(); }

  rep(i, 2) rep(j, 2) rep(k, N / 2 + 1) dp[i][j][k] = inf;
  dp[0][0][0] = 0;

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= min(i, N / 2); ++j) {
      dp[(i + 1) & 1][0][j + 1] = min(dp[i & 1][0][j], dp[i & 1][1][j] + c[i]);
      dp[(i + 1) & 1][1][j] = min(dp[i & 1][1][j], dp[i & 1][0][j] + c[i]);
    }

    dp[(i + 1) & 1][0][0] = inf;
  }

  printf("%d\\n", min(dp[(N / 2) % 2][0][N / 2], dp[(N / 2) % 2][1][N / 2]));
}

Error: MLE

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define name dividing_snacks

namespace name {
static const int inf = 1e9;
static const int MAX_N = 10005;

static int N;
static int C[MAX_N];

static int dp[2][MAX_N / 2][MAX_N / 2];

int rec(int turn, int a, int b) {
  if (dp[turn][a][b] != inf)
    return dp[turn][a][b];

  if (a + b == N)
    return 0;
  if (a + b > N)
    return inf;

  int res = inf;

  if (turn == 0) {
    for (int i = 1; a + i <= N / 2; ++i) {
      res = min(res, C[a + b + i - 1] + rec(turn ^ 1, a + i, b));
    }
  } else {
    for (int i = 1; b + i <= N / 2; ++i) {
      res = min(res, C[a + b + i - 1] + rec(turn ^ 1, a, b + i));
    }
  }

  return dp[turn][a][b] = res;
}

void MAIN() {
  cin >> N;

  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j <= N / 2; j++) {
      for (int k = 0; k <= N / 2; k++) {
        dp[i][j][k] = inf;
      }
    }
  }

  for (int i = 0; i < N - 1; i++) {
    cin >> C[i];
  }

  cout << rec(0, 0, 0) << endl;
}
} // namespace name

int main() { name::MAIN(); }