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

#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

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

int main() {
  int n;
  cin >> n;
  FOR(i, 1, n) cin >> t[i];
  rep(i, 2) rep(j, n / 2 + 1) rep(k, 2) dp[i][j][k] = 1 << 29;
  dp[0][0][0] = dp[0][0][1] = 0;

  rep(i, n) {
    rep(j, n / 2) rep(k, 2) rep(l, 2) {
      int x = k == 1;
      dp[i + 1 & 1][j + x][k] =
          min(dp[i + 1 & 1][j + x][k], dp[i & 1][j][l] + t[i] * (k != l));
    }
    rep(j, n / 2 + 1) rep(k, 2) dp[i & 1][j][k] = 1 << 29;
  }
  cout << min(dp[0][n / 2][0], dp[0][n / 2][1]) << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

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

int main() {
  int n;
  cin >> n;
  FOR(i, 1, n) cin >> t[i];
  rep(i, n + 1) rep(j, n / 2 + 1) rep(k, 2) dp[i][j][k] = 1 << 30;
  dp[0][0][0] = dp[0][0][1] = 0;

  rep(i, n) rep(j, n / 2) rep(k, 2) rep(l, 2) {
    int x = k == 1;
    dp[i + 1][j + x][k] =
        min(dp[i + 1][j + x][k], dp[i][j][l] + t[i] * (k != l));
  }
  cout << min(dp[n][n / 2][0], dp[n][n / 2][1]) << endl;

  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Runtime Error

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

int n;
int t[10001];
int dp[2][10001];
int INF = 1e9 + 7;

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

Error: 0

Fixed submission:

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

int n;
int t[10001];
int dp[2][10001];
int INF = 1e9 + 7;

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++)
    scanf("%d", &t[i]);
  dp[0][0] = INF;
  dp[1][0] = INF;
  for (int i = n - 1; i > 0; --i)
    for (int j = 1; j < n + 1; ++j)
      dp[i % 2][j] =
          min(dp[(i + 1) % 2][j - 1], t[i] + dp[(i + 1) % 2][n - i - j + 1]);
  printf("%d\\n", dp[1][n / 2]);
}
Problem p00473 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
ll n;
ll dummy[2][5001][2]; //! j???i???????????????????????´j???????°??????????
ll a[10000];
const ll INF = 0xffffffffff;

int main() {
  cin >> n;
  rep1(i, n - 1) cin >> a[i];
  fill(dummy[0][0], dummy[1][5001], INF);
  ll(*dp1)[2] = dummy[0];
  ll(*dp2)[2] = dummy[1];
  dp1[0][0] = 0;
  dp1[0][1] = 0;
  rep(i, n - 1) {
    rep(j, n / 2 + 1) rep(k, 2) dp2[j][k] = INF;
    rep(j, min(n / 2, i) + 1) {
      rep(k, 2) {
        dp2[j][k] = min(dp2[j][k], dp1[j][k]); // k????????????
        if (i + 1 - j <= (n / 2))
          dp2[i + 1 - j][!k] =
              min(dp2[i + 1 - j][!k], dp1[j][k] + a[i + 1]); //! k????????????
      }
    }
    swap(dp1, dp2);
  }
  cout << min(dp1[n / 2][0], dp2[n / 2][1]) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
ll n;
ll dummy[2][5001][2]; //! j???i???????????????????????´j???????°??????????
ll a[10002];
const ll INF = 0xffffffffff;

int main() {
  cin >> n;
  rep1(i, n - 1) cin >> a[i];
  // fill(dummy[0][0],dummy[1][5001],INF);
  rep(i, 2) rep(j, 5001) rep(k, 2) dummy[i][j][k] = INF;
  ll(*dp1)[2] = dummy[0];
  ll(*dp2)[2] = dummy[1];
  dp1[0][0] = 0;
  dp1[0][1] = 0;
  rep(i, n - 1) {
    rep(j, n / 2 + 1) rep(k, 2) dp2[j][k] = INF;
    rep(j, min(n / 2, i) + 1) {
      rep(k, 2) {
        dp2[j][k] = min(dp2[j][k], dp1[j][k]); // k????????????
        dp2[i + 1 - j][!k] =
            min(dp2[i + 1 - j][!k], dp1[j][k] + a[i + 1]); //! k????????????
      }
    }
    swap(dp1, dp2);
  }
  cout << min(dp1[n / 2][0], dp2[n / 2][1]) << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;

int dp[2][5050][2];
int main(void) {
  int N;
  int t[10010];
  cin >> N;
  REP(i, N - 1) { cin >> t[i + 1]; }
  REP(i, 2) REP(j, 5050) REP(k, 2) dp[i][j][k] = INF;
  dp[0][0][0] = 0;
  REP(i, N) {
    REP(j, min(i, N / 2) + 1) {
      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][1], dp[i & 1][j][0] + t[i]);
    }
    dp[(i + 1) & 1][0][0] = INF;
  }
  int res = min(dp[N & 1][N / 2][0], dp[N & 1][N / 2][1]);
  cout << res << endl;
}

