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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 1001001001;
int N;
int b[10002];
int dp
    [2][5002]
    [2]; //[A][B][C]=A??????????????????C???????????¨???1??????????????§???Bmm?????£?????????????????????????????????????°????
class DividingSnacks {
public:
  int solve() {
    scanf("%d", &N); // N%2=0
    rep(i, N - 1) { scanf("%d", &b[i]); }
    b[N - 1] = 0; //?????????????????????????????????????????¨??????
    fill(dp[0][0], dp[2][0], INF);
    rep(i, N) {
      rep(j, N / 2 + 1) {
        // rep(k,2){
        if (i == 0) {
          //?????????1mm???A?????¨???
          if (j == 1)
            dp[i % 2][j][0] = 0;
          else
            dp[i % 2][j][0] = INF;
          dp[i % 2][j][1] = INF;
        } else { // i=1,2,3???
          // i+1mm??????????????????????????????????????????????????????????????????i-1>0???
          //??????????????????????????§????????¨???
          // A?????????
          // j=0???????????????????????§INF?????????
          if (j > 0)
            dp[i % 2][j][0] = dp[(i - 1) % 2][j - 1][1] + b[i - 1];
          // B?????¨???
          dp[i % 2][j][1] = dp[(i - 1) % 2][j][0] + b[i - 1];

          //??????????????¨???
          if (j > 0)
            dp[i % 2][j][0] = min(dp[i % 2][j][0], dp[(i - 1) % 2][j - 1][0]);
          dp[i % 2][j][1] = min(dp[i % 2][j][1], dp[(i - 1) % 2][j][1]);
        }
        //}
      }
    }

    int ans = min(dp[(N - 1) % 2][(N / 2)][0], dp[(N - 1) % 2][(N / 2)][1]);
    return (int)ans;
  }
};

signed main(void) {
  DividingSnacks ans;
  printf("%d\\n", ans.solve());
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 1001001001001001001;
int N;
int b[10002];
int dp
    [10002][5002]
    [2]; //[A][B][C]=A??????????????????C???????????¨???1??????????????§???Bmm?????£?????????????????????????????????????°????
class DividingSnacks {
public:
  int solve() {
    scanf("%d", &N); // N%2=0
    rep(i, N - 1) { scanf("%d", &b[i]); }
    b[N - 1] = 0; //?????????????????????????????????????????¨??????
    fill(dp[0][0], dp[N + 1][0], INF);
    rep(i, N) {
      rep(j, N / 2 + 1) {
        // rep(k,2){
        if (i == 0) {
          //?????????1mm???A?????¨???
          if (j == 1)
            dp[i][j][0] = 0;
          else
            dp[i][j][0] = INF;
          dp[i][j][1] = INF;
        } else { // i=1,2,3???
          // i+1mm??????????????????????????????????????????????????????????????????i-1>0???
          //??????????????????????????§????????¨???
          // A?????????
          // j=0???????????????????????§INF?????????
          if (j > 0)
            dp[i][j][0] = dp[i - 1][j - 1][1] + b[i - 1];
          // B?????¨???
          dp[i][j][1] = dp[i - 1][j][0] + b[i - 1];

          //??????????????¨???
          if (j > 0)
            dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j - 1][0]);
          dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][1]);
        }
        //}
      }
    }

    int ans = min(dp[N - 1][(N / 2)][0], dp[N - 1][(N / 2)][1]);
    return (int)ans;
  }
};

signed main(void) {
  DividingSnacks ans;
  printf("%d\\n", ans.solve());
}