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

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

const int INF = 1010101010;
int dp[5001];
int nx[5001];
int min1[5001], min2[5001];

int main() {
  int n;
  scanf(" %d", &n);

  vector<int> a(n);
  for (int i = 1; i < n; ++i)
    scanf(" %d", &a[i]);

  fill(dp, dp + 5001, INF);
  dp[0] = 0;

  rep(i, n / 2 + 1) min1[i] = dp[i];
  min2[0] = INF;
  for (int i = 1; i <= n / 2; ++i)
    min2[i] = dp[i - 1];

  int ans = INF;

  for (int i = 1; i < n; ++i) {
    rep(j, n / 2 + 1) nx[j] = min(min1[j], min2[j]) + a[i];

    rep(j, n / 2 + 1) min1[j] = min(min1[j], nx[j]);
    for (int j = n / 2; j >= 1; --j)
      min2[j] = min(min2[j - 1], nx[j - 1]);

    rep(j, n / 2 + 1) dp[j] = nx[j];

    ans = min(ans, dp[n / 2]);
    if (n / 2 - n + i >= 0)
      ans = min(ans, dp[n / 2 - n + i]);
  }

  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

const int INF = 1010101010;
int dp[10001][5001];
int min1[5001], min2[5001];

int main() {
  int n;
  scanf(" %d", &n);

  vector<int> a(n);
  for (int i = 1; i < n; ++i)
    scanf(" %d", &a[i]);

  fill(dp[0], dp[10001], INF);
  dp[0][0] = 0;

  rep(i, n / 2 + 1) min1[i] = dp[0][i];

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

  for (int i = 1; i < n; ++i) {
    rep(j, n / 2 + 1) dp[i][j] = min(dp[i][j], min(min1[j], min2[j]) + a[i]);

    rep(j, n / 2 + 1) min1[j] = min(min1[j], dp[i][j]);
    for (int j = n / 2; j >= 1; --j)
      min2[j] = min(min2[j - 1], dp[i][j]);
  }

  int ans = INF;
  for (int i = 1; i < n; ++i) {
    ans = min(ans, dp[i][n / 2]);
    if (n / 2 - n + i >= 0)
      ans = min(ans, dp[i][n / 2 - n + i]);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  const int INF = 1000000000;
  int n, c;
  static int dp[2][5000 + 2][2];
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 5002; j++) {
      dp[i][j][0] = INF;
      dp[i][j][1] = INF;
    }
  dp[0][1][0] = 0;
  dp[0][0][1] = 0;
  scanf("%d", &n);
  for (int i = 2; i <= n; i++) {
    scanf("%d", &c);
    for (int j = 0; j <= n / 2; j++) {
      if (j == 0)
        dp[1][j][0] = INF;
      else
        dp[1][j][0] = min(dp[0][j - 1][0], dp[0][j - 1][1] + c);
      dp[1][j][1] = min(dp[0][j][0] + c, dp[0][j][1]);
    }
    for (int j = 0; j <= n / 2; j++) {
      dp[0][j][0] = dp[1][j][0];
      dp[0][j][1] = dp[1][j][1];
    }
  }
  printf("%d\\n", min(dp[0][n / 2][0], dp[0][n / 2][1]));
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  const int INF = 1000000000;
  int n, c;
  static int dp[10000 + 5][5000 + 2][2];
  for (int i = 0; i < 10005; i++)
    for (int j = 0; j < 5002; j++) {
      dp[i][j][0] = INF;
      dp[i][j][1] = INF;
    }
  dp[1][1][0] = 0;
  dp[1][0][1] = 0;
  scanf("%d", &n);
  for (int i = 2; i <= n; i++) {
    scanf("%d", &c);
    for (int j = 0; j <= n / 2; j++) {
      dp[i][j][0] = min(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + c);
      dp[i][j][1] = min(dp[i - 1][j][0] + c, dp[i - 1][j][1]);
    }
  }
  printf("%d\\n", min(dp[n][n / 2][0], dp[n][n / 2][1]));
}
Problem p00473 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

const int INF = 1e8;

int N;
int t[10001];

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

int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++)
    cin >> t[i];
  t[N - 1] = 0;

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 10001; j++) {
      for (int k = 0; k < 2; k++) {
        dp[i][j][k] = INF;
      }
    }
  }
  dp[0][1][0] = dp[0][0][1] = 0;

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

  cout << min(dp[(N - 1) & 1][N / 2][0], dp[(N - 1) & 1][N / 2][1]) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int N;