Error: MLE

Fixed submission:

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

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;

int dp[10010][10010][2];
int main(void) {
  int N;
  int t[10010];
  cin >> N;
  REP(i, N - 1) { cin >> t[i + 1]; }
  t[N] = 0;
  t[0] = 0;
  REP(i, 10010) REP(j, 10010) REP(k, 2) dp[i][j][k] = INF;
  FOR(i, 1, N + 1) { dp[i][i][0] = t[i]; }
  FOR(i, 1, N + 1) {
    REP(j, i + 1) {
      FOR(k, 1, j) { dp[i][j][0] = min(dp[i][j][0], dp[i - k][k][1] + t[i]); }
      REP(k, i) { dp[i][j][1] = min(dp[i][j][1], dp[k][j][0] + t[i]); }
    }
  }
  int res = INF;
  FOR(i, N / 2, N) {
    res = min(res, dp[i][N / 2][0]);
    res = min(res, dp[i][i - N / 2][1]);
  }
  cout << res << endl;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

constexpr int32_t N_MAX = 10000;
constexpr int32_t INF = 10000 * N_MAX + 9999;

int32_t N;
int32_t t[N_MAX];
int32_t dp[2][N_MAX + 1][2]; // 0:a
int main() {
  std::cin >> N;
  for (int i = 1; i < N; ++i) {
    std::cin >> t[i];
  }

  uint8_t mode = 0;
  for (int n = 1; n <= N; ++n) {
    auto last_mode = mode;
    mode ^= 1;
    dp[mode][0][0] = INF;
    dp[mode][0][1] = 0;
    dp[mode][n][0] = 0;
    dp[mode][n][1] = INF;
    for (int a = 1; a < n; ++a) {
      dp[mode][a][0] =
          std::min(dp[last_mode][a - 1][0], dp[last_mode][a - 1][1] + t[n - 1]);
      dp[mode][a][1] =
          std::min(dp[last_mode][a][0] + t[n - 1], dp[last_mode][a][1]);
    }
  }
#if 0
std::cout<<"------------0-----------------\\n";
	for(int n = 1;n <= N;++n){
	for(int a = 0;a <= n;++a){
		std::cout << dp[n][a][0] << ' ';
	}std::cout<<'\\n';}
std::cout<<"------------1-----------------\\n";
	for(int n = 1;n <= N;++n){
	for(int a = 0;a <= n;++a){
		std::cout << dp[n][a][1] << ' ';
	}std::cout<<'\\n';}
#endif
  std::cout << std::min(dp[0][N / 2][0], dp[0][N / 2][1]) << std::endl;
}

Error: MLE

Fixed submission:

#include <iostream>

constexpr int32_t N_MAX = 10000;
constexpr int32_t INF = 10000 * N_MAX + 9999;

int32_t N;
int32_t t[N_MAX];
int32_t dp[N_MAX + 1][N_MAX + 1][2]; // 0:a
int main() {
  std::cin >> N;
  for (int i = 1; i < N; ++i) {
    std::cin >> t[i];
  }

  uint8_t mode = 0;
  for (int n = 1; n <= N; ++n) {
    dp[n][0][0] = INF;
    dp[n][0][1] = 0;
    dp[n][n][0] = 0;
    dp[n][n][1] = INF;
    for (int a = 1; a < n; ++a) {
      auto last_mode = mode;
      mode ^= 1;
      dp[n][a][0] =
          std::min(dp[n - 1][a - 1][0], dp[n - 1][a - 1][1] + t[n - 1]);
      dp[n][a][1] = std::min(dp[n - 1][a][0] + t[n - 1], dp[n - 1][a][1]);
    }
  }
#if 0
std::cout<<"------------0-----------------\\n";
	for(int n = 1;n <= N;++n){
	for(int a = 0;a <= n;++a){
		std::cout << dp[n][a][0] << ' ';
	}std::cout<<'\\n';}
std::cout<<"------------1-----------------\\n";
	for(int n = 1;n <= N;++n){
	for(int a = 0;a <= n;++a){
		std::cout << dp[n][a][1] << ' ';
	}std::cout<<'\\n';}
#endif
  std::cout << std::min(dp[N][N / 2][0], dp[N][N / 2][1]) << std::endl;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int n;
int t[10002];

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

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

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

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

  for (int i = n; i > 0; i--) {
    for (int j = 0; j < 5000; j++) {
      dp[i % 2][j + 1][0] =
          min(dp[(i + 1) % 2][j][0], dp[(i + 1) % 2][j][1] + t[i]);
      dp[i % 2][j][1] =
          min(dp[(i + 1) % 2][j][1], dp[(i + 1) % 2][j][0] + t[i]);
      dp[(i + 1) % 2][j][0] = 1 << 25;
      dp[(i + 1) % 2][j][1] = 1 << 25;
    }
  }
  printf("%d\\n", dp[1][n / 2][0]);
}

Error: MLE

Fixed submission:

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

int n;
int t[10002];

int dp[5001][10002];
int solve(int a, int l) {
  if (2 * a > n)
    return 1 << 25;
  if (l == n && 2 * a == n)
    return 0;
  if (dp[a][l])
    return dp[a][l];

  int ret = 1 << 25;
  for (int i = 1; i + l <= n; i++) {
    ret = min(ret, solve(a, l + i) + t[l + i]);
    ret = min(ret, solve(a + i, l + i) + t[l + i]);
  }
  return dp[a][l] = ret;
}

int main(void) {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d", t + i);
  }
  printf("%d\\n", solve(0, 0));
}
Problem p00473 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

