prithivMLmods/Coder-Stat huggingface dataset:
Problem p03868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int mod = 1000 * 1000 * 1000 + 7;

int N;
vector<int> A, B;

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

  A.resize(N);
  B.resize(N);

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

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  int ans = 1;

  for (int i = N - 1; i >= 0; i--) {
    int s = i + 1, e = N - 1;
    int p = i;

    while (s <= e) {
      int m = (s + e) >> 1;

      if (A[m] <= B[i]) {
        p = m;
        s = m + 1;
      } else
        e = m - 1;
    }
    s = i + 1, e = N - 1;
    int q = i;
    while (s <= e) {
      int m = (s + e) >> 1;

      if (B[m] <= A[i]) {
        q = m;
        s = m + 1;
      } else
        e = m + 1;
    }

    ans = 1LL * ans * (max(p, q) - i + 1) % mod;
  }

  cout << ans;
}


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

const int mod = 1000 * 1000 * 1000 + 7;

int N;
vector<int> A, B;

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

  A.resize(N);
  B.resize(N);

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

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  int ans = 1;

  for (int i = N - 1; i >= 0; i--) {
    int s = i + 1, e = N - 1;
    int p = i;

    while (s <= e) {
      int m = (s + e) >> 1;

      if (A[m] <= B[i]) {
        p = m;
        s = m + 1;
      } else
        e = m - 1;
    }
    s = i + 1, e = N - 1;
    int q = i;
    while (s <= e) {
      int m = (s + e) >> 1;

      if (B[m] <= A[i]) {
        q = m;
        s = m + 1;
      } else
        e = m - 1;
    }

    ans = 1LL * ans * (max(p, q) - i + 1) % mod;
  }

  cout << ans;
}
Problem p03870 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof a)
#define mcpy(a, b) memcpy(a, b, sizeof b)
#define lb(x) ((x) & (-(x)))
#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define dalao 1000000007
#define inf 0x3f3f3f3f
#define N 305
using namespace std;
typedef long long ll;
int n, a[N], s, ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), s ^= a[i];
  for (int i = 29; ~i; i--)
    if (s >> i & 1)
      for (int j = 1; j <= n; j++)
        if (lb(a[j]) == 1 << i) {
          ans++, s ^= (1 << i + 1) - 1;
          break;
        }
  if (s)
    puts("-1");
  else
    cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof a)
#define mcpy(a, b) memcpy(a, b, sizeof b)
#define lb(x) ((x) & (-(x)))
#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define dalao 1000000007
#define inf 0x3f3f3f3f
#define N 100010
using namespace std;
typedef long long ll;
int n, a[N], s, ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), s ^= a[i];
  for (int i = 29; ~i; i--)
    if (s >> i & 1)
      for (int j = 1; j <= n; j++)
        if (lb(a[j]) == 1 << i) {
          ans++, s ^= (1 << i + 1) - 1;
          break;
        }
  if (s)
    puts("-1");
  else
    cout << ans;
  return 0;
}
Problem p03874 (C++)

Buggy submission (status: 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 <= 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;
}

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 * 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;
}
Problem p03878 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n;
pair<int, int> p[100005];
long long MOD = 1000000007;