long long d[10001];
int a = 0, b = 0;

long long cost(int di, int prevdi, bool phase) {
  if (di >= N)
    return 0;

  long long res = LONG_MAX;
  if (phase) { // a
    for (int i = di; i < N; i++) {
      if (a + i - prevdi <= N / 2) {
        a += i - prevdi;
        res = min(res, d[di] + cost(i + 1, i, !phase));
        a -= i - prevdi;
      }
    }
  } else { // b
    for (int i = di; i < N; i++) {
      if (b + i - prevdi <= N / 2) {
        b += i - prevdi;
        res = min(res, d[di] + cost(i + 1, i, !phase));
        b -= i - prevdi;
      }
    }
  }
  return res;
}

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

  long long res = cost(0, -1, true);
  cout << res << endl;

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

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define FI first
#define SE second
using namespace std;
typedef pair<int, int> pii;
static const int inf = 1ll << 60;
static const int MAX_N = 10000;
int N;
int a[MAX_N + 5];
int dp[2][MAX_N / 2 + 5][2];
int ans = inf;

signed main() {
  cin >> N;
  for (int i = 1; i < N; ++i)
    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)
        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) % 2][j + 1][0] =
          min(dp[i % 2][j][0], dp[i % 2][j][1] + a[i + 1]);
      dp[(i + 1) % 2][j][1] = min(dp[i % 2][j][0] + a[i + 1], dp[i % 2][j][1]);
    }
    for (int j = 0; j < N / 2; ++j)
      dp[i % 2][j][0] = dp[i % 2][j][1] = inf;
  }
  cout << min(dp[N % 2][N / 2][0], dp[N % 2][N / 2][0]) << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define FI first
#define SE second
using namespace std;
typedef pair<int, int> pii;
static const int inf = 1ll << 60;
static const int MAX_N = 10000;
int N;
int a[MAX_N];
int dp[MAX_N][MAX_N / 2 + 1][2];
int ans = inf;

signed main() {
  cin >> N;
  for (int i = 1; i < N; ++i)
    cin >> a[i];
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j)
      for (int k = 0; k < 2; ++k)
        dp[i][j][k] = inf;
  dp[0][0][0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      dp[i + 1][j + 1][0] = min(dp[i][j][0], dp[i][j][1] + a[i + 1]);
      dp[i + 1][j][1] = min(dp[i][j][0] + a[i + 1], dp[i][j][1]);
    }
  }
  cout << dp[N][N / 2][0] << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded


#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

#define MAXN 10012
#define INF 1e+9

typedef struct {
  int exist;
  int none;
} dint;

dint origin[MAXN];
dint buffer[MAXN];
dint *dp = origin;
dint *dpbuf = buffer;
int field[MAXN] = {0};
int length = 0;

int SwapDim1Array(dint **a, dint **b);

int main(void) {
  scanf("%d", &length);
  for (int i = 0; i <= length; i++) {
    dp[i].exist = INF;
    dp[i].none = INF;
    dpbuf[i].exist = INF;
    dpbuf[i].none = INF;
  }
  dp[0].none = 0;
  for (int i = 1; i < length; i++) {
    scanf("%d", &field[i]);
  }

  for (int i = 0; i < length; i++) { // くばるDP
    for (int j = 0; j <= i; j++) {
      dpbuf[j].exist = INF;
      dpbuf[j].none = INF;
    }
    for (int j = 0; j <= i; j++) {
      dpbuf[j + 1].exist = min(min(dp[j].exist + field[i + 1] - field[i],
                                   dp[j].none + field[i] + field[i + 1]),
                               dpbuf[j + 1].exist);
      dpbuf[j].none = min(min(dp[j].exist, dp[j].none), dpbuf[j].none);
      // printf("(%d,%d)\\t",dp[j].exist,dp[j].none);
    } // puts("");
    SwapDim1Array(&dp, &dpbuf);
  }
  /*for(int j=0;j<=length;j++){
          printf("(%d,%d)\\t",dp[j].exist,dp[j].none);
  }puts("");*/

  int answer = 0;
  answer = min(dp[length / 2].exist, dp[length / 2].none);
  printf("%d\\n", answer);

  return 0;
}

