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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int n;
  static int t[10100];
  scanf("%d", &n);
  rep(i, n - 1) scanf("%d", t + i);
  int m = n / 2;

  static int dp1[2][10100], dp2[2][10100];
  const int INF = 1e9;
  rep(i, 2) rep(j, n + 1) dp1[i][j] = dp2[i][j] = INF;

  dp1[1][0] = 0;
  fill(dp2[1] + 1, dp2[1] + n, t[0]);
  for (int i = 2; i <= m; ++i) {
    int *prev1 = dp1[(i + 1) & 1];
    int *cur1 = dp1[i & 1];
    int *prev2 = dp2[(i + 1) & 1];
    int *cur2 = dp2[i & 1];

    cur1[0] = INF;
    for (int j = 1; j <= n; ++j) {
      cur1[j] = min(prev1[j - 1], prev2[j - 1] + t[j - 1]);
      cur2[j] = min(cur2[j - 1], cur1[j - 1] + t[j - 1]);
    }
  }

  printf("%d\\n", dp2[m & 1][n]);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int n;
  static int t[10100];
  scanf("%d", &n);
  rep(i, n - 1) scanf("%d", t + i);
  int m = n / 2;

  static int dp1[5100][10100], dp2[5100][10100];
  const int INF = 1e9;
  rep(i, m + 1) rep(j, n + 1) dp1[i][j] = dp2[i][j] = INF;

  dp1[1][0] = 0;
  fill(dp2[1] + 1, dp2[1] + n, t[0]);
  for (int i = 2; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      dp1[i][j] = min(dp1[i - 1][j - 1], dp2[i - 1][j - 1] + t[j - 1]);
      dp2[i][j] = min(dp2[i][j - 1], dp1[i][j - 1] + t[j - 1]);
    }
  }

  printf("%d\\n", dp2[m][n]);
}