alexjercan/bugnet huggingface dataset:
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

#pragma warning(disable : 4996)

#define INF (1 << 25)

using namespace std;

int main() {
  int N;

  scanf("%d", &N);

  vector<int> D(N - 1);

  for (int i = 0; i < N - 1; i++) {
    scanf("%d", &D[i]);
  }

  vector<vector<vector<int>>> dp(
      2, vector<vector<int>>(N / 2 + 1, vector<int>(2, INF)));

  dp[0][0][0] = 0;

  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= N / 2; j++) {
      dp[i % 2][j][0] =
          min(dp[(i - 1) % 2][j][0], dp[(i - 1) % 2][j][1] + D[i - 1]);

      if (j != 0) {
        dp[i % 2][j][1] = min(dp[(i - 1) % 2][j - 1][1],
                              dp[(i - 1) % 2][j - 1][0] + D[i - 1]);
      }
    }
  }

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#pragma warning(disable : 4996)

#define INF (1 << 25)

using namespace std;

int main() {
  int N;

  scanf("%d", &N);

  vector<int> D(N - 1);

  for (int i = 0; i < N - 1; i++) {
    scanf("%d", &D[i]);
  }

  vector<vector<vector<int>>> dp(
      N, vector<vector<int>>(N / 2 + 1, vector<int>(2, INF)));

  dp[0][0][0] = 0;

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

      if (j != 0) {
        dp[i][j][1] = min(dp[i - 1][j - 1][1], dp[i - 1][j - 1][0] + D[i - 1]);
      }
    }
  }

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

  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
const int INF = 1e9;

// dp[i][j][k]
// :=???(?????????????????????i(mod2)??§A???????????????j????????§?¬???????k??????????????????????????????
//?????????????????????????§???°???????°??????????) (k==0:A k==1:B)
int dp[2][5010][2];
int main(void) {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n - 1) cin >> v[i];
  rep(i, 2) rep(j, 5010) rep(k, 2) dp[i][j][k] = INF;

  dp[0][0][0] = 0;
  dp[0][0][1] = 0;
  //??????dp
  for (int i = 0; i < n; ++i) {
    rep(j, 5010) rep(k, 2) dp[(i + 1) % 2][j][k] = INF;
    for (int j = 0; j <= n / 2; ++j) {
      // dp[(i + 1) % 2][j + 1][0] = dp[(i + 1) % 2][j + 1][1] =
      // INF;//????????¨???????????????????????? dp[(i + 1) % 2][j][0] = dp[(i +
      // 1) % 2][j][1] = INF;
      for (int k = 0; k < 2; ++k) {
        if (dp[i % 2][j][k] == INF)
          continue;
        //???????????????????????????
        if (k == 0) {
          dp[(i + 1) % 2][j + 1][k] =
              min(dp[(i + 1) % 2][j + 1][k], dp[i % 2][j][k]);
        } else {
          dp[(i + 1) % 2][j][k] = min(dp[(i + 1) % 2][j][k], dp[i % 2][j][k]);
        }
        //?????????????????????
        if (k == 0) {
          dp[(i + 1) % 2][j + 1][1] =
              min(dp[(i + 1) % 2][j + 1][1], dp[i % 2][j][k] + v[i]);
        } else {
          dp[(i + 1) % 2][j][0] =
              min(dp[(i + 1) % 2][j][0], dp[i % 2][j][k] + v[i]);
        }
      }
    }
  }

  int ans = INF;
  rep(k, 2) { ans = min(ans, dp[n % 2][n / 2][k]); }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
const int INF = 1e9;

