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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
//????????????????????????????????????
// #define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const int inf2 = 1145141919;
const ld pi =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { //??£??\\???????????¨???
  ll to, cost;
};
struct S { //???????????±?????\\???????????°
  int from, to, cost;
};
bool operator<(S a, S b) { return a.cost < b.cost; }
bool operator>(S a, S b) { return a.cost > b.cost; }
struct H {
  int x, y;
};
ll gcm(ll i, ll j) { //?????§??¬?´???°
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
  return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad?????§?¨???????2??????????????¢
  //---------------------------------------------------
  //+++++++++++++++++++++++++++++++++++++++++++++++++++
struct A {
  int cost, time;
  //????????????i????????????????????´??°????????????
};
int n, a[10000], d = 0, e = 1;
A dp[2][5002][2];
signed main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++)
    cin >> a[i];
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 5001; j++)
      for (int z = 0; z < 2; z++)
        dp[i][j][z] = A{inf2, -1};
  dp[1][0][0] = A{0, 0}, dp[0][1][1] = A{0, 0};
  for (int i = 0; i <= n / 2; i++) {
    for (int j = 0; j <= n / 2; j++) {
      if (i + j == 0)
        continue;
      if (dp[e][j][0].time != i + 1)
        dp[e][j][0] = A{inf2, i + 1};
      if (dp[e][j][1].time != i + 1)
        dp[e][j][1] = A{inf2, i + 1};
      if (dp[d][j + 1][0].time != i)
        dp[d][j + 1][0] = A{inf2, i};
      if (dp[d][j + 1][1].time != i)
        dp[d][j + 1][1] = A{inf2, i};
      if (dp[d][j][0].time == i) {
        dp[e][j][0].cost = dp[d][j][0].cost;
        dp[d][j + 1][1].cost =
            min(dp[d][j + 1][1].cost, dp[d][j][0].cost + a[i + j - 1]);
      }
      if (dp[d][j][1].time == i) {
        dp[e][j][0].cost =
            min(dp[e][j][0].cost, dp[d][j][1].cost + a[i + j - 1]);
        dp[d][j + 1][1].cost = min(dp[d][j + 1][1].cost, dp[d][j][1].cost);
      }
    }
    swap(d, e);
  }
  int ans = inf2;
  if (dp[d][n / 2][0].time == n / 2 + 1)
    ans = dp[d][n / 2][0].cost;
  if (dp[d][n / 2][1].time == n / 2 + 1)
    ans = min(ans, dp[d][n / 2][1].cost);
  cout << ans << endl;
  getchar();
  getchar();
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
//????????????????????????????????????
// #define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const int inf2 = 1145141919;
const ld pi =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { //??£??\\???????????¨???
  ll to, cost;
};
struct S { //???????????±?????\\???????????°
  int from, to, cost;
};
bool operator<(S a, S b) { return a.cost < b.cost; }
bool operator>(S a, S b) { return a.cost > b.cost; }
struct H {
  int x, y;
};
ll gcm(ll i, ll j) { //?????§??¬?´???°
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
  return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad?????§?¨???????2??????????????¢
//---------------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++
int n, a[10000], dp[5001][5001][2];
signed main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++)
    cin >> a[i];
  for (int i = 0; i <= 5000; i++)
    for (int j = 0; j <= 5000; j++)
      for (int z = 0; z < 2; z++)
        dp[i][j][z] = inf2;
  dp[1][0][0] = 0;
  dp[0][1][1] = 0;
  for (int i = 0; i <= n / 2; i++) {
    for (int j = 0; j <= n / 2; j++) {
      if (i + j == 0)
        continue;
      dp[i + 1][j][0] = min(dp[i + 1][j][0], dp[i][j][0]);
      dp[i + 1][j][1] = min(dp[i + 1][j][1], dp[i][j][0] + a[i + j - 1]);
      dp[i][j + 1][0] = min(dp[i][j + 1][0], dp[i][j][1] + a[i + j - 1]);
      dp[i][j + 1][1] = min(dp[i][j + 1][1], dp[i][j][1]);
    }
  }
  cout << max(dp[n / 2][n / 2][0], dp[n / 2][n / 2][1]) << endl;
  getchar();
  getchar();
}