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

//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\\\

#include <bits/stdc++.h>

#define int ll

#define F first
#define S second
#define _sz(x) (int)x.size()

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;

const int N = 5000 + 20;
int n, l[N], r[N], dp[2][N][2], ord[N];

inline bool byLength(int i, int j) { return l[i] + r[i] > l[j] + r[j]; }

int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> l[i] >> r[i];

  if (!(n & 1))
    n++;

  int k = (n - 1) / 2;

  iota(ord, ord + n, 0);
  sort(ord, ord + n, byLength);

  memset(dp, 127, sizeof dp);
  dp[0][0][0] = 0;

  for (int i = 0; i <= k; i++) {
    int nw = i & 1, nxt = !nw;

    memset(dp[nxt], 127, sizeof dp[nxt]);

    for (int j = 0; j <= k; j++) {
      for (int b = 0; b <= 1; b++) {
        int ind = ord[i + j + b], len = l[ind] + r[ind];

        if (i < k)
          dp[nxt][j][b] = min(dp[nxt][j][b], dp[nw][j][b] + r[ind] + i * len);

        if (j < k)
          dp[nw][j + 1][b] =
              min(dp[nw][j + 1][b], dp[nw][j][b] + l[ind] + j * len);

        if (b < 1)
          dp[nw][j][b + 1] = min(dp[nw][j][b + 1], dp[nw][j][b] + k * len);
      }
    }
  }

  cout << dp[k & 1][k][1] << '\\n';
}


Error: MLE

Fixed submission:

//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\\\

#include <bits/stdc++.h>

#define int ll

#define F first
#define S second
#define _sz(x) (int)x.size()

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;

const int N = 5000 + 20;
int n, l[N], r[N], dp[N][N][2], ord[N];

inline bool byLength(int i, int j) { return l[i] + r[i] > l[j] + r[j]; }

int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> l[i] >> r[i];

  if (!(n & 1))
    n++;

  int k = (n - 1) / 2;

  iota(ord, ord + n, 0);
  sort(ord, ord + n, byLength);

  memset(dp, 127, sizeof dp);
  dp[0][0][0] = 0;

  for (int i = 0; i <= k; i++) {
    for (int j = 0; j <= k; j++) {
      for (int b = 0; b <= 1; b++) {
        int ind = ord[i + j + b], len = l[ind] + r[ind];

        if (i < k)
          dp[i + 1][j][b] =
              min(dp[i + 1][j][b], dp[i][j][b] + r[ind] + i * len);

        if (j < k)
          dp[i][j + 1][b] =
              min(dp[i][j + 1][b], dp[i][j][b] + l[ind] + j * len);

        if (b < 1)
          dp[i][j][b + 1] = min(dp[i][j][b + 1], dp[i][j][b] + k * len);
      }
    }
  }

  cout << dp[k][k][1] << '\\n';
}
Problem p03873 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

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

const int MN = 5010;

int N;
pii S[MN];
int L[MN];
ll psum[MN];

bool cmp(pii a, pii b) { return a.first + a.second < b.first + b.second; }

ll dp[2][MN][2];

void prop() {
  for (int i = 0; i <= N; i++)
    for (int j = 0; j < 2; j++)
      dp[N & 1][i][j] = 1e18;
  dp[N & 1][0][0] = 0;
  for (int i = N - 1; i >= 0; i--) {
    int c = i & 1;
    int p = c ^ 1;
    for (int j = 0; j <= N; j++) {
      for (int k = 0; k < 2; k++) {
        ll &ret = dp[c][j][k];
        ret = 1e18;

        ret = min(ret,
                  dp[p][j][k] + S[i].second + 1LL * L[i] * (N - 1 - i - j - k));
        if (j)
          ret = min(ret, dp[p][j - 1][k] + S[i].first + 1LL * L[i] * (j - 1));
        if (k)
          ret = min(ret, dp[p][j][0] + 1LL * L[i] * (N / 2));
      }
    }
  }
}

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

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &S[i].first, &S[i].second);
  }

  sort(S, S + N, cmp);

  ll ans = 1e18;

  for (int i = 0; i < N; i++) {
    L[i] = S[i].first + S[i].second;
    psum[i] = L[i];
    if (i)
      psum[i] += psum[i - 1];
  }

  prop();
  for (int i = 0; i <= N; i++) {
    ans = min(ans, dp[0][i][0]);
  }

  if (N % 2) {
    ans = min(ans, dp[0][N / 2][1]);
  }

  printf("%lld", ans);
}


Error: MLE

Fixed submission:

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

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

const int MN = 5010;

int N;
pii S[MN];
int L[MN];
ll psum[MN];

bool cmp(pii a, pii b) { return a.first + a.second < b.first + b.second; }