// dp[i][j][k]
// :=???(?????????????????????i??§A???????????????j????????§?¬???????k??????????????????????????????
//?????????????????????????§???°???????°??????????) (k==0:A k==1:B)
int dp[10010][5010][2];
int main(void) {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n - 1) cin >> v[i];
  rep(i, 10010) rep(j, 5010) rep(k, 2) dp[i][j][k] = INF;

  dp[0][0][0] = 0;
  dp[0][0][1] = 0;
  //??????dp
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= n / 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        if (dp[i][j][k] == INF)
          continue;
        //???????????????????????????
        if (k == 0) {
          dp[i + 1][j + 1][k] = min(dp[i + 1][j + 1][k], dp[i][j][k]);
        } else {
          dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        }
        //?????????????????????
        if (k == 0) {
          dp[i + 1][j + 1][1] = min(dp[i + 1][j + 1][1], dp[i][j][k] + v[i]);
        } else {
          dp[i + 1][j][0] = min(dp[i + 1][j][0], dp[i][j][k] + v[i]);
        }
      }
    }
  }

  int ans = INF;
  rep(k, 2) { ans = min(ans, dp[n][n / 2][k]); }
  cout << ans << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

constexpr int MAX_N = 10000, INF = (int)1e9;

int n;
int a[MAX_N + 1];
int dp
    [2][MAX_N / 2 + 1][2]
    [2]; //??????????????????????????????????????°???????????????????????????????????´???????????????????????????

int main() {
  std::cin >> n;
  for (int i = 1; i < n; ++i)
    std::cin >> a[i];
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j <= n / 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        for (int l = 0; l < 2; ++l)
          dp[i][j][k][l] = INF;
      }
    }
  }
  for (int i = 0; i <= 1; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k)
        dp[0][j][!j][k] = 0;
    }
  }

  for (int i = 1; i < n; ++i) {
    for (int j = 1; j <= n / 2; ++j) {
      dp[i % 2][j][0][0] =
          std::min(dp[(i - 1) % 2][j - 1][0][0], dp[(i - 1) % 2][j - 1][0][1]);
      dp[i % 2][j][0][1] =
          std::min(dp[(i - 1) % 2][j - 1][1][0], dp[(i - 1) % 2][j - 1][1][1]) +
          a[i];
      dp[i % 2][j][1][0] =
          std::min(dp[(i - 1) % 2][j][0][0], dp[(i - 1) % 2][j][0][1]) + a[i];
      dp[i % 2][j][1][1] =
          std::min(dp[(i - 1) % 2][j][1][0], dp[(i - 1) % 2][j][1][1]);
    }
    dp[i % 2][0][1][1] = 0;
  }

  int ans = INF;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j)
      ans = std::min(ans, dp[1][n / 2][i][j]);
  }
  std::cout << ans << std::endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>

constexpr int MAX_N = 10000, INF = (int)1e9;

int n;
int a[MAX_N + 1];
int dp
    [MAX_N][MAX_N / 2 + 1][2]
    [2]; //??????????????????????????????????????°???????????????????????????????????´???????????????????????????

int main() {
  std::cin >> n;
  for (int i = 1; i < n; ++i)
    std::cin >> a[i];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= n / 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        for (int l = 0; l < 2; ++l)
          dp[i][j][k][l] = INF;
      }
    }
  }
  for (int i = 0; i <= 1; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k)
        dp[0][j][!j][k] = 0;
    }
  }

  for (int i = 1; i < n; ++i) {
    for (int j = 1; j <= n / 2; ++j) {
      dp[i][j][0][0] = std::min(dp[i - 1][j - 1][0][0], dp[i - 1][j - 1][0][1]);
      dp[i][j][0][1] =
          std::min(dp[i - 1][j - 1][1][0], dp[i - 1][j - 1][1][1]) + a[i];
      dp[i][j][1][0] = std::min(dp[i - 1][j][0][0], dp[i - 1][j][0][1]) + a[i];
      dp[i][j][1][1] = std::min(dp[i - 1][j][1][0], dp[i - 1][j][1][1]);
    }
    dp[i][0][1][1] = 0;
  }

  int ans = INF;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j)
      ans = std::min(ans, dp[n - 1][n / 2][i][j]);
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

// unsolved

#include <bits/stdc++.h>
using namespace std;

// #define DEBUG
#define INF 1000000000
#define REP(i, n) for (int i = 0; i < n; i++)
int n;
int a[10002];
int dp[2][5002][2];