int SwapDim1Array(dint **a, dint **b) {
  dint *c;
  c = *a;
  *a = *b;
  *b = c;
  return 0;
}

Error: MLE

Fixed submission:


#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

#define MAXN 10012
#define INF 1e+9

typedef struct {
  int exist;
  int none;
} dint;
dint dp[MAXN][MAXN];

int main(void) {
  int field[MAXN] = {0};
  int length = 0;

  // puts("###DBG");
  scanf("%d", &length);
  for (int i = 0; i <= length; i++) {
    for (int j = 0; j <= length; j++) {
      dp[i][j].exist = INF;
      dp[i][j].none = INF;
    }
  }
  dp[0][0].none = 0;
  for (int i = 1; i < length; i++) {
    scanf("%d", &field[i]);
  }

  for (int i = 0; i < length; i++) { // くばるDP
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j + 1].exist = min(min(dp[i][j].exist + field[i + 1] - field[i],
                                       dp[i][j].none + field[i] + field[i + 1]),
                                   dp[i + 1][j + 1].exist);
      dp[i + 1][j].none =
          min(min(dp[i][j].exist, dp[i][j].none), dp[i + 1][j].none);
    }
  }

  int answer = 0;
  answer = min(dp[length][length / 2].exist, dp[length][length / 2].none);
  printf("%d\\n", answer);
  /*puts("field");
  for(int i=1;i<length;i++){
          printf("%d\\t",field[i]);
  }puts("");
  printf("###%d###%d\\n",dp[1][1].exist,dp[1][1].none);*/

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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define INF (1 << 26)
int N, ans = INF;
int t[10001];
int dp[2][5001][2];

int main() {
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 5001; j++) {
      dp[i][j][true] = dp[i][j][false] = INF;
    }
  }
  cin >> N;
  for (int i = 1; i < N; i++)
    cin >> t[i];

  dp[1][1][true] = 0;
  dp[1][0][false] = 0;

  for (int i = 2; i <= N; i++) {
    int ai = i % 2, bi = 1 - ai;
    for (int j = 0; j <= N / 2; j++) {
      dp[ai][j][true] = INF;
      if (j)
        dp[ai][j][true] = min(dp[ai][j][true], dp[bi][j - 1][true]);
      if (j)
        dp[ai][j][true] = min(dp[ai][j][true], dp[bi][j - 1][false] + t[i - 1]);

      dp[ai][j][false] = INF;
      dp[ai][j][false] = min(dp[ai][j][false], dp[bi][j][false]);
      dp[ai][j][false] = min(dp[ai][j][false], dp[bi][j][true] + t[i - 1]);
    }
  }
  cout << min(dp[N % 2][N / 2][true], dp[N % 2][N / 2][false]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define INF (1 << 28)
int N;
int t[10001];
int used[5001][2];

int solve(int pos, int a, bool flg) {
  if (a == 0)
    return 0;
  if (a < 0)
    return INF;
  if (a + N - pos < N / 2)
    return INF;
  int res = INF;
  for (int i = pos + 1; i <= N; i++) {
    if (used[i - pos][flg])
      continue;
    used[i - pos][flg] = true;
    int d = solve(i, a - (i - pos) * (flg), !flg) + t[i];
    res = min(res, d);
    used[i - pos][flg] = false;
  }
  return res;
}

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_N 10000
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int dp[2][MAX_N / 2 + 1][2];
int main() {
  int n, ans = INF, t[MAX_N + 1] = {};
  cin >> n;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n / 2 + 1; j++) {
      fill(dp[i][j], dp[i][j] + 2, INF);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> t[i];
  }
  dp[1][1][1] = t[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n / 2 + 1; j++) {
      for (int k = 0; k < 2; k++) {
        dp[0][j][k] = dp[1][j][k];
        dp[1][j][k] = INF;
      }
    }
    for (int j = 0; j < n / 2; j++) {
      if (j > i)
        break;
      dp[1][j + 1][0] = min(dp[0][j + 1][1], dp[0][j + 1][0]);

      dp[1][j + 1][1] = min(dp[1][j + 1][1], dp[0][j][1] + t[i] - t[i - 1]);
      dp[1][j + 1][1] = min(dp[1][j + 1][1], dp[0][j][0] + t[i] + t[i - 1]);
    }
    ans = min(ans, min(dp[1][n / 2][0], dp[1][n / 2][1]));
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_N 10000
// #define INF 1<<30
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int dp[MAX_N][MAX_N / 2 + 1][2];
int main() {
  int n, ans = INF, t[MAX_N + 1] = {};
  cin >> n;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n / 2 + 1; j++) {
      fill(dp[i][j], dp[i][j] + 2, INF);
    }
    dp[i][0][0] = 0;
  }
  for (int i = 1; i < n; i++) {
    cin >> t[i];
  }
  dp[1][1][1] = t[1];
  for (int i = 2; i < n; i++) {
    for (int j = 1; j < i; j++) {
      if (j <= n / 2) {
        dp[i][j][0] = min(dp[i - 1][j][1], dp[i - 1][j][0]);
        dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][1] + t[i] - t[i - 1]);
        dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][0] + t[i] + t[i - 1]);
        if (j == n / 2) {
          ans = min(min(dp[i][j][1], dp[i][j][0]), ans);
        }
      }
    }
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define INF 1145141919