int X;
ll cc[MN][MN][2];
ll dp(int x, int k, int t) {
  if (x == N)
    return k == 0 && t == 0 ? 0 : 1e18;
  ll &ret = cc[x][k][t];
  if (ret != 9e18)
    return ret;

  ret = 1e18;
  if (N - 1 - X - x - k >= 0)
    ret = min(ret,
              dp(x + 1, k, t) + S[x].second + 1LL * L[x] * (N - 1 - X - x - k));
  if (k)
    ret = min(ret, dp(x + 1, k - 1, t) + S[x].first + 1LL * L[x] * (k - 1));
  if (t)
    ret = min(ret, dp(x + 1, k, 0) + 1LL * L[x] * N / 2);
  return ret;
}

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

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &S[i].first, &S[i].second);
  }

  sort(S, S + N, cmp);

  ll ans = 1e18;

  for (int i = 0; i < N; i++) {
    L[i] = S[i].first + S[i].second;
    psum[i] = L[i];
    if (i)
      psum[i] += psum[i - 1];
  }

  for (int i = 0; i < MN; i++)
    for (int j = 0; j < MN; j++)
      cc[i][j][0] = 9e18;

  X = 0;
  for (int i = 0; i <= N; i++) {
    ans = min(ans, dp(0, i, 0));
  }

  if (N % 2) {
    for (int i = 0; i < MN; i++)
      for (int j = 0; j < MN; j++)
        for (int k = 0; k < 2; k++)
          cc[i][j][k] = 9e18;
    X = 1;
    ans = min(ans, dp(0, N / 2, 1));
  }

  printf("%lld", ans);
}
Problem p03874 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string solve(long long n) {
  vector<long long> v;
  long long cur = 1;
  for (int i = 1; cur * 100 <= n + 1000; i++) {
    v.push_back(cur);
    cur *= i + 7;
    cur /= i;
  }
  vector<int> c(v.size());
  for (int i = v.size() - 1; i >= 0; i--) {
    while (n >= v[i]) {
      n -= v[i];
      c[i]++;
    }
  }
  string ret;
  for (int i = 0; i < v.size(); i++) {
    ret += "FESTIVA" + string(c[i], 'L');
  }
  return ret;
}
int main() {
  long long n;
  cin >> n;
  cout << solve(n) << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string solve(long long n) {
  vector<long long> v;
  long long cur = 1;
  for (int i = 1; cur <= n; i++) {
    v.push_back(cur);
    cur *= i + 7;
    cur /= i;
  }
  vector<int> c(v.size());
  for (int i = v.size() - 1; i >= 0; i--) {
    while (n >= v[i]) {
      n -= v[i];
      c[i]++;
    }
  }
  string ret;
  for (int i = 0; i < v.size(); i++) {
    ret += "FESTIVA" + string(c[i], 'L');
  }
  return ret;
}
int main() {
  long long n;
  cin >> n;
  cout << solve(n) << "\\n";
  return 0;
}
Problem p03877 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

long long N, A, B, C, dp[5009][5009], fact[5009], factinv[5009],
    mod = 1000000007;

long long modpow(long long a, long long b, long long m) {
  long long p = 1, q = a;
  for (int i = 0; i < 30; i++) {
    if ((b / (1 << i)) % 2 == 1) {
      p *= q;
      p %= m;
    }
    q *= q;
    q %= m;
  }
  return p;
}
long long Div(long long a, long long b, long long m) {
  return (a * modpow(b, m - 2, m)) % m;
}
void init() {
  fact[0] = 1;
  for (int i = 1; i <= 5000; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  for (int i = 0; i <= 5000; i++)
    factinv[i] = Div(1, fact[i], mod);

  dp[0][0] = 1;
  for (int i = 1; i <= 5000; i++) {
    for (int j = 0; j <= 5000; j++)
      dp[i][j] += dp[i - 1][j];
    for (int j = 1; j <= 5000; j++) {
      dp[i][j] += dp[i][j - 1];
      dp[i][j] %= mod;
    }
  }
}

int main() {
  init();
  cin >> N >> A >> B >> C;
  if (B % 2 == 1) {
    cout << "0" << endl;
    return 0;
  }
  long long sum = 0;
  for (int i = 0; i <= A; i++) {
    for (int j = 0; j <= C; j++) {
      long long a = i, b = (A - i), c = j, d = B / 2, n = (C - b - 3 * c);
      if (a < 0 || b < 0 || c < 0 || d < 0 || n < 0)
        continue;
      long long X = fact[a + b + c + d];
      X *= factinv[a];
      X %= mod;
      X *= factinv[b];
      X %= mod;
      X *= factinv[c];
      X %= mod;
      X *= factinv[d];
      X %= mod;
      X *= dp[d][n];
      X %= mod;
      sum += X;
      sum %= mod;
    }
  }
  cout << sum << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

long long N, A, B, C, sum1[5009], sum2[5009][5009], ans, mod = 1000000007;
long long fact[5009], inv[5009], factinv[5009];

void init() {
  fact[0] = 1;
  for (int i = 1; i <= 5000; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  inv[1] = 1;
  for (int i = 2; i <= 5000; i++)
    inv[i] = (mod - (1LL * (mod / i) * inv[mod % i]) % mod) % mod;
  factinv[0] = 1;
  for (int i = 1; i <= 5000; i++)
    factinv[i] = (factinv[i - 1] * inv[i]) % mod;
}
long long ncr(long long n, long long r) {
  if (n < 0 || r < 0 || n < r)
    return 0;
  return (((fact[n] * factinv[n - r]) % mod) * factinv[r]) % mod;
}

int main() {
  init();
  cin >> N >> A >> B >> C;
  if (B % 2 == 1) {
    cout << "0" << endl;
    return 0;
  }

  for (int i = 0; i <= B / 2; i++) {
    for (int j = 0; j <= C; j++) {
      long long ret = factinv[i] * factinv[B / 2 - i];
      ret *= ncr(j + (i - 1), (i - 1));
      ret %= mod;
      sum1[j] += ret;
      sum1[j] %= mod;
    }
  }
  for (int i = 0; i <= C; i++) {
    for (int j = 0; j <= C; j += 3) {
      if (i + j > C || i > A)
        continue;
      long long P1 = A - i, P2 = i, P3 = j / 3;
      long long ret = factinv[P1] * factinv[P2];
      ret %= mod;
      ret *= factinv[P3];
      ret %= mod;
      sum2[P1 + P2 + P3][i + j] += ret;
      sum2[P1 + P2 + P3][j] %= mod;
    }
  }

  for (int i = 0; i <= C; i++) {
    for (int j = 0; j <= N; j++) {
      ans += ((sum1[i] * sum2[j + A + i][C - i]) % mod) * fact[j];
      ans %= mod;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03877 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define DN(en, ka) (F[en] * pot(F[ka] * F[en - ka] % Q, -1) % Q)
using namespace std;
typedef long long int ll;
const ll N = 1 << 16, Q = 1000000007;
ll n, a, b, c, res, F[N] = {1};
int pot(ll a, ll b) {
  a %= Q;
  if (b < 0)
    return pot(pot(a, -b), Q - 2);
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return ((ll)pot(a, b - 1) * a) % Q;
  ll t = pot(a, b / 2);
  return (t * t) % Q;
}
int solve(int x, int y) {
  if (b % 2 == 1 || c < y * 3 + a - x ||
      (b == 0 && ((c - a + x) % 3 != 0 || c != y * 3 + a - x)))
    return 0;
  ll t = c - a + x - 3 * y, l = F[a + y + b / 2],
     m = F[x] * F[y] % Q * F[a - x] % Q * F[b / 2] % Q,
     k = b ? DN(b / 2 + t - 1, t) : 1;
  return l * pot(m, -1) % Q * k % Q;
}
int main() {
  for (int i = 1; i < N; i++)
    F[i] = (F[i - 1] * i) % Q;
  cin >> n >> a >> b >> c;
  for (int i = 0; i <= a; i++)
    for (int j = 0; j <= c / 3; j++) {
      res += solve(i, j);
    }
  cout << res % Q << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define DN(en, ka) (F[en] * pot(F[ka] * F[en - ka] % Q, -1) % Q)
using namespace std;
typedef long long int ll;
const ll N = 1 << 16, Q = 1000000007;
ll n, a, b, c, res, F[N] = {1};
int pot(ll a, ll b) {
  a %= Q;
  if (b < 0)
    return pot(pot(a, -b), Q - 2);
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return ((ll)pot(a, b - 1) * a) % Q;
  ll t = pot(a, b / 2);
  return (t * t) % Q;
}
int solve(int x, int y) {
  ll t = c - a + x - 3 * y, l = F[a + y + b / 2],
     m = F[x] * F[y] % Q * F[a - x] % Q * F[b / 2] % Q,
     k = b ? DN(b / 2 + t - 1, t) : 1;
  return (c < y * 3 + a - x) ? 0 : (l * pot(m, -1) % Q * k % Q);
}
int main() {
  for (int i = 1; i < N; i++)
    F[i] = (F[i - 1] * i) % Q;
  cin >> n >> a >> b >> c;
  if (b % 2 == 1) {
    cout << "0\\n";
    return 0;
  }
  for (int i = 0; i <= a; i++)
    for (int j = 0; j <= c / 3; j++)
      res += solve(i, j);
  cout << res % Q << endl;
}