alexjercan/bugnet huggingface dataset:
Problem p00473 (C++) — buggy submission, verdict: Runtime Error

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int dp[5005][2][2];
int main() {
  int n;
  int v[10005];
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d", &v[i]);
  }
  for (int i = 0; i < 5005; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 2; k++)
        dp[i][j][k] = INF;
  dp[1][0][0] = 0;
  int cur = 0, nex = 1;
  for (int i = 1; i < n; i++) {
    for (int i = 0; i < 5005; i++)
      for (int j = 0; j < 2; j++)
        dp[i][j][nex] = INF;
    for (int j = min(n / 2, i); j >= max(0, i - n / 2); j--) {
      dp[j + 1][0][nex] = min(dp[j + 1][0][nex], dp[j][0][cur]);
      dp[j][1][nex] = min(dp[j][1][nex], dp[j][0][cur] + v[i]);

      dp[j][1][nex] = min(dp[j][1][nex], dp[j][1][cur]);
      dp[j + 1][0][nex] = min(dp[j + 1][0][nex], dp[j][1][cur] + v[i]);
    }
    cur = 1 - cur;
    nex = 1 - nex;
  }
  printf("%d\\n", min(dp[n / 2][0][cur], dp[n / 2][1][cur]));
}

Error: 0

Fixed submission:

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int dp[5005][2][2];
int main() {
  int n;
  int v[10005];
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d", &v[i]);
  }
  for (int i = 0; i < 5005; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 2; k++)
        dp[i][j][k] = INF;
  dp[1][0][0] = 0;
  int cur = 0, nex = 1;
  for (int i = 1; i < n; i++) {
    for (int i = 0; i < 5005; i++)
      for (int j = 0; j < 2; j++)
        dp[i][j][nex] = INF;
    for (int j = n - 1; j >= 0; j--) {
      dp[j + 1][0][nex] = min(dp[j + 1][0][nex], dp[j][0][cur]);
      dp[j][1][nex] = min(dp[j][1][nex], dp[j][0][cur] + v[i]);

      dp[j][1][nex] = min(dp[j][1][nex], dp[j][1][cur]);
      dp[j + 1][0][nex] = min(dp[j + 1][0][nex], dp[j][1][cur] + v[i]);
    }
    cur = 1 - cur;
    nex = 1 - nex;
  }
  printf("%d\\n", min(dp[n / 2][0][cur], dp[n / 2][1][cur]));
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M INT_MAX
int dp[4][5001][2];
int main(void) {
  int n, nn, ti[10001], i, j, k, t, p, pp;
  cin >> n;
  nn = n / 2;
  for (i = 0; i < n - 1; i++)
    cin >> ti[i];
  ti[n - 1] = 0;
  //	for(i=0;i<n;i++) cout<<ti[i]<<endl;
  for (i = 0; i <= 2; i++) {
    for (j = 0; j <= nn; j++)
      dp[i][j][0] = M, dp[i][j][1] = M;
  }
  dp[0][1][0] = ti[0];
  for (i = 0; i < n - 1; i++) {
    if (i % 2 == 0)
      p = 0, pp = 1;
    else
      p = 1, pp = 0;
    for (j = 0; j <= nn; j++)
      dp[pp][j][0] = M, dp[pp][j][1] = M;
    for (j = 1; j <= nn; j++) {
      if (dp[p][j][0] != M) {
        dp[pp][j][1] = min(dp[pp][j][1], dp[p][j][0]);
        t = dp[p][j][0] - ti[i] + ti[i + 1];
        dp[pp][j + 1][0] = min(dp[pp][j + 1][0], t);
      }
      if (dp[p][j][1] != M) {
        dp[pp][j][1] = min(dp[pp][j][1], dp[p][j][1]);
        t = dp[p][j][1] + ti[i + 1] + ti[i];
        dp[pp][j + 1][0] = min(dp[pp][j + 1][0], t);
      }
    }
  }
  cout << min(dp[1][nn][0], dp[1][nn][1]) << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M INT_MAX
int dp[10001][5001][2];
int main(void) {
  int n, nn, ti[10001], i, j, k, t, mi;
  cin >> n;
  nn = n / 2;
  for (i = 1; i < n; i++)
    cin >> ti[i];
  ti[n] = 0;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= nn; j++)
      dp[i][j][0] = M, dp[i][j][1] = M;
  }
  dp[1][1][0] = ti[1];
  for (i = 1; i < n; i++) {
    for (j = 1; j <= nn; j++) {
      if (dp[i][j][0] != M) {
        dp[i + 1][j][1] = min(dp[i + 1][j][1], dp[i][j][0]);
        t = dp[i][j][0] - ti[i] + ti[i + 1];
        dp[i + 1][j + 1][0] = min(dp[i + 1][j + 1][0], t);
      }
      if (dp[i][j][1] != M) {
        dp[i + 1][j][1] = min(dp[i + 1][j][1], dp[i][j][1]);
        t = dp[i][j][1] + ti[i + 1] + ti[i];
        dp[i + 1][j + 1][0] = min(dp[i + 1][j + 1][0], t);
      }
    }
  }
  /*	for(i=1;i<=n;i++){
                  for(j=1;j<=nn;j++) cout<<dp[i][j][0]<<':'<<dp[i][j][1]<<' ';
                  cout<<endl;
          }*/
  cout << min(dp[n][nn][0], dp[n][nn][1]) << endl;
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int n, a[10009], dp[5009][2];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i <= n >> 1; i++)
    dp[i][0] = dp[i][1] = 999999999;
  dp[0][0] = dp[1][1] = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = n >> 1; j >= 0; j--) {
      dp[j][0] = min(dp[j][0], dp[j][1] + a[i]);
      dp[j][1] = (j ? min(dp[j - 1][1], dp[j - 1][0] + a[i]) : 999999999);
    }
  }
  printf("%d\\n", dp[n >> 1][0]);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#pragma warning(disable : 4996)