int N;
int T[10010];
int dp[2][10010][2];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> T[i];

  for (int j = 0; j <= N; j++) {
    for (int k = 0; k < 2; k++) {
      dp[0][j][k] = INF;
    }
  }

  dp[0][0][0] = 0;
  dp[0][0][1] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      for (int k = 0; k < 2; k++) {
        dp[(i + 1) % 2][j][k] = INF;
      }
    }
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < 2; k++) {
        int nj = k == 0 ? j + 1 : j;
        if (dp[i % 2][j][k] == INF)
          continue;
        dp[(i + 1) % 2][nj][k] = min(dp[(i + 1) % 2][nj][k], dp[i % 2][j][k]);
        dp[(i + 1) % 2][nj][1 - k] =
            min(dp[(i + 1) % 2][nj][1 - k], dp[i % 2][j][k] + T[i]);
      }
    }
  }
  cout << min(dp[N % 2][N / 2][0], dp[N % 2][N / 2][1]) << "\\n";
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define INF 1145141919

int N;
int T[10010];
int memo[10010][10010];

int f(int x, int t) {
  if (x + t > N)
    return INF;
  if (x >= N - 1)
    return 0;

  t = min(t, N - (x + t));
  if (memo[x][t] >= 0)
    return memo[x][t];
  if (x == N - 2) {
    int ret = 0;
    if (t == 1)
      ret = T[N - 2];
    return memo[x][t] = ret;
  }

  int ret = INF;
  if (t == 0) {
    ret = 0;
  } else {
    for (int i = x + 1; i <= N - 1; i++) {
      if (t >= (i - x)) {
        ret = min(ret, T[i - 1] + f(i, t - (i - x)));
      }
      ret = min(ret, T[i - 1] + f(i, t));
    }
  }
  return memo[x][t] = ret;
}

int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++)
    cin >> T[i];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      memo[i][j] = -1;
    }
  }
  cout << f(0, N / 2) << "\\n";
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int dp[2][10001][2];
int cost[10000];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n - 1) scanf("%d", &cost[i]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; i++) {
    int t = i & 1;
    rep(j, i + 1) rep(k, 2) dp[t][j][k] = INF;
    rep(j, i + 1) {
      if (i >= 2) {
        if (j)
          dp[t][j][0] = dp[!t][j - 1][1] + cost[i - 2];
        dp[t][j][1] = dp[!t][j][0] + cost[i - 2];
      }
      if (j)
        dp[t][j][0] = min(dp[t][j][0], dp[!t][j - 1][0]);
      dp[t][j][1] = min(dp[t][j][1], dp[!t][j][1]);
    }
  }
  printf("%d\\n", min(dp[n & 1][n / 2][0], dp[n & 1][n / 2][1]));
}