const int MAX_N = 10000;

const int INF = 1 << 29;
int T[MAX_N + 10];
int dp[2][2][MAX_N / 2 + 10];

int main(void) {
  int n;
  cin >> n;
  int half = n / 2;
  for (int i = 0; i + 1 < n; ++i) {
    cin >> T[i + 1];
  }
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k <= half; ++k) {
        dp[i][j][k] = INF;
      }
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    int ii = i & 1, ij = (i + 1) & 1;
    for (int k = 0; k <= half; ++k) {
      dp[ii][0][k] = min(dp[ij][0][k], dp[ij][1][k]);
      if (k > 0) {
        dp[ii][1][k] = min(dp[ij][0][k - 1] + T[i - 1] + T[i],
                           dp[ij][1][k - 1] - T[i - 1] + T[i]);
      }
    }
  }
  cout << min(dp[0][0][half], dp[0][1][half]) << endl;
  //		cout << min(dp[1][0][half], dp[1][1][half]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

const int MAX_N = 10000;

int N;
int T[MAX_N + 1];
#define mp(x, y) make_pair(x, y)
typedef pair<int, int> PII;
typedef pair<bool, PII> PBII;
map<PBII, int> M;
int g(bool isA, int na, int nb) {
  if (na + nb == 0) {
    return 0;
  }
  int ret = (1 << 29);
  PBII key = PBII(isA, mp(na, nb));
  if (nb <= 2000 && M.count(key)) {
    return M[key];
  }
  if (isA) {
    if (na) {
      ret = min(ret, g(true, na - 1, nb));
    }
    if (nb) {
      ret = min(ret, g(false, na, nb - 1) + T[na + nb - 1]);
    }
  } else {
    if (na) {
      ret = min(ret, g(true, na - 1, nb) + T[na + nb - 1]);
    }
    if (nb) {
      ret = min(ret, g(false, na, nb - 1));
    }
  }
  if (nb <= 2000) {
    M[key] = ret;
  }
  return ret;
}