using namespace std;
int n, a[10009], dp[10009][5009][2];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i <= n >> 1; i++)
    dp[0][i][0] = dp[0][i][1] = 999999999;
  dp[0][0][0] = dp[0][1][1] = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j <= n >> 1; j++) {
      dp[i + 1][j][0] = min(dp[i][j][0], dp[i][j][1] + a[i]);
      dp[i + 1][j][1] =
          (j ? min(dp[i][j - 1][1], dp[i][j - 1][0] + a[i]) : 999999999);
    }
  }
  printf("%d\\n", dp[n - 1][n >> 1][0]);
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <iostream>

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

using namespace std;

int cut[10000];
int memo[2][5010][2];

int main() {
  int n;
  const int MAX = 10000 * 10000 * 2;
  scanf("%d", &n);
  REP(i, n - 1) scanf("%d", cut + i + 1);

  REP(i, 2) REP(j, 5001) REP(k, 2) memo[i][j][k] = MAX;
  memo[0][0][0] = 0;

  REP(i, n) {
    REP(j, n / 2 + 1) {
      memo[(i + 1) % 2][j + 1][0] =
          min(memo[i % 2][j][0], memo[i % 2][j][1] + cut[i]);
      memo[(i + 1) % 2][j][1] =
          min(memo[i % 2][j][1], memo[i % 2][j][0] + cut[i]);
    }
    memo[(i + 1) % 2][0][0] = MAX;
  }

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

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#define print(x) printf("%d\\n", x)

#define f first
#define s second
#define mp make_pair

using namespace std;

int cut[10000];
bool memo[5001][5001];

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n - 1) scanf("%d", cut + i);

  typedef pair<int, pair<int, int>> data;
  priority_queue<data, vector<data>, greater<data>> pq;

  pq.push(mp(0, mp(0, 0)));

  while (pq.size()) {
    data d = pq.top();
    pq.pop();

    int cost = d.f;
    int left = d.s.f;
    int right = d.s.s;

    // printf("%d %d %d\\n", cost, left, right);

    if (memo[left][right])
      continue;
    memo[left][right] = true;

    if (right == n / 2) {
      print(cost);
      break;
    }

    int sum = left + right;

    for (int i = sum; i < n - 1; i++) {
      int add = i - sum + 1;
      int cc = cut[i];

      {
        int nl = left + add;
        int nr = right;
        if (nl > nr)
          swap(nl, nr);
        if (!memo[nl][nr])
          pq.push(mp(cost + cc, mp(nl, nr)));
      }
      {
        int nl = left;
        int nr = right + add;
        if (nl > nr)
          swap(nl, nr);
        if (!memo[nl][nr])
          pq.push(mp(cost + cc, mp(nl, nr)));
      }
    }
  }
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int dp[2][5005][2]{}; // 1 here 0 there
int main() {
  for (int x = 0; x < 2; x++) {
    for (int y = 0; y < 5000; y++) {
      dp[x][y][0] = dp[x][y][1] = 1 << 29;
    }
  }
  dp[0][0][0] = dp[0][1][1] = 0;
  int a;
  cin >> a;
  for (int b = 1; b < a; b++) {
    for (int i = 0; i <= a / 2; i++)
      dp[b & 1][i][0] = dp[b & 1][i][1] = 1 << 29;
    int c;
    scanf("%d", &c);
    dp[b & 1][0][0] = dp[(b + 1) & 1][0][0];
    for (int d = a / 2; d >= 1; d--) {
      dp[b & 1][d][1] =
          min({dp[(b + 1) & 1][d - 1][1], dp[(b + 1) & 1][d - 1][0] + c});
      dp[b & 1][d][0] = min({dp[(b + 1) & 1][d][0], dp[(b + 1) & 1][d][1] + c});
    }
  }
  cout << dp[(a + 1) & 1][a / 2][1] << endl;
}