int dir(long long a) {
  if (a > 0)
    return 1;
  else if (a < 0)
    return -1;
  else
    return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int pp;
    cin >> pp;
    p[i] = make_pair(pp, 1);
  }
  for (int i = 0; i < n; i++) {
    int pp;
    cin >> pp;
    p[i + n] = make_pair(pp, -1);
  }
  sort(p, p + 2 * n);

  long long ans = 1;
  long long tmp = 0;

  for (int i = 0; i < 2 * n; i++) {
    if (tmp == 0)
      tmp += p[i].second;
    else {
      if (dir(tmp) == dir(p[i].second))
        tmp += p[i].second;
      else {
        ans *= abs(tmp);
        ans %= MOD;
        tmp += p[i].second;
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n;
pair<int, int> p[200005];
long long MOD = 1000000007;

int dir(long long a) {
  if (a > 0)
    return 1;
  else if (a < 0)
    return -1;
  else
    return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int pp;
    cin >> pp;
    p[i] = make_pair(pp, 1);
  }
  for (int i = 0; i < n; i++) {
    int pp;
    cin >> pp;
    p[i + n] = make_pair(pp, -1);
  }
  sort(p, p + 2 * n);

  long long ans = 1;
  long long tmp = 0;

  for (int i = 0; i < 2 * n; i++) {
    if (tmp == 0)
      tmp += p[i].second;
    else {
      if (dir(tmp) == dir(p[i].second))
        tmp += p[i].second;
      else {
        ans *= abs(tmp);
        ans %= MOD;
        tmp += p[i].second;
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03878 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
int main() {
  pair<int, int> p[100010];
  int i, n, a, b;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a;
    p[i] = make_pair(a, 0);
  }
  for (i = 0; i < n; i++) {
    cin >> b;
    p[i + n] = make_pair(b, 1);
  }

  sort(p, p + 2 * n);
  long long ans = 1;
  int s = 0, t = 0;
  for (i = 0; i < 2 * n; i++) {
    if (p[i].second == 0) {
      if (t == 0) {
        s++;
      } else {
        ans *= t;
        ans = ans % 1000000007;
        t--;
      }
    } else {
      if (s == 0) {
        t++;
      } else {
        ans *= s;
        ans = ans % 1000000007;
        s--;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
int main() {
  pair<int, int> p[200010];
  int i, n, a, b;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a;
    p[i] = make_pair(a, 0);
  }
  for (i = 0; i < n; i++) {
    cin >> b;
    p[i + n] = make_pair(b, 1);
  }

  sort(p, p + 2 * n);
  long long ans = 1;
  int s = 0, t = 0;
  for (i = 0; i < 2 * n; i++) {
    if (p[i].second == 0) {
      if (t == 0) {
        s++;
      } else {
        ans *= t;
        ans = ans % 1000000007;
        t--;
      }
    } else {
      if (s == 0) {
        t++;
      } else {
        ans *= s;
        ans = ans % 1000000007;
        s--;
      }
    }
  }
  cout << ans << endl;
}
Problem p03878 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = 1 << 12;
const double EPS = 10e-8;
const double PI = 3.14159265359;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int n;
P p[100005];

int main() {
  cin >> n;
  REP(i, n) {
    ll a;
    cin >> a;
    p[i] = {a, 1};
  }
  REP(i, n) {
    ll b;
    cin >> b;
    p[i + n] = {b, 2};
  }
  sort(p, p + n * 2);
  ll t = 0, flag, ans = 1;
  REP(i, n * 2) {
    if (t == 0) {
      flag = p[i].second;
      t++;
    } else {
      if (p[i].second == flag) {
        t++;
      } else {
        ans = ans * t % MOD;
        t--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = 1 << 12;
const double EPS = 10e-8;
const double PI = 3.14159265359;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int n;
P p[200005];

int main() {
  cin >> n;
  REP(i, n) {
    ll a;
    cin >> a;
    p[i] = {a, 1};
  }
  REP(i, n) {
    ll b;
    cin >> b;
    p[i + n] = {b, 2};
  }
  sort(p, p + n * 2);
  ll t = 0, flag, ans = 1;
  REP(i, n * 2) {
    if (t == 0) {
      flag = p[i].second;
      t++;
    } else {
      if (p[i].second == flag) {
        t++;
      } else {
        ans = ans * t % MOD;
        t--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03879 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define in(v) cin >> v;
#define out(v) cout << v << "\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int x[3], y[3];
  rep(i, 3) {
    in(x[i]);
    in(y[i]);
  }
  double a = sqrt((x[0] - x[1]) * (x[0] - x[1]) +
                  (y[0] - y[1]) * (y[0] - y[1])),
         b = sqrt((x[1] - x[2]) * (x[1] - x[2]) +
                  (y[1] - y[2]) * (y[1] - y[2])),
         c = sqrt((x[2] - x[0]) * (x[2] - x[0]) +
                  (y[2] - y[0]) * (y[2] - y[0]));
  double max_abc = max(a, max(b, c));
  double S = sqrt((a + b + c) / 2.0 * (-a + b + c) / 2.0 * (a - b + c) / 2.0 *
                  (a + b - c) / 2.0);
  double r = 2.0 * S / (a + b + c);
  double start = 0, end = 1000;
  while (end - start > 1e-15) {
    double x = start + (end - start) / 2.0;
    if ((1 - x / r) * max_abc >= 2.0 * x)
      start = x;
    else
      end = x;
  }
  printf("%.12f\n", start);

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define in(v) cin >> v;
#define out(v) cout << v << "\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int x[3], y[3];
  rep(i, 3) {
    in(x[i]);
    in(y[i]);
  }
  double a = sqrt((x[0] - x[1]) * (x[0] - x[1]) +
                  (y[0] - y[1]) * (y[0] - y[1])),
         b = sqrt((x[1] - x[2]) * (x[1] - x[2]) +
                  (y[1] - y[2]) * (y[1] - y[2])),
         c = sqrt((x[2] - x[0]) * (x[2] - x[0]) +
                  (y[2] - y[0]) * (y[2] - y[0]));
  double max_abc = max(a, max(b, c));
  double S = sqrt((a + b + c) / 2.0 * (-a + b + c) / 2.0 * (a - b + c) / 2.0 *
                  (a + b - c) / 2.0);
  double r = 2.0 * S / (a + b + c);
  double start = 0, end = max_abc;
  rep(i, 1e5) {
    double x = start + (end - start) / 2.0;
    if ((1 - x / r) * max_abc >= 2.0 * x)
      start = x;
    else
      end = x;
  }
  printf("%.12f\n", start);

  return 0;
}
Problem p03879 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define int long long
#define ll long long
#define eps LDBL_EPSILON
#define mod 998244353
#define double long double
#define INF LLONG_MAX / 1000
#define P pair<int, int>
#define prique priority_queue
using namespace std;
double x[3], y[3], d[3];
signed main() {
  rep(i, 3) cin >> x[i] >> y[i];
  d[0] = hypot(x[0] - x[1], y[0] - y[1]);
  d[1] = hypot(x[1] - x[2], y[1] - y[2]);
  d[2] = hypot(x[2] - x[0], y[2] - y[0]);
  double s = (d[0] + d[1] + d[2]) / 2;
  double S = sqrt(s * (s - d[0]) * (s - d[1]) * (s - d[2]));
  double r = S / s;
  double mi = 0, ma = 1000;
  while (ma - mi > eps) {
    double md = (mi + ma) / 2;
    if (r < md)
      ma = md;
    else if (max({d[0], d[1], d[2]}) / r * (r - md) >= 2 * md)
      mi = md;
    else
      ma = md;
  }
  printf("%.12Lf\n", mi);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define int long long
#define ll long long
#define eps LDBL_EPSILON
#define mod 998244353
#define double long double
#define INF LLONG_MAX / 1000
#define P pair<int, int>
#define prique priority_queue
using namespace std;
double x[3], y[3], d[3];
signed main() {
  rep(i, 3) cin >> x[i] >> y[i];
  d[0] = hypot(x[0] - x[1], y[0] - y[1]);
  d[1] = hypot(x[1] - x[2], y[1] - y[2]);
  d[2] = hypot(x[2] - x[0], y[2] - y[0]);
  double s = (d[0] + d[1] + d[2]) / 2;
  double S = sqrt(s * (s - d[0]) * (s - d[1]) * (s - d[2]));
  double r = S / s;
  double mi = 0, ma = 1000;
  rep(i, 100000) {
    double md = (mi + ma) / 2;
    if (r < md)
      ma = md;
    else if (max({d[0], d[1], d[2]}) / r * (r - md) >= 2 * md)
      mi = md;
    else
      ma = md;
  }
  printf("%.12Lf\n", mi);
  return 0;
}
Problem p03880 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)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 fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;

int main(void) {
  uint32 N;
  vector<uint32> a;
  cin >> N;
  a.resize(N);
  REP(i, N) cin >> a[i];
  int32 cnt[32] = {};
  uint32 rest = 0;
  REP(i, N) {
    rest ^= a[i];
    if (a[i] != 0)
      cnt[__builtin_ffs(a[i])]++;
  }
  int32 res = 0;
  REP(i, N) {
    int32 bit = 32 - __builtin_clz(rest);
    if (cnt[bit]) {
      res++;
      rest ^= (1 << bit) - 1;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;
}

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

#define INF_LL (int64)1e18
#define INF (int32)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 fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;

int main(void) {
  uint32 N;
  vector<uint32> a;
  cin >> N;
  a.resize(N);
  REP(i, N) cin >> a[i];
  int32 cnt[32] = {};
  uint32 rest = 0;
  REP(i, N) {
    rest ^= a[i];
    if (a[i] != 0)
      cnt[__builtin_ffs(a[i])]++;
  }
  int32 res = 0;
  while (rest) {
    int32 bit = 32 - __builtin_clz(rest);
    if (cnt[bit]) {
      res++;
      rest ^= (1 << bit) - 1;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;
}
Problem p03880 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <iostream>
using namespace std;
int main(void) {
  int n, b[32], x = 0, c = 0;
  cin >> n;
  fill(b, b + 32, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    x ^= a;
    int t = (a ^ (a - 1));
    for (int j = 31; j >= 0; j--) {
      if (((t >> j) & 1) == 1) {
        b[i]++;
        break;
      }
    }
  }
  for (int i = 31; i >= 0; i--) {
    if (((x >> i) & 1) == 1) {
      if (b[i] > 0) {
        x ^= (1 << (i + 1)) - 1;
        c++;
      } else {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << c << endl;
}


Fixed submission:
#include <bitset>
#include <iostream>
using namespace std;
int main(void) {
  int n, b[32], x = 0, c = 0;
  cin >> n;
  fill(b, b + 32, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    x ^= a;
    int t = (a ^ (a - 1));
    for (int j = 31; j >= 0; j--) {
      if (((t >> j) & 1) == 1) {
        b[j]++;
        break;
      }
    }
  }
  for (int i = 31; i >= 0; i--) {
    if (((x >> i) & 1) == 1) {
      if (b[i] > 0) {
        x ^= (1 << (i + 1)) - 1;
        c++;
      } else {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << c << endl;
}
Problem p03884 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
#define show(x) std::cerr << #x << " = " << (x) << std::endl
int main() {
  ll K;
  std::cin >> K;
  const ll NUM = 1000;
  std::vector<ll> num(NUM + 1);
  for (ll i = 0; i <= NUM; i++) {
    num[i] = i * (i + 1) / 2 * (i + 2) / 3 * (i + 3) / 4 * (i + 4) / 5 *
             (i + 5) / 6 * (i + 6) / 7;
  }
  bool usef = false;
  std::string ans;
  for (int i = NUM; i >= 1; i--) {
    const ll f = K / num[i];
    K %= num[i];
    if (f == 0 and not usef) {
      continue;
    }
    usef = true, ans += std::string(f, 'F') + "ESTIVAL";
  }
  assert(ans.size() <= 5000);
  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
#define show(x) std::cerr << #x << " = " << (x) << std::endl
int main() {
  ll K;
  std::cin >> K;
  const ll NUM = 700;
  std::vector<ll> num(NUM + 1);
  for (ll i = 0; i <= NUM; i++) {
    num[i] = i * (i + 1) / 2 * (i + 2) / 3 * (i + 3) / 4 * (i + 4) / 5 *
             (i + 5) / 6 * (i + 6) / 7;
  }
  bool usef = false;
  std::string ans;
  for (int i = NUM; i >= 1; i--) {
    const ll f = K / num[i];
    K %= num[i];
    if (f == 0 and not usef) {
      continue;
    }
    usef = true, ans += std::string(f, 'F') + "ESTIVAL";
  }
  assert(ans.size() <= 5000);
  std::cout << ans << std::endl;
  return 0;
}
Problem p03887 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int Comb[5005][5005];

void add(int &a, int b) {
  a += b;
  while (a >= MOD)
    a -= MOD;
  while (a < 0)
    a += MOD;
}

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;

  if (b % 2) {
    cout << "0\n";
    return 0;
  }

  for (int i = 0; i <= n; ++i) {
    Comb[i][0] = 1;
    for (int j = 1; j <= i; ++j)
      Comb[i][j] = (Comb[i - 1][j - 1] + Comb[i - 1][j]) % MOD;
  }

  int block2 = b / 2;
  int ans = 0;

  for (int block1 = 0; block1 <= a; ++block1) {
    int block13 = (a - block1);
    for (int block3 = 0; 3 * block3 + block13 <= c; ++block3) {
      int remain3 = c - (3 * block3 + block13);
      if (remain3 && block2 == 0)
        continue;
      int stars = Comb[block2 + remain3 - 1][block2 - 1];
      int temp = Comb[block1 + block2][block1];
      temp = 1LL * temp * Comb[block1 + block2 + block13][block13] % MOD;
      temp =
          1LL * temp * Comb[block1 + block2 + block13 + block3][block3] % MOD;
      temp = 1LL * stars * temp % MOD;
      add(ans, temp);
    }
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int Comb[5005][5005];

void add(int &a, int b) {
  a += b;
  while (a >= MOD)
    a -= MOD;
  while (a < 0)
    a += MOD;
}

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;

  if (b % 2) {
    cout << "0\n";
    return 0;
  }

  for (int i = 0; i <= n; ++i) {
    Comb[i][0] = 1;
    for (int j = 1; j <= i; ++j)
      Comb[i][j] = (Comb[i - 1][j - 1] + Comb[i - 1][j]) % MOD;
  }

  int block2 = b / 2;
  int ans = 0;

  for (int block1 = 0; block1 <= a; ++block1) {
    int block13 = (a - block1);
    for (int block3 = 0; 3 * block3 + block13 <= c; ++block3) {
      int remain3 = c - (3 * block3 + block13);
      if (remain3 && block2 == 0)
        continue;
      int stars = 1;
      if (block2 > 0)
        stars = Comb[block2 + remain3 - 1][block2 - 1];
      int temp = Comb[block1 + block2][block1];
      temp = 1LL * temp * Comb[block1 + block2 + block13][block13] % MOD;
      temp =
          1LL * temp * Comb[block1 + block2 + block13 + block3][block3] % MOD;
      temp = 1LL * stars * temp % MOD;
      add(ans, temp);
    }
  }

  cout << ans << "\n";
}
Problem p03888 (Python)

Buggy submission (status: Runtime Error):
a, b, c = map(int, input().split())
print((a * b / (a + b)))


Fixed submission:
a, b = map(int, input().split())
print((a * b / (a + b)))
Problem p03889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char s[10005], ss[10005];
signed main() {
  scanf("%s", s);
  int len = strlen(s);
  for (int i = 0; i < len; i++) {
    switch (s[i]) {
    case 'b': {
      ss[len - i - 1] = 'd';
      break;
    }
    case 'd': {
      ss[len - i - 1] = 'b';
      break;
    }
    case 'p': {
      ss[len - i - 1] = 'q';
      break;
    }
    case 'q': {
      ss[len - i - 1] = 'p';
      break;
    }
    }
  }
  if (strcmp(s, ss))
    printf("No");
  else
    printf("Yes");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char s[100005], ss[100005];
signed main() {
  scanf("%s", s);
  int len = strlen(s);
  for (int i = 0; i < len; i++) {
    switch (s[i]) {
    case 'b': {
      ss[len - i - 1] = 'd';
      break;
    }
    case 'd': {
      ss[len - i - 1] = 'b';
      break;
    }
    case 'p': {
      ss[len - i - 1] = 'q';
      break;
    }
    case 'q': {
      ss[len - i - 1] = 'p';
      break;
    }
    }
  }
  if (strcmp(s, ss))
    printf("No");
  else
    printf("Yes");
  return 0;
}
Problem p03889 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

#define ull unsigned long long

const int base = 233;
const int N = 1e4 + 5;

char st[N];
ull hasf, hass, hast;

int main(void) {
  scanf("%s", st + 1);
  int len = strlen(st + 1);
  for (int i = 1; i <= len; i++)
    hasf = hasf * base + st[i];
  for (int i = len; i >= 1; i--)
    hass = hass * base + st[i];
  if (hasf == hass) {
    puts("Yes");
    //  system ( "pause" );
    return 0;
  }
  for (int i = 1; i <= len; i++) {
    if (st[i] == 'b')
      st[i] = 'd';
    else if (st[i] == 'd')
      st[i] = 'b';
    else if (st[i] == 'p')
      st[i] = 'q';
    else if (st[i] == 'q')
      st[i] = 'p';
  }
  for (int i = 1; i <= len; i++)
    hast = hast * base + st[i];
  if (hasf == hast || hast == hass) {
    puts("Yes");
    //   system ( "pause" );
    return 0;
  }
  puts("No");
  // system ( "pause" );
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

#define ull unsigned long long

const int base = 233;
const int N = 1e5 + 5;

char st[N];
ull hasf, hass, hast;

int main(void) {
  scanf("%s", st + 1);
  int len = strlen(st + 1);
  for (int i = 1; i <= len; i++)
    hasf = hasf * base + st[i];
  for (int i = len; i >= 1; i--)
    hass = hass * base + st[i];
  if (hasf == hass) {
    puts("Yes");
    //  system ( "pause" );
    return 0;
  }
  for (int i = 1; i <= len; i++) {
    if (st[i] == 'b')
      st[i] = 'd';
    else if (st[i] == 'd')
      st[i] = 'b';
    else if (st[i] == 'p')
      st[i] = 'q';
    else if (st[i] == 'q')
      st[i] = 'p';
  }
  for (int i = 1; i <= len; i++)
    hast = hast * base + st[i];
  if (hasf == hast || hast == hass) {
    puts("Yes");
    //   system ( "pause" );
    return 0;
  }
  puts("No");
  // system ( "pause" );
  return 0;
}
Problem p03889 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;
char a[10010], b[10010];
int main() {
  cin >> a;
  int j = 0;
  int l = strlen(a);
  for (int i = l - 1; i >= 0; i--) {
    b[j] = a[i];
    if (b[j] == 'p')
      b[j] = 'q';
    else if (b[j] == 'q')
      b[j] = 'p';
    else if (b[j] == 'b')
      b[j] = 'd';
    else if (b[j] == 'd')
      b[j] = 'b';
    j++;
  }
  if (!strcmp(a, b))
    cout << "Yes";
  else
    cout << "No";
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
char a[100100], b[100010];
int main() {
  cin >> a;
  int j = 0;
  int l = strlen(a);
  for (int i = l - 1; i >= 0; i--) {
    b[j] = a[i];
    if (b[j] == 'p')
      b[j] = 'q';
    else if (b[j] == 'q')
      b[j] = 'p';
    else if (b[j] == 'b')
      b[j] = 'd';
    else if (b[j] == 'd')
      b[j] = 'b';
    j++;
  }
  if (!strcmp(a, b))
    cout << "Yes";
  else
    cout << "No";
  return 0;
}
Problem p03889 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <cstring>
int main(void) {
  int c;
  int n;
  char d[10000];
  char e[10000];
  c = 0;
  scanf("%s", e);
  n = strlen(e);
  for (int i = 0; i < n; i++) {
    d[i] = e[n - i - 1];
    if (d[i] == 98)
      d[i] = 100;
    else if (d[i] == 100)
      d[i] = 98;
    else if (d[i] == 112)
      d[i] = 113;
    else if (d[i] == 113)
      d[i] = 112;
    if (d[i] == e[i])
      c++;
  }
  if (n == c)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
int main(void) {
  int c;
  int n;
  char d[100001];
  char e[100001];
  c = 0;
  scanf("%s", e);
  n = strlen(e);
  for (int i = 0; i < n; i++) {
    d[i] = e[n - i - 1];
    if (d[i] == 98)
      d[i] = 100;
    else if (d[i] == 100)
      d[i] = 98;
    else if (d[i] == 112)
      d[i] = 113;
    else if (d[i] == 113)
      d[i] = 112;
    if (d[i] == e[i])
      c++;
  }
  if (n == c)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}
Problem p03889 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
char a[1005], s[1005];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  a['q'] = 'p';
  a['b'] = 'd';
  a['p'] = 'q';
  a['d'] = 'b';
  for (int i = 1; i <= n; i++)
    if (s[i] != a[s[n - i + 1]]) {
      printf("No\n");
      return 0;
    }
  printf("Yes\n");
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
char a[1005], s[10000005];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  a['q'] = 'p';
  a['b'] = 'd';
  a['p'] = 'q';
  a['d'] = 'b';
  for (int i = 1; i <= n; i++)
    if (s[i] != a[s[n - i + 1]]) {
      printf("No\n");
      return 0;
    }
  printf("Yes\n");
}
Problem p03889 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  char a[10086], b[10086];
  int sum;
  cin >> a;
  sum = strlen(a);
  sum -= 1;
  for (int i = 0; a[i] != '\0'; i++) {
    if (a[i] == 'b')
      b[sum] = 'd';
    if (a[i] == 'd')
      b[sum] = 'b';
    if (a[i] == 'p')
      b[sum] = 'q';
    if (a[i] == 'q')
      b[sum] = 'p';
    sum--;
  }
  if (strcmp(a, b) == false) {
    cout << "Yes";
  } else {
    cout << "No";
  }
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  char a[10086000], b[10086000];
  int sum;
  cin >> a;
  sum = strlen(a);
  sum -= 1;
  for (int i = 0; a[i] != '\0'; i++) {
    if (a[i] == 'b')
      b[sum] = 'd';
    if (a[i] == 'd')
      b[sum] = 'b';
    if (a[i] == 'p')
      b[sum] = 'q';
    if (a[i] == 'q')
      b[sum] = 'p';
    sum--;
  }
  if (strcmp(a, b) == false) {
    cout << "Yes";
  } else {
    cout << "No";
  }
  return 0;
}
Problem p03889 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

inline char transform(char x) {
  if (x == 'b')
    return 'd';
  if (x == 'd')
    return 'b';
  if (x == 'q')
    return 'p';
  if (x == 'p')
    return 'q';
  return 0;
}

char s[10005];
int main(int argc, char **args) {
  scanf("%s", s);
  int len = strlen(s) - 1;
  for (int i = 0; s[i]; i++) {
    if (transform(s[i]) != s[len - i]) {
      return printf("No\n"), 0;
    }
  }
  printf("Yes\n");
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>

inline char transform(char x) {
  if (x == 'b')
    return 'd';
  if (x == 'd')
    return 'b';
  if (x == 'q')
    return 'p';
  if (x == 'p')
    return 'q';
  return 0;
}

char s[1000005];
int main(int argc, char **args) {
  scanf("%s", s);
  int len = strlen(s) - 1;
  for (int i = 0; s[i]; i++) {
    if (transform(s[i]) != s[len - i]) {
      return printf("No\n"), 0;
    }
  }
  printf("Yes\n");
  return 0;
}
Problem p03890 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

#include <cmath>

using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<int> v(pow(2, N));
  for (int i = 0; i < pow(2, N); i++)
    cin >> v[i];

  for (int i = N; i >= 1; --i) {
    for (int j = 0; j < pow(2, N); j++) {
      v[j] = max(v[2 * j], v[2 * j + 1]) - min(v[2 * j], v[2 * j + 1]);
    }
  }

  cout << v[0] << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

#include <cmath>

using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<int> v(pow(2, N));
  for (int i = 0; i < pow(2, N); i++)
    cin >> v[i];

  for (int i = N; i >= 1; --i) {
    for (int j = 0; j < pow(2, N - 1); j++) {
      v[j] = max(v[2 * j], v[2 * j + 1]) - min(v[2 * j], v[2 * j + 1]);
    }
  }

  cout << v[0] << endl;

  return 0;
}
Problem p03890 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)

int a[100000];

int main() {
  int n;
  cin >> n;
  int N = 1;
  rep(i, n) { N *= 2; }

  rep(i, N) { cin >> a[i]; }

  int right = N;
  rep(i, n) {
    for (int k = 0; k < right; k += 2) {
      if (a[k] != a[k + 1]) {
        a[k / 2] = abs(a[k] - a[k + 1]);
      }
    }
    right /= 2;
  }
  cout << a[0] << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)

int a[1000000];

int main() {
  int n;
  cin >> n;
  int N = 1;
  rep(i, n) { N *= 2; }

  rep(i, N) { cin >> a[i]; }

  int right = N;
  rep(i, n) {
    for (int k = 0; k < right; k += 2) {
      if (a[k] != a[k + 1]) {
        a[k / 2] = abs(a[k] - a[k + 1]);
      }
    }
    right /= 2;
  }
  cout << a[0] << endl;
}
Problem p03891 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)

int main() {
  int A, B, C;
  cin >> A >> B >> C;

  int n = 0;
  while (1) {
    int x4 = n - A - C;
    int x3 = n - B - C;
    int x6 = n - A - B;
    int x5 = n - x4 - x6;
    int x2 = n - x3 - x4;
    int x1 = n - A - x2;
    if (x1 < 0 || x2 < 0 || x3 < 0 || x4 < 0 || x5 < 0) {
      n++;
      continue;
    }
    if (x4 + x5 + x6 == n && x2 + x3 + x4 == n && A + x1 + x2 == n &&
        x1 + C + x5 == n && x2 + C + x6 == n) {
      printf("%d %d %d \n", A, B, x6);
      printf("%d %d %d \n", x1, C, x5);
      printf("%d %d %d \n", x2, x3, x4);
      break;
    }
    n++;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)

int main() {
  int A, B, C;
  cin >> A >> B >> C;

  int n = 0;
  while (1) {
    int x4 = n - A - C;
    int x3 = n - B - C;
    int x6 = n - A - B;
    int x5 = n - x4 - x6;
    int x2 = n - x3 - x4;
    int x1 = n - A - x2;
    /*if(x1<0 || x2<0 || x3<0 || x4<0 || x5<0){
        n++;
        continue;
    }*/
    if (x4 + x5 + x6 == n && x2 + x3 + x4 == n && A + x1 + x2 == n &&
        x1 + C + x5 == n && x2 + C + x6 == n) {
      printf("%d %d %d \n", A, B, x6);
      printf("%d %d %d \n", x1, C, x5);
      printf("%d %d %d \n", x2, x3, x4);
      break;
    }
    n++;
  }
}
Problem p03891 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int MAX = max(a + b, max(b + c, c + a));
  int d[3][3] = {0};
  d[0][0] = a;
  d[0][1] = b;
  d[1][1] = c;
  for (int i = MAX;; i++) {
    d[0][2] = i - d[0][0] - d[0][1];
    d[2][1] = i - d[0][1] - d[1][1];
    d[2][2] = i - d[0][0] - d[1][1];
    d[1][2] = i - d[0][2] - d[2][2];
    d[2][0] = i - d[2][1] - d[2][2];
    d[1][0] = i - d[1][1] - d[1][2];
    bool h = true;
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (d[j][k] < 0) {
          h = false;
        }
      }
    }
    for (int j = 1; j < 3; j++) {
      if (d[0][j] + d[1][j] + d[2][j] != i) {
        h = false;
      }
    }
    for (int j = 0; j < 3; j++) {
      if (d[j][0] + d[j][1] + d[j][2] != i) {
        h = false;
      }
    }
    if (d[0][2] + d[1][1] + d[2][0] != i) {
      h = false;
    }
    if (h) {
      break;
    }
  }
  for (int i = 0; i < 3; i++) {
    cout << d[i][0];
    for (int j = 1; j < 3; j++) {
      cout << ' ' << d[i][j];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int MAX = max(a + b, max(b + c, c + a));
  int d[3][3] = {0};
  d[0][0] = a;
  d[0][1] = b;
  d[1][1] = c;
  int x = 3 * c;
  d[0][2] = x - a - b;
  d[2][1] = x - b - c;
  d[2][2] = x - a - c;
  d[1][2] = x - d[0][2] - d[2][2];
  d[2][0] = x - d[2][1] - d[2][2];
  d[1][0] = x - d[0][0] - d[2][0];
  for (int i = 0; i < 3; i++) {
    cout << d[i][0];
    for (int j = 1; j < 3; j++) {
      cout << ' ' << d[i][j];
    }
    cout << endl;
  }
  return 0;
}
Problem p03892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <typeinfo>
#include <vector>

#define DIV 1000000007

using namespace std;

long long A, B, C, D;
long long X, Y;

long gcd(long a, long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  cin >> A >> B >> C >> D;
  X = abs(A - C);
  Y = abs(B - D);
  if (X == Y) {
    cout << X << endl;
    return 0;
  }
  long long ans;
  long long num = gcd(X, Y);
  X /= num;
  Y /= num;

  ans = 0;

  set<pair<long long, long long>> done;
  double slope = 1.0 * Y / X;
  done.insert(make_pair(0, 0));
  done.insert(make_pair(X - 1, Y - 1));
  // cout << endl;
  for (int i = 1; i < X; i++) {
    double yy = slope * i;
    done.insert(make_pair(i - 1, (int)yy));
    done.insert(make_pair(i, (int)yy));
  }
  /*
  for(auto d: done){
          cout << d.first << "," << d.second << endl;
  }
  cout << endl;
  */
  for (int i = 1; i < Y; i++) {
    // cout << "slope " << slope << endl;
    double xx = i / slope;
    // cout << "y = " << i << " xx " << xx << endl;
    done.insert(make_pair((int)xx, i));
    done.insert(make_pair((int)xx, i - 1));
  }

  ans = done.size();
  cout << ans * num << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <typeinfo>
#include <vector>

#define DIV 1000000007

using namespace std;

long long A, B, C, D;
long long X, Y;

long gcd(long a, long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  cin >> A >> B >> C >> D;
  X = abs(A - C);
  Y = abs(B - D);
  if (X == Y) {
    cout << X << endl;
    return 0;
  }
  long long ans;
  long long num = gcd(X, Y);
  X /= num;
  Y /= num;

  ans = 0;

  ans = X + Y - 1;
  cout << ans * num << endl;
}
Problem p03892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  return gcd(y, x % y);
}
signed main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  cout << abs(c - a) + abs(d - b) - gcd(abs(c - a), abs(d - b)) << endl;
  getchar();
  getchar();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
signed main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  cout << abs(c - a) + abs(d - b) - gcd(abs(c - a), abs(d - b)) << endl;
  getchar();
  getchar();
  return 0;
}
Problem p03897 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(), (A).end()
#define ll long long
const ll mod = 1e9 + 7;
const ll INF = 2 * 1e18;
const int inf = 1e9 + 7;

string s[1005];
int main(void) {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if ((i + j) % 2 == 0) {
        s[i][j] = '#';
      } else {
        s[i][j] = '.';
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (s[i][0] == '.') {
      ans++;
      s[i][0] = 'x';
    }
    if (s[0][i] == '.') {
      ans++;
      s[0][i] = 'x';
    }
    if (s[N - 1][i] == '.') {
      ans++;
      s[N - 1][i] = 'x';
    }
  }
  for (int i = 1; i < N - 1; i++)
    if (i % 3 == 2) {
      for (int j = 0; j < N; j++) {
        if (s[i][j] == '.') {
          ans++;
          s[i][j] = 'x';
        }
      }
    }

  cout << ans << endl;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (s[i][j] == 'x')
        cout << i << " " << j << endl;
    }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(), (A).end()
#define ll long long
const ll mod = 1e9 + 7;
const ll INF = 2 * 1e18;
const int inf = 1e9 + 7;

char s[1005][1005];
int main(void) {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if ((i + j) % 2 == 0) {
        s[i][j] = '#';
      } else {
        s[i][j] = '.';
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (s[i][0] == '.') {
      ans++;
      s[i][0] = 'x';
    }
    if (s[0][i] == '.') {
      ans++;
      s[0][i] = 'x';
    }
    if (s[N - 1][i] == '.') {
      ans++;
      s[N - 1][i] = 'x';
    }
  }
  for (int i = 1; i < N - 1; i++)
    if (i % 3 == 2) {
      for (int j = 0; j < N; j++) {
        if (s[i][j] == '.') {
          ans++;
          s[i][j] = 'x';
        }
      }
    }

  cout << ans << endl;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (s[i][j] == 'x')
        cout << i << " " << j << endl;
    }
}
Problem p03899 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()

const ll inf = 1e18;
const int N = 100100;

ll n, m, k, a[N], dp[N][305], ans;

int main() {
  cin >> n >> m >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i][1] = a[i];
  }
  for (ll i = 2; i <= k; i++) {
    for (ll j = i; j <= n; j++)
      for (ll t = max(1LL, j - m); t < j; t++)
        dp[j][i] = max(dp[j][i], dp[t][i - 1] + i * a[j]);
  }
  for (ll i = 1; i <= n; i++)
    ans = max(ans, dp[i][k]);
  cout << ans;
}


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

typedef long long ll;
typedef pair<ll, ll> pll;

#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()

const ll inf = 1e18;
const int N = 100100;

ll n, m, k, a[N], dp[N][305], ans;

int main() {
  cin >> n >> m >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i][1] = a[i];
  }
  for (ll i = 2; i <= k; i++) {
    deque<ll> d;
    ll j;
    for (j = max(1LL, i - m); j < i; j++) {
      while (!d.empty() && dp[j][i - 1] >= dp[d.back()][i - 1])
        d.pop_back();
      d.push_back(j);
    }
    for (; j <= n; j++) {
      ll mx = dp[d.front()][i - 1];
      dp[j][i] = max(dp[j][i], mx + i * a[j]);
      while (!d.empty() && d.front() <= j - m)
        d.pop_front();
      while (!d.empty() && dp[j][i - 1] >= dp[d.back()][i - 1])
        d.pop_back();
      d.push_back(j);
    }
  }
  for (ll i = 1; i <= n; i++)
    ans = max(ans, dp[i][k]);
  cout << ans;
}
Problem p03899 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

template <typename T> vector<T> slide_min(const vector<T> &v, int k) {
  deque<int> deq;
  vector<T> ret;
  for (int i = 0; i < v.size(); i++) {
    while (!deq.empty() && v[deq.back()] <= v[i]) {
      deq.pop_back();
    }
    deq.push_back(i);
    if (i - k + 1 >= 0) {
      ret.emplace_back(v[deq.front()]);
      if (deq.front() == i - k + 1)
        deq.pop_front();
    }
  }
  return (ret);
}

using int64 = long long;

int main() {
  int N, M, K, A[10000];

  cin >> N >> M >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<int64> dp(N);
  for (int i = 0; i < N; i++) {
    dp[i] = A[i];
  }
  for (int i = 1; i < K; i++) {
    vector<int64> dp2(N);
    reverse(begin(dp), end(dp));
    dp.resize(N + M - 1);
    reverse(begin(dp), end(dp));
    auto p = slide_min(dp, M);

    for (int j = i; j < N; j++) {

      dp2[j] = max(dp2[j], p[j - 1] + 1LL * (i + 1) * A[j]);
    }
    dp.swap(dp2);
  }

  cout << *max_element(begin(dp), end(dp)) << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

template <typename T> vector<T> slide_min(const vector<T> &v, int k) {
  deque<int> deq;
  vector<T> ret;
  for (int i = 0; i < v.size(); i++) {
    while (!deq.empty() && v[deq.back()] <= v[i]) {
      deq.pop_back();
    }
    deq.push_back(i);
    if (i - k + 1 >= 0) {
      ret.emplace_back(v[deq.front()]);
      if (deq.front() == i - k + 1)
        deq.pop_front();
    }
  }
  return (ret);
}

using int64 = long long;

int main() {
  int N, M, K, A[100000];

  cin >> N >> M >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<int64> dp(N);
  for (int i = 0; i < N; i++) {
    dp[i] = A[i];
  }
  for (int i = 1; i < K; i++) {
    vector<int64> dp2(N);
    reverse(begin(dp), end(dp));
    dp.resize(N + M - 1);
    reverse(begin(dp), end(dp));
    auto p = slide_min(dp, M);

    for (int j = i; j < N; j++) {

      dp2[j] = max(dp2[j], p[j - 1] + 1LL * (i + 1) * A[j]);
    }
    dp.swap(dp2);
  }

  cout << *max_element(begin(dp), end(dp)) << endl;
}
Problem p03903 (C++)

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

int par[5000];
int find(int x) {
  if (par[x] == -1)
    return x;
  return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  par[x] = y;
}
struct st {
  int a, b, c;
};

int s[200000], t[200000];
int l[200000], r[200000];
vector<int> query[5000];

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<st> v, u;
  rep(i, m) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    v.push_back({a, b, c});
  }
  sort(v.begin(), v.end(), [](st a, st b) { return a.c < b.c; });
  memset(par, -1, sizeof(par));
  ll cnt = 0;
  for (st &p : v) {
    if (find(p.a) == find(p.b))
      continue;
    cnt += p.c;
    unite(p.a, p.b);
    u.push_back(p);
  }
  int q;
  scanf("%d", &q);
  rep(i, q) {
    scanf("%d%d", &s[i], &t[i]);
    s[i]--;
    t[i]--;
    l[i] = 0;
    r[i] = n - 1;
  }
  rep(i, 30) {
    rep(j, n - 1) query[j].clear();
    rep(j, n) {
      if (r[j] - l[j] > 1)
        query[(l[j] + r[j]) / 2].push_back(j);
    }
    memset(par, -1, sizeof(par));
    rep(j, n - 1) {
      for (int k : query[j]) {
        if (find(s[k]) != find(t[k]))
          l[k] = j;
        else
          r[k] = j;
      }
      unite(u[j].a, u[j].b);
    }
  }
  rep(i, q) {
    if (r[i] - l[i] > 1) {
      if (l[i] == 0)
        abort();
    }
    printf("%lld\n", cnt - u[l[i]].c);
  }
}

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

int par[5000];
int find(int x) {
  if (par[x] == -1)
    return x;
  return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  par[x] = y;
}
struct st {
  int a, b, c;
};

int s[200000], t[200000];
int l[200000], r[200000];
vector<int> query[5000];

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<st> v, u;
  rep(i, m) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    v.push_back({a, b, c});
  }
  sort(v.begin(), v.end(), [](st a, st b) { return a.c < b.c; });
  memset(par, -1, sizeof(par));
  ll cnt = 0;
  for (st &p : v) {
    if (find(p.a) == find(p.b))
      continue;
    cnt += p.c;
    unite(p.a, p.b);
    u.push_back(p);
  }
  int q;
  scanf("%d", &q);
  rep(i, q) {
    scanf("%d%d", &s[i], &t[i]);
    s[i]--;
    t[i]--;
    l[i] = 0;
    r[i] = n - 1;
  }
  rep(i, 30) {
    rep(j, n - 1) query[j].clear();
    rep(j, q) {
      if (r[j] - l[j] > 1)
        query[(l[j] + r[j]) / 2].push_back(j);
    }
    memset(par, -1, sizeof(par));
    rep(j, n - 1) {
      for (int k : query[j]) {
        if (find(s[k]) != find(t[k]))
          l[k] = j;
        else
          r[k] = j;
      }
      unite(u[j].a, u[j].b);
    }
  }
  rep(i, q) {
    if (r[i] - l[i] > 1) {
      if (l[i] == 0)
        abort();
    }
    printf("%lld\n", cnt - u[l[i]].c);
  }
}