int main(void) {
  while (cin >> N) {
    for (int i = 0; i + 1 < N; ++i) {
      cin >> T[i];
    }
    M.clear();
    cout << g(true, N / 2 - 1, N / 2) << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define MAX 10001
#define ll long long
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
const int inf = 1e9;
const ll INF = 1e18;
#define B 100

int a[MAX], dp[2][MAX][2];
int main() {
  int n, t1, t2;
  scanf("%d", &n);
  rep(i, 2) rep(j, MAX) rep(k, 2) dp[i][j][k] = inf;
  for (int i = 2; i <= n; i++)
    scanf("%d", &a[i]);
  dp[0][0][0] = dp[0][0][1] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i % 2][j][0] =
          min(dp[(i - 1) % 2][j - 1][0], dp[(i - 1) % 2][j - 1][1] + a[i]);
      dp[i % 2][j][1] =
          min(dp[(i - 1) % 2][j][1], dp[(i - 1) % 2][j][0] + a[i]);
    }
    rep(j, n) rep(k, 2) dp[(i - 1) % 2][j][k] = inf;
  }
  /*for(int i = 0;i <= n;i++){
          for(int j = 0;j <= n;j++)printf("%d ", dp[i][j][1]);
          printf("\\n");
  }*/
  printf("%d\\n", min(dp[n % 2][n / 2][0], dp[n % 2][n / 2][1]));
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define MAX 10001
#define ll long long
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
const int inf = 1e9;
const ll INF = 1e18;
#define B 100

int a[MAX], dp[MAX][MAX][2];
int main() {
  int n, t1, t2;
  scanf("%d", &n);
  rep(i, MAX) rep(j, MAX) rep(k, 2) dp[i][j][k] = inf;
  for (int i = 2; i <= n; i++)
    scanf("%d", &a[i]);
  dp[0][0][0] = dp[0][0][1] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j][0] = min(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + a[i]);
      dp[i][j][1] = min(dp[i - 1][j][1], dp[i - 1][j][0] + a[i]);
    }
  }
  /*for(int i = 0;i <= n;i++){
          for(int j = 0;j <= n;j++)printf("%d ", dp[i][j][1]);
          printf("\\n");
  }*/
  printf("%d\\n", min(dp[n][n / 2][0], dp[n][n / 2][1]));
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, t[10000];
  cin >> n;
  rep(i, 0, n - 1) cin >> t[i];

  static int dp[2][2][6000];
  fill_n((int *)dp, 2 * 2 * 6000, INF);
  dp[0][0][1] = dp[0][1][0] = 0;
  rep(i, 0, n - 1) {
    rep(j, 0, n / 2 + 100) {
      if (j - 1 >= 0)
        dp[1][0][j] = min(dp[0][0][j - 1], dp[0][1][j - 1] + t[i]);
      dp[1][1][j] = min(dp[0][1][j], dp[0][0][j] + t[i]);

      dump(j);
      dump(dp[1][0][j]);
      dump(dp[1][1][j]);
    }
    swap(dp[0], dp[1]);
  }

  cout << min(dp[0][0][n / 2], dp[0][1][n / 2]) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, t[10000];
  cin >> n;
  rep(i, 0, n - 1) cin >> t[i];

  static int dp[2][2][6000];
  fill_n((int *)dp, 2 * 2 * 6000, INF);
  dp[0][0][1] = dp[0][1][0] = 0;
  rep(i, 0, n - 1) {
    rep(j, 0, i + 2) {
      if (j - 1 >= 0)
        dp[1][0][j] = min(dp[0][0][j - 1], dp[0][1][j - 1] + t[i]);
      dp[1][1][j] = min(dp[0][1][j], dp[0][0][j] + t[i]);

      dump(j);
      dump(dp[1][0][j]);
      dump(dp[1][1][j]);
    }
    swap(dp[0], dp[1]);
  }

  cout << min(dp[0][0][n / 2], dp[0][1][n / 2]) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#define min(a, b) ((a) <= (b) ? (a) : (b))