Error: MLE

Fixed submission:

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

int dp[10005][5005][2]{}; // 1 here 0 there
int main() {
  for (int x = 0; x < 10000; x++) {
    for (int y = 0; y < 5000; y++) {
      dp[x][y][0] = dp[x][y][1] = 1 << 29;
    }
  }
  dp[0][0][0] = dp[0][1][1] = 0;
  int a;
  cin >> a;
  for (int b = 1; b < a; b++) {
    for (int i = 0; i <= a / 2; i++)
      dp[b][i][0] = dp[b][i][1] = 1 << 29;
    int c;
    scanf("%d", &c);
    dp[b][0][0] = dp[b - 1][0][0];
    for (int d = a / 2; d >= 1; d--) {
      dp[b][d][1] = min({dp[b - 1][d - 1][1], dp[b - 1][d - 1][0] + c});
      dp[b][d][0] = min({dp[b - 1][d][0], dp[b - 1][d][1] + c});
    }
  }
  cout << dp[a - 1][a / 2][1] << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int inf = 1e9 + 9;

int n;
int t[10101];

int dp[2][2][5050];

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> t[i];
  }

  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 5050; k++)
        dp[i][j][k] = inf;

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

      dp[i & 1][0][j] = dp[i & 1][1][j] = inf;
    }
  }

  cout << dp[n & 1][0][n / 2] << endl;
}

Error: TLE

Fixed submission:

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

const int inf = 1e9 + 9;

int n;
int t[10101];

int dp[10010][5010][2];

int dfs(int i = 0, int l = n / 2, bool f = false) {
  if (l < 0)
    return inf;
  if (i == n) {
    if (l == 0) {
      return 0;
    } else {
      return inf;
    }
  }

  int &ret = dp[i][l][f];
  ret = inf;
  for (int j = i + 1; j <= n; j++) {
    ret = min(ret, dfs(j, l - (f ? (j - i) : 0), !f) + t[i]);
  }

  return ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> t[i];
  }

  cout << dfs() << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dp[2][5002][2];
int main() {
  int N;
  cin >> N;
  vector<int> v(N - 1);
  REP(i, N - 1) cin >> v[i];
  REP(i, 2) REP(j, 5002) REP(k, 2) dp[i][j][k] = INF;
  dp[0][1][0] = 0;
  REP(i, N - 1) {
    REP(j, N / 2 + 1) {
      dp[(i + 1) % 2][j + 1][0] = min(dp[i % 2][j][0], dp[i % 2][j][1] + v[i]);
      dp[(i + 1) % 2][j][1] = min(dp[i % 2][j][0] + v[i], dp[i % 2][j][1]);
    }
    REP(j, N / 2 + 1) dp[i % 2][j][0] = dp[i % 2][j][1] = INF;
  }
  cout << min(dp[(N - 1) % 2][N / 2][0], dp[(N - 1) % 2][N / 2][1]) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
void update(int &to, int from) { to = min(to, from); }

int main() {
  int N;
  while (cin >> N && N) {
    vector<int> v(N - 1);
    REP(i, N - 1) cin >> v[i];
    static int dp[10001][5001][2];
    memset(dp, 0, sizeof(dp));
    REP(i, N) REP(j, N / 2 + 1) dp[i][j][0] = dp[i][j][1] = INF;
    dp[0][1][0] = 0;
    dp[0][0][1] = 0;
    REP(i, N - 1) REP(j, N / 2 + 1) {
      if (j + 1 <= N / 2)
        update(dp[i + 1][j + 1][0], dp[i][j][0]);
      if (j + 1 <= N / 2)
        update(dp[i + 1][j + 1][0], dp[i][j][1] + v[i]);
      update(dp[i + 1][j][1], dp[i][j][0] + v[i]);
      update(dp[i + 1][j][1], dp[i][j][1]);
    }
    /*
    REP(i, N-1) REP(k, 2) {
      printf("i:%d k:%d ", i, k);
      REP(j, N/2+1) printf("%d ", dp[i][j][k]);
      printf("\\n");
    }
    */
    cout << min(dp[N - 1][N / 2][0], dp[N - 1][N / 2][1]) << endl;
  }
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int MAX_N = 10005;
const int INF = 1 << 29;
int N;
int Cost[MAX_N];
int T[2][2][MAX_N];

int solve() {
  fill(T[0][0], T[2][0], INF);
  T[1][0][0] = T[1][1][1] = 0;
  for (int i = 2; i <= N; ++i) {
    fill(T[i % 2][0], T[i % 2][2], INF);
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k <= N; ++k) {
        for (int l = 0; l < 2; ++l) {
          int cost = (j == l ? 0 : Cost[i - 2]);
          if (k - j < 0)
            continue;
          T[i % 2][j][k] = min(T[i % 2][j][k], T[(i - 1) % 2][l][k - j] + cost);
        }
      }
    }
  }
  return min(T[0][0][N / 2], T[0][1][N / 2]);
}