int main() {
  cin >> n;
  REP(i, n - 1) cin >> a[i + 1];
  REP(i, 2) REP(j, n / 2 + 1) REP(k, 2) dp[i][j][k] = INF;
  dp[0][0][0] = 0;
  dp[1][1][1] = 0;
  dp[1][0][0] = 0;
  REP(i, n) {
    REP(j, n / 2) {
      if (j + 1 > i)
        continue;
      dp[(i + 1) & 1][j + 1][0] =
          min(dp[i & 1][j + 1][0], dp[i & 1][j + 1][1] + a[i]);
      if (j > i)
        continue;
      dp[(i + 1) & 1][j + 1][1] = min(dp[i & 1][j][0] + a[i], dp[i & 1][j][1]);
    }
#ifdef DEBUG
    REP(j, n / 2) {
      cout << dp[(i + 1) & 1][j + 1][0] << "/";
      cout << dp[(i + 1) & 1][j + 1][1] << " ";
    }
    cout << endl;
#endif
  }
  cout << min(dp[0][n / 2][0], dp[0][n / 2][1]) << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// #define DEBUG
#define INF 1000000000
#define REP(i, n) for (int i = 1; i <= n; i++)
int n;
int a[10002];
int dp[5002][5002];

int main() {
  cin >> n;
  REP(i, n - 1) cin >> a[i];
  REP(i, n / 2) REP(j, n / 2 + 1) dp[i][j - 1] = INF;
  REP(i, n / 2) {
    REP(j, n / 2) {
      dp[i][j] = min(min(dp[i][j], dp[i][j - 1]), dp[j - 1][i] + a[i + j - 1]);
    }
  }
#ifdef DEBUG
  REP(i, n / 2 + 1) {
    REP(j, n / 2 + 1) cout << dp[i][j] << " ";
    cout << endl;
  }
#endif
  cout << dp[n / 2][n / 2] << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
// #define int long long
using namespace std;
typedef long long ll;

int k[10000];
int dp[2][5101][2];

signed main() {
  int N, i, j;
  cin >> N;
  rep(i, N - 1) cin >> k[i];

  memset(dp, 0x7f, sizeof(dp));
  dp[0][0][0] = 0;
  rep(i, N) {
    int m = min(5009, i);
    for (j = 0; j <= m; j++) {
      dp[(i + 1) & 1][j + 1][0] = min(dp[i & 1][j][0], dp[i & 1][j][1] + k[i]);
      dp[(i + 1) & 1][j][1] = min(dp[i & 1][j][1], dp[i & 1][j][0] + k[i]);
    }
    dp[i & 1][0][0] = 0x7f7f;
  }

  cout << dp[N % 2][N / 2][0] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
using namespace std;
typedef long long ll;

int N;
int t[10010];

//??????l????????????r?????§??????len???????????????????°????
int rec(int l, int r, int len) {
  int ret = INT_MAX;
  for (int i = l + 1; i <= r - 1; i++) {
    if (i - l == len) {
      ret = min(ret, t[i - 1]);
    } else if (r - i == len) {
      ret = min(ret, t[i - 1]);
    } else if (i - l < len) {
      ret = min(ret, t[i - 1] + rec(i, r, len - i + l));
      ret = min(ret, t[i - 1] + rec(i, r, len));
    } else if (r - i < len) {
      ret = min(ret, t[i - 1] + rec(l, i, len - r + i));
      ret = min(ret, t[i - 1] + rec(l, i, len));
    } else {
      ret = min(ret, t[i - 1] + rec(l, i, len));
      ret = min(ret, t[i - 1] + rec(i, r, len));
    }
  }
  return ret;
}

signed main() {
  cin >> N;
  rep(i, N - 1) cin >> t[i];

  cout << rec(0, N, N / 2) << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  const int S = n / 2;

  vector<int> t(n, 0);
  REP(i, 1, n) cin >> t[i];

  enum { A, B, NUM };
  constexpr int INF = (1 << 29);
  vector<vector<int>> dp(S + 2, vector<int>(NUM, INF));
  dp[0][A] = dp[0][B] = 0;

  rep(i, n) {
    vector<vector<int>> next(S + 2, vector<int>(NUM, INF));

    rep(j, min(S, i) + 1) {
      next[j + 1][A] = min(dp[j][A], dp[j][B] + t[i]);
      next[j][B] = min(dp[j][A] + t[i], dp[j][B]);
    }

    dp = move(next);
  }

  cout << min(dp[S][A], dp[S][B]) << endl;

  return EXIT_SUCCESS;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

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

template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> t(n - 1);
  for (auto &e : t)
    cin >> e;

  t.emplace_back(0);

  const int S = n / 2;

  constexpr int INF = (1 << 29);
  vector<vector<int>> dp(n + 1, vector<int>(S + 1, INF));
  dp[0][0] = 0;

  rep(l, n) {
    REP(r, l + 1, n + 1) {
      rep(i, S + 1) { chmin(dp[r][i], dp[l][i] + t[r - 1]); }

      const int inc = r - l;
      rep(i, S - inc + 1) { chmin(dp[r][i + inc], dp[l][i] + t[r - 1]); }
    }
  }

  cout << dp[n][S] << endl;

  return EXIT_SUCCESS;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int dp[2][5001][2];

int main() {
  int N, a[10000];
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> a[i];
  }

  fill_n(**dp, 2 * 5001 * 2, INF);

  // dp[i][false] <- dp[i - 1][false]は切らなみ dp[i - 1][true]は切り身
  // dp[i][true] <- dp[i - 1][true]は切らなみ   dp[i - 1][false]は切り身

  // ケーキの長さもほしみありそう

  dp[0][0][false] = dp[0][1][true] = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = N / 2; j >= 0; j--) { // 今の長さ
      dp[(i + 1) & 1][j][false] =
          min(dp[i & 1][j][false], dp[i & 1][j][true] + a[i]);
      if (j > 0) {
        dp[(i + 1) & 1][j][true] =
            min(dp[i & 1][j - 1][true], dp[i & 1][j - 1][false] + a[i]);
      }
    }
    for (int j = 0; j <= N / 2; j++) {
      dp[i & 1][j][false] = dp[i & 1][j][true] = INF;
    }
  }
  cout << min(dp[(N - 1) & 1][N / 2][false], dp[(N - 1) & 1][N / 2][true])
       << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int dp[10001][5001][2];

int main() {
  int N, a[10000];
  cin >> N;
  for (int i = 1; i < N; i++) {
    cin >> a[i];
  }

  fill_n(**dp, 10001 * 5001 * 2, INF);

  // dp[i][false] <- dp[i - 1][false]は切らなみ dp[i - 1][true]は切り身
  // dp[i][true] <- dp[i - 1][true]は切らなみ   dp[i - 1][false]は切り身

  // ケーキの長さもほしみありそう

  // dp[i][j]: 切り目i で
  // [N][N / 2]が答え
  dp[0][0][false] = dp[0][0][true] = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= N / 2; j++) { // 今の長さ
      dp[i][j][false] = min(dp[i - 1][j][false], dp[i - 1][j][true] + a[i]);
      if (j > 0)
        dp[i][j][true] =
            min(dp[i - 1][j - 1][true], dp[i - 1][j - 1][false] + a[i]);
    }
  }

  cout << min(dp[N - 1][N / 2][false], dp[N - 1][N / 2][true]) << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef int useType;
typedef pair<useType, useType> pr;
typedef vector<useType> VI;
const int INF = 1 << 30;
int DP[2][2][10002];
int main() {
  int n, p;
  cin >> n;
  REP(i, 2)
  REP(j, 10002) {
    DP[0][i][j] = INF;
    DP[1][i][j] = INF;
  }

  DP[0][0][1] = 0;
  DP[0][1][0] = 0;
  REP(i, n - 1) {
    cin >> p;
    FOR(k, 1, n + 1)
    DP[(i + 1) & 1][0][k] = min(DP[i & 1][0][k - 1], DP[i & 1][1][k - 1] + p);
    REP(k, n + 1)
    DP[(i + 1) & 1][1][k] = min(DP[i & 1][0][k] + p, DP[i & 1][1][k]);
  }
  cout << min(DP[(n - 1) & 1][0][n / 2], DP[(n - 1) & 1][1][n / 2]) << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef int useType;
typedef pair<useType, useType> pr;
typedef vector<useType> VI;
const int INF = 1 << 30;
int DP[2][2][10002];
int main() {
  int n, p;

  REP(i, 2)
  REP(j, 10002) {
    DP[0][i][j] = INF;
    DP[1][i][j] = INF;
  }

  DP[0][0][1] = 0;
  DP[0][1][0] = 0;
  REP(i, n - 1) {
    cin >> p;
    FOR(k, 1, n + 1)
    DP[(i + 1) & 1][0][k] = min(DP[i & 1][0][k - 1], DP[i & 1][1][k - 1] + p);
    REP(k, n + 1)
    DP[(i + 1) & 1][1][k] = min(DP[i & 1][0][k] + p, DP[i & 1][1][k]);
  }
  cout << min(DP[(n - 1) & 1][0][n / 2], DP[(n - 1) & 1][1][n / 2]) << endl;
  while (scanf("%d", &n) != EOF)
    ;
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#define MANM 10000

int dp[2][MANM + 5] /*[MAXN*/ = {0};
int n = 0;
int c[MANM + 1] = {0};

int mins(int a, int b) {
  // if(a==0)return b;
  if (a < b /*|| b==0*/)
    return a;
  else
    return b;
}

int saiki(int p, int q) {

  /*if(p==1 || q==0 || p==q ||  dp[p][q]!=0){
    return dp[p][q];
  }*/
  int a = 0;
  int b = 0;
  int i = 0;
  int j = 0;
  dp[1][1] = 0;
  dp[1][0] = c[1];
  for (i = 2; i <= p; i++) {
    a = i % 2;
    b = (i + 1) % 2;
    dp[a][0] = c[i];
    for (j = 1; j <= i; j++) {
      dp[a][j] = mins(dp[b][j - 1], dp[b][i - j] + c[i - 1]);
    }
  }
  return dp[p % 2][q];
}
// dp[1][1]=0
// dp[1][0]=0
// dp[i][0]=0
// dp[i][j]=左から[i]までで、右端を含む組がj長になるの。
// dp[i][j]=min{dp[i-1][j-1],dp[i-1][i-j]+c[i-1]}
int main(void) {

  scanf("%d", &n);
  int i = 0;
  for (i = 1; i < n; i++) {
    scanf("%d", &(c[i]));
  }

  int ans = saiki(n, n / 2);
  printf("%d\\n", ans);
  return 0;
}


Error: MLE

Fixed submission:

#include <cstdio>
#define MANM 10000

int dp[MANM + 1][MANM] = {0};
int n = 0;
int c[MANM] = {0};
using namespace std;

int mins(int a, int b) {
  if (a == 0)
    return b;
  if (a < b || b == 0)
    return a;
  else
    return b;
}

int saiki(int p, int q) {

  if (p == 1 || q == 0 || p == q || dp[p][q] != 0) {
    return dp[p][q];
  }
  int m = 0;
  int a = 0;
  int b = 0;
  a = saiki(p - 1, q - 1);
  b = saiki(p - 1, p - q) + c[p - 2];
  m = mins(a, b);
  dp[p][q] = m;
  return m;
}

int main(void) {

  scanf("%d", &n);
  int i = 0;
  for (i = 0; i < n - 1; i++) {
    scanf("%d", &(c[i]));
  }

  int ans = saiki(n, n / 2);
  printf("%d", ans);
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, a[100000], dp[5005][2];
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++)
    cin >> a[i];
  for (int i = 0; i < 5005; i++) {
    dp[i][0] = 999999999;
    dp[i][1] = 999999999;
  }
  dp[n / 2][0] = 0;
  dp[n / 2][1] = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= n / 2; j++) {
      dp[j][0] = min(dp[j][0], dp[j][1] + a[i]);
      dp[j][1] = min(dp[j + 1][1], dp[j + 1][0] + a[i]);
    }
  }
  cout << min(dp[0][0], dp[0][1]) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, a[100000], dp[10000][5000][2];
int solve(int p, int r, int u) {
  if (dp[p][r][u] >= 1)
    return dp[p][r][u];
  if (p == n) {
    if (r == n / 2)
      return 0;
    return 999999999;
  }
  if (r > n / 2)
    return 999999999;
  int Y = 0;
  if (u == 0)
    Y = min(solve(p + 1, r, u), solve(p + 1, r, 1 - u) + a[p]);
  if (u == 1)
    Y = min(solve(p + 1, r + 1, u), solve(p + 1, r + 1, 1 - u) + a[p]);
  if (Y >= 10001)
    Y = 999999999;
  dp[p][r][u] = Y;
  return Y;
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++)
    cin >> a[i];
  cout << solve(0, 0, 0) << endl;
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX_N 500000
#define INF 2000000000

int dp[2][MAX_N]
      [2]; // dp[????????????][???????????§??§A?????????????????????][??´???]
int n, a[MAX_N];

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, 127, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[0][j][0] < INF) {
        dp[1][j + 1][0] = min(dp[1][j + 1][0], dp[0][j][0]);
        dp[1][j + 1][1] = min(dp[1][j + 1][1], dp[0][j][0] + a[i + 1]);
      }
      if (dp[0][j][1] < INF) {
        dp[1][j][1] = min(dp[1][j][1], dp[0][j][1]);
        dp[1][j][0] = min(dp[1][j][0], dp[0][j][1] + a[i + 1]);
      }
    }
    for (int j = 0; j <= n; j++) {
      dp[0][j][0] = dp[1][j][0];
      dp[0][j][1] = dp[1][j][1];
      dp[1][j][0] = INF;
      dp[1][j][1] = INF;
    }
  }
  cout << min(dp[0][n / 2][0], dp[0][n / 2][1]) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX_N 2000