#define rep(i, n) for (int i = 0; i < n; i++)
int dp[2][5002][2], cost[9999];
int main() {
  int N;
  scanf("%d", &N);
  rep(i, N - 1) scanf("%d", &cost[i]);
  memset(dp, 0x7f, sizeof(dp));
  dp[0][1][0] = dp[0][0][1] = 0;
  rep(i, N - 1) {
    int m = min(i + 2, N / 2);
    rep(j, m + 1) {
      dp[(i + 1) & 1][j + 1][0] =
          min(dp[i & 1][j][0], dp[i & 1][j][1] + cost[i]);
      dp[(i + 1) & 1][j][1] = min(dp[i & 1][j][0] + cost[i], dp[i & 1][j][1]);
    }
    memset(dp + (i & 1), 0x7f, 5001 * 2);
  }
  printf("%d\\n", min(dp[1][N / 2][0], dp[1][N / 2][1]));
}

Error: MLE

Fixed submission:

#include <stdio.h>

int memo[2][5001][10000];

int N;
int map[10000 - 1];

int tansaku(int pos, int rest, int status) {
  int result = 0x3fffffff;
  int now_result;
  if (pos >= N - 1)
    return (rest - status == 0) ? 0 : 0x3fffffff;
  if (rest < 0)
    return 0x3fffffff;
  if (memo[status][rest][pos] > 0)
    return memo[status][rest][pos] - 1;
  if (status) {
    now_result = tansaku(pos + 1, rest - 1, 1);
    if (now_result < result)
      result = now_result;
    now_result = tansaku(pos + 1, rest - 1, 0) + map[pos];
    if (now_result < result)
      result = now_result;
  } else {
    now_result = tansaku(pos + 1, rest, 0);
    if (now_result < result)
      result = now_result;
    now_result = tansaku(pos + 1, rest, 1) + map[pos];
    if (now_result < result)
      result = now_result;
  }
  memo[status][rest][pos] = result + 1;
  return result;
}

int main(void) {
  int i;
  int result;
  scanf("%d", &N);
  for (i = 0; i < N - 1; i++)
    scanf("%d", &map[i]);
  result = tansaku(0, N / 2, 0);
  printf("%d\\n", result);
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Runtime Error

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

int a[20000];
int dp[2][10001][3];
// 切った: A: B

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

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

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j <= 10000; 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 <= i; j++) {
      for (int k = 0; k <= 2; k++) {
        dp[(i + 1) % 2][j][k] = INF;
      }
    }

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

      dp[(i + 1) % 2][j + 1][1] =
          min(dp[(i + 1) % 2][j + 1][1], dp[i % 2][j][0]);
      dp[(i + 1) % 2][j + 1][1] =
          min(dp[(i + 1) % 2][j + 1][1], dp[i % 2][j][1]);

      dp[(i + 1) % 2][j][2] = min(dp[(i + 1) % 2][j][2], dp[i % 2][j][0]);
      dp[(i + 1) % 2][j][2] = min(dp[(i + 1) % 2][j][2], dp[i % 2][j][2]);
    }
  }

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

  return 0;
}

Error: 0

Fixed submission:

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

int a[10000];
int dp[2][5002][3];
// 切った: A: B

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

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

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j <= 5000; 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 <= i; j++) {
      for (int k = 0; k <= 2; k++) {
        dp[(i + 1) % 2][j][k] = INF;
      }
    }

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

      dp[(i + 1) % 2][j + 1][1] =
          min(dp[(i + 1) % 2][j + 1][1], dp[i % 2][j][0]);
      dp[(i + 1) % 2][j + 1][1] =
          min(dp[(i + 1) % 2][j + 1][1], dp[i % 2][j][1]);

      dp[(i + 1) % 2][j][2] = min(dp[(i + 1) % 2][j][2], dp[i % 2][j][0]);
      dp[(i + 1) % 2][j][2] = min(dp[(i + 1) % 2][j][2], dp[i % 2][j][2]);
    }
  }

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

  return 0;
}