int main() {
  cin >> N;
  for (int i = 0; i < N - 1; ++i)
    cin >> Cost[i];
  cout << solve() << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

const int MAX_N = 10005;
const int INF = 1 << 29;
int N;
int Cost[MAX_N];
int T[MAX_N][2][MAX_N / 2];

int solve() {
  fill(T[0][0], T[MAX_N][0], INF);
  T[1][0][0] = T[1][1][1] = 0;
  for (int i = 2; i <= N; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k <= min(i, N / 2); ++k) {
        for (int l = 0; l < 2; ++l) {
          int cost = (j == l ? 0 : Cost[i - 2]);
          if (k - l < 0)
            continue;
          T[i][j][k] = min(T[i][j][k], T[i - 1][l][k - l] + cost);
        }
      }
    }
  }
  return min(T[N][0][N / 2], T[N][1][N / 2]);
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i < N - 1; ++i) {
      cin >> Cost[i];
    }
    cout << solve() << endl;
  }
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N, a;
  vector<int> dp;
  cin >> N;
  vector<int> cost;
  for (int i = 0; i < N - 1; i++) {
    cin >> a;
    cost.push_back(a);
  }
  for (int i = 2; i <= N; i++) {
    vector<int> temp;
    for (int j = 0; j <= i; j++) {
      if (j == i || j == 0) {
        temp.push_back(0);
      } else if (j == 1) {
        temp.push_back(cost[i - 2]);
      } else {
        temp.push_back(min(dp[j - 1], cost[i - 2] + dp[i - j]));
      }
    }
    dp = temp;
  }
  cout << dp[N / 2] << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N, a;
  vector<int> dp[10005];
  cin >> N;
  vector<int> cost;
  for (int i = 0; i < N - 1; i++) {
    cin >> a;
    cost.push_back(a);
  }
  for (int i = 2; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == i || j == 0) {
        dp[i].push_back(0);
      } else if (j == 1) {
        dp[i].push_back(cost[i - 2]);
      } else {
        dp[i].push_back(min(dp[i - 1][j - 1], cost[i - 2] + dp[i - 1][i - j]));
      }
    }
  }
  cout << dp[N][N / 2] << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