Error: MLE

Fixed submission:

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

int dp[10001][10001][2];
int cost[10000];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n - 1) scanf("%d", &cost[i]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; i++)
    rep(j, i + 1) {
      if (i >= 2) {
        if (j)
          dp[i][j][0] = dp[i - 1][j - 1][1] + cost[i - 2];
        dp[i][j][1] = dp[i - 1][j][0] + cost[i - 2];
      }
      if (j)
        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]);
    }
  printf("%d\\n", min(dp[n][n / 2][0], dp[n][n / 2][1]));
}
Problem p00473 (C++) — buggy submission, verdict: Time Limit Exceeded

// 58
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int dp[2][10001][2];
  fill(dp[0][0], dp[2][0], 1 << 30);
  dp[0][1][0] = dp[0][0][1] = 0;
  for (int i = 0; i < n - 1; i++) {
    int cc;
    cin >> cc;
    for (int j = 0; j < n - 2; j++) {
      dp[!(i & 1)][j + 1][0] = min(dp[i & 1][j][0], dp[i & 1][j][1] + cc);
      dp[!(i & 1)][j][1] = min(dp[i & 1][j][1], dp[i & 1][j][0] + cc);
    }
  }
  cout << min(dp[(n - 1) & 1][n / 2][0], dp[(n - 1) & 1][n / 2][1]) << endl;
  return 0;
}


Error: TLE

Fixed submission:

// 47
#include <iostream>

using namespace std;

int n;
int t[9999];
int mem[2000][2000];