#define INF 2000000000

int dp[2][MAX_N]
      [2]; // dp[????????????][???????????§??§A?????????????????????][??´???]
int n, a[MAX_N];

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, 127, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[0][j][0] < INF) {
        dp[1][j + 1][0] = min(dp[1][j + 1][0], dp[0][j][0]);
        dp[1][j + 1][1] = min(dp[1][j + 1][1], dp[0][j][0] + a[i + 1]);
      }
      if (dp[0][j][1] < INF) {
        dp[1][j][1] = min(dp[1][j][1], dp[0][j][1]);
        dp[1][j][0] = min(dp[1][j][0], dp[0][j][1] + a[i + 1]);
      }
    }
    for (int j = 0; j <= n; j++) {
      dp[0][j][0] = dp[1][j][0];
      dp[0][j][1] = dp[1][j][1];
      dp[1][j][0] = INF;
      dp[1][j][1] = INF;
    }
  }
  cout << min(dp[0][n / 2][0], dp[0][n / 2][1]) << endl;
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)

int n;
int t[10000];
int dp[2][5001][2];
//[i][j][k] = i???????????§??§j??¬?????£???????????´????????¶???k(i -
//1??????????£?????????£??????)?????¨??????????????????????°????

int main() {
  cin >> n;
  REP(i, n - 1) scanf("%d", &t[i]);
  t[n - 1] = 0;

  fill(dp[0][0], dp[2][0], INF);

  dp[0][0][0] = 0;
  REP(i, n) {
    int now = i % 2, next = (i + 1) % 2;
    REP(j, n / 2 + 1) {
      // ??????
      if (j < n / 2)
        dp[next][j + 1][1] = min(dp[now][j][1], dp[now][j][0] + t[i]);

      // ????????????
      dp[next][j][0] = min(dp[now][j][0], dp[now][j][1] + t[i]);
    }
  }

  cout << dp[n % 2][n / 2][0] << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)

int n;
int t[10000];
int dp[10000][5000]; //[i][j] = i????????\\?????§j??¬??????????°?

// d????????\\?????§a?????????????°????
int dfs(int d, int a) {
  if (a == 0)
    return 0;
  if (a < 0 || d == n - 1)
    return INF;
  if (dp[d][a] != INF)
    return dp[d][a];

  int res = INF;
  FOR(i, d + 1, n) {
    res = min(res, t[i - 1] + dfs(i, a - (i - d)));
    res = min(res, t[i - 1] + dfs(i, a));
  }
  return dp[d][a] = res;
}

int main() {
  cin >> n;
  REP(i, n - 1) scanf("%d", &t[i]);

  fill(dp[0], dp[n], INF);
  cout << dfs(0, n / 2) << endl;
  return 0;
}