#define INF (1 << 28)
int cut[10000], n;
#define rep(i, n) for (int i = 0; i < n; i++)
int dp[2][5001][2];
/*
int dfs(int pos,int amount){
        if(amount > n/2) return INF;
        if(pos == n) return amount == n/2 ? 0 : INF;
        int ans = INF;
        for(int i = 1 ; i+pos <= n ; i++){
                ans = min( dfs( i+pos , amount+i) + cut[pos+i-1] , ans );
                ans = min( dfs( i+pos , amount  ) + cut[pos+i-1] , ans );
        }
        return ans;
}
*/
int main() {
  cin >> n;
  rep(i, n - 1) cin >> cut[i];
  rep(i, 2) rep(j, 5001) rep(k, 2) dp[i][j][k] = INF;
  dp[0][0][0] = 0;
  dp[0][0][1] = 0;
  rep(i, n) {
    rep(amount, n / 2 + 1) rep(f, 2) dp[(i + 1) & 1][amount][f] = INF;
    rep(amount, n / 2 + 1) {
      rep(f, 2) {
        if (f == 0) {
          dp[(i + 1) & 1][amount + 1][0] =
              min(dp[(i + 1) & 1][amount + 1][0], dp[i & 1][amount][f]);
          dp[(i + 1) & 1][amount + 1][1] = min(dp[(i + 1) & 1][amount + 1][1],
                                               cut[i] + dp[i & 1][amount][f]);
        } else {
          dp[(i + 1) & 1][amount][1] =
              min(dp[(i + 1) & 1][amount][1], dp[i & 1][amount][f]);
          dp[(i + 1) & 1][amount][0] =
              min(dp[(i + 1) & 1][amount][0], cut[i] + dp[i & 1][amount][f]);
        }
      }
    }
  }
  cout << min(dp[n & 1][n / 2][0], dp[n & 1][n / 2][1]) << endl;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
#define INF (1 << 28)
int cut[10000], n;
int dp[10001][5001];
#define rep(i, n) for (int i = 0; i < n; i++)
/*
int dfs(int pos,int amount){
        if(amount > n/2) return INF;
        if(pos == n) return amount == n/2 ? 0 : INF;
        int ans = INF;
        for(int i = 1 ; i+pos <= n ; i++){
                ans = min( dfs( i+pos , amount+i) + cut[pos+i-1] , ans );
                ans = min( dfs( i+pos , amount  ) + cut[pos+i-1] , ans );
        }
        return ans;
}
*/
int main() {
  cin >> n;
  rep(i, n) cin >> cut[i];
  rep(i, 10001) rep(j, 5001) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(pos, n) {
    rep(amount, n / 2 + 1) {
      for (int i = 1; i + pos <= n; i++) {
        if (i + amount <= n / 2)
          dp[i + pos][amount + i] =
              min(dp[i + pos][amount + i], cut[pos + i - 1] + dp[pos][amount]);
        dp[i + pos][amount] =
            min(dp[i + pos][amount], cut[pos + i - 1] + dp[pos][amount]);
      }
    }
  }
  cout << dp[n][n / 2] << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

int dp[2][5001];

int main() {
  int n, in;

  scanf("%d", &n);

  for (int i = 0; i <= n / 2; i++)
    dp[0][i] = dp[1][i] = 2000000000;

  dp[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    scanf("%d", &in);
    for (int j = n / 2 - 1; j >= 0; j--) {
      dp[1][j + 1] = dp[1][j];
    }
    dp[1][0] = 2000000000;
    for (int j = 0; j <= n / 2; j++) {
      dp[0][j] = min(dp[1][j] + in, dp[0][j]);
      dp[1][j] = min(dp[0][j] + in, dp[1][j]);
    }
  }

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

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

int main() {
  int n, in;

  scanf("%d", &n);

  for (int i = 0; i <= n / 2; i++)
    for (int j = 0; j <= n / 2; j++) {
      dp[0][i][j] = 2000000000;
      dp[1][i][j] = 2000000000;
    }

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

  for (int i = 0; i < n; i++) {
    scanf("%d", &in);
    for (int j = 0; j <= i; j++) {
      if (j + 1 <= n / 2 && i - j <= n / 2)
        dp[0][j + 1][i - j] = dp[0][j][i - j];
      if (j <= n / 2 && i - j + 1 <= n / 2)
        dp[1][j][i - j + 1] = dp[1][j][i - j];
    }
    for (int j = 0; j <= i + 1; j++) {
      if (j <= n / 2 && i - j + 1 <= n / 2) {
        dp[0][j][i - j + 1] =
            min(dp[0][j][i - j + 1], dp[1][j][i - j + 1] + in);
        dp[1][j][i - j + 1] =
            min(dp[1][j][i - j + 1], dp[0][j][i - j + 1] + in);
      }
    }
  }

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

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

#include <algorithm>
#include <stdio.h>
using namespace std;
int N;
int T[10010];
int dp[10][10010] = {0};
int ANS = 100000000;
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N - 1; i++)
    scanf("%d", &T[i]);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i + 1; j++) {
      if (i == j)
        dp[1][j] = T[i];
      else {
        dp[1][j] = min(dp[0][j], T[i] + dp[0][i - j - 1]);
      }
      if (j == N / 2 - 1)
        ANS = min(ANS, dp[1][j]);
    }
    for (int j = 0; j < 10000; j++) {
      dp[0][j] = dp[1][j];
    }
  }
  printf("%d\\n", ANS);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int N;
int T[10010];
int dp[10010][10010] = {0};
int ANS = 100000000;
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N - 1; i++)
    scanf("%d", &T[i]);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i + 1; j++) {
      if (i == j)
        dp[i][j] = T[i];
      else {
        dp[i][j] = min(dp[i - 1][j], T[i] + dp[i - 1][i - j - 1]);
      }
      if (j == N / 2 - 1)
        ANS = min(ANS, dp[i][j]);
    }
  }
  printf("%d\\n", ANS);
  return 0;
}