int dfs(int s, int d) {
  if (s == n) {
    return 0;
  } else if (n - s < 2000 && d < 2000 && mem[n - s][d]) {
    return mem[n - s][d];
  } else {
    int m = 1 << 30;
    for (int i = 1; s + i <= n && i <= d; i++) {
      m = min(m, t[s + i - 1] + dfs(s + i, n - s - d));
    }
    if (n - s < 2000 && d < 2000) {
      mem[n - s][d] = m;
    }
    return m;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> t[i];
  }
  cout << dfs(0, n / 2) << endl;
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;
int dp[2][5010][2];
#define MAX 100000000
int main(void) {
  int n;
  scanf("%d", &n);
  int i, j;
  int a[10001];
  for (i = 0; i < n - 1; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 0; i < 2; i++) {
    for (j = 0; j <= 5001; j++) {
      dp[i][j][0] = MAX;
      dp[i][j][1] = MAX;
    }
  }
  dp[0][0][0] = 0; // スタート地点
  for (int i = 0; i < n; i++) {
    int m = min(5010, i);
    for (int j = 0; j <= m; j++) {
      dp[(i + 1) & 1][j + 1][0] = min(dp[i & 1][j][0], dp[i & 1][j][1] + a[i]);
      dp[(i + 1) & 1][j][1] = min(dp[i & 1][j][1], dp[i & 1][j][0] + a[i]);
    }
    dp[i & 1][0][0] = MAX;
  }
  int min = MAX;
  for (i = 0; i < 2; i++) {
    if (dp[0][n / 2][i] < min) {
      min = dp[0][n / 2][i];
    }
  }
  printf("%d\\n", dp[n % 2][n / 2][0]);

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int dp[2][5001][2];
#define MAX 100000000
int main(void) {
  int n;
  scanf("%d", &n);
  int i, j;
  for (i = 0; i < 2; i++) {
    for (j = 0; j <= n / 2; j++) {
      dp[i][j][0] = MAX;
      dp[i][j][1] = MAX;
    }
  }
  dp[0][0][0] = 0; // スタート地点
  for (i = 1; i <= n - 1; i++) {
    int a;
    scanf("%d", &a);
    for (j = 0; j <= n / 2; j++) {
      if (dp[1][j + 1][0] >
          dp[0][j][0] + a) { // ［0］［j］［0］とaの時間をdp[1][j+1][0]に代入
        dp[1][j + 1][0] = dp[0][j][0] + a;
      }
      if (dp[1][j][1] >
          dp[0][j][0] + a) { //[0][j][0]とaの時間をdp[1][j][1]に代入
        dp[1][j][1] = dp[0][j][0] + a;
      }
      if (dp[1][j][1] >
          dp[0][j]
            [1]) { //[0][j][1]と[1][j][1]は同じ位置、だからきる必要なしなので[0][j][1]はいらない
        dp[1][j][1] = a;
      }
      if (dp[1][j + 1][0] > dp[0][j][1] + a) { //[1][j+1][0]に[0][j][1]+a   代入
        dp[1][j + 1][0] = dp[0][j][1] + a;
      }
      int temp, b;
      temp = i;
      b = temp;
      if (temp > n / 2) {
        temp = n - temp;
        b = i;
      }
      if (dp[1][j + temp][0] > dp[0][j][0] + a && j + temp <= n / 2 &&
          j + temp <= b) {
        // printf("j.temp=%d %d\\n",j,temp);
        dp[1][j + temp][0] = dp[0][j][0] + a;
      }
    }
    for (j = 0; j <= n / 2; j++) { // 0桁に戻す作業
      if (dp[0][j][0] > dp[1][j][0]) {
        dp[0][j][0] = dp[1][j][0];
      }
      if (dp[0][j][1] > dp[1][j][1]) {
        dp[0][j][1] = dp[1][j][1];
      }
    }
    /*for(j=0;j<=n/2;j++){
            printf("%d,%d   ",dp[0][j][0],dp[0][j][1]);
    }
    printf("\\n");
    printf("\\n");*/
  }
  int min = MAX;
  for (i = 0; i < 2; i++) {
    if (dp[0][n / 2][i] < min) {
      min = dp[0][n / 2][i];
    }
  }
  printf("%d\\n", min);
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define INF 0x33433433
#define B 5001

int n;
int ans;
int dp /*[10001]*/[10001];
int t[10001];
int mdp[30001];

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

  for (int i = 0; i <= n; i++) {
    dp[i] = INF;
    mdp[i] = INF;
  }

  dp[0] = 0;
  mdp[0] = 0;
  ans = INF;
  for (int i = 1; i <= n - 1; i++) {
    for (int j = 0; j <= i; j++) {
      dp[j] = mdp[i - j] + t[i - 1];
      mdp[i - j] = min(mdp[i - j], dp[j]);
    }

    dp[0] = t[i - 1];
    mdp[i] = t[i - 1];
    for (int j = 0; j <= i; j++) {
      dp[j] = min(dp[j], dp[i - j]);
      dp[i - j] = dp[j];
      mdp[i - j] = min(mdp[i - j], dp[j]);
      mdp[j] = min(mdp[j], dp[i - j]);
    }
    /*
            for (int j=0; j<=n; j++) {
                printf("%d ", dp[j]);
            }puts("");*/
    ans = min(ans, dp[n / 2]);
  }

  printf("%d\\n", ans);
}

Error: MLE

Fixed submission:

/*
    低知能なので制約を読まない
 */

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

#define INF 0x33433433
#define B 2000

int n;
int ans;
int dp[10001][10001];
int t[10001];
int mdp[10001];

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

  for (int i = 0; i <= n; i++) {
    mdp[i] = INF;
    for (int j = 0; j <= n; j++) {
      dp[i][j] = INF;
    }
  }

  dp[0][0] = 0;
  mdp[0] = 0;
  for (int i = 1; i <= n - 1; i++) {
    // mdp[i] = 0;
    for (int j = 0; j <= i; j++) {
      dp[i][j] = min(dp[i][j], mdp[i - j] + t[i - 1]);
      mdp[i - j] = min(mdp[i - j], dp[i][j]);
    }
    dp[i][0] = t[i - 1];
    mdp[i] = t[i - 1];
  }
  /*
      for (int i=0; i<=n-1; i++) {
          for (int j=0; j<=n; j++) {
              printf("%d ", dp[i][j]);
          }
          puts("");
      }
  */
  ans = INF;
  for (int i = 0; i <= n - 1; i++) {
    ans = min(ans, dp[i][n / 2]);
  }

  printf("%d\\n", ans);
}