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

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

#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}

template <class t> using vc = vector<t>;
template <class t> using vvc = vc<vc<t>>;

using vi = vc<int>;
long gcd(long a, long b) {
  if (b == 0)
    return 0;
  else
    return gcd(b, a % b);
}

int fact(int n) {
  if (n == 0)
    return 1;
  return fact(n - 1);
}
int main() {
  int t;
  cin >> t;
  long a, b, c, d;
  long g;
  rep(i, t) {
    cin >> a >> b >> c >> d;
    g = gcd(b, d);
    if (b > a) {
      puts("No");
    } else if (b > d) {
      puts("No");
    } else if (c >= b) {
      puts("Yes");

    } else if ((b - g + (a % g)) > c) {
      puts("No");
    } else {
      puts("Yes");
    }
  }
}

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

#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}

template <class t> using vc = vector<t>;
template <class t> using vvc = vc<vc<t>>;

using vi = vc<int>;
long gcd(long a, long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int fact(int n) {
  if (n == 0)
    return 1;
  return fact(n - 1);
}
int main() {
  int t;
  cin >> t;
  long a, b, c, d;
  long g;
  rep(i, t) {
    cin >> a >> b >> c >> d;
    g = gcd(b, d);
    if (b > a) {
      puts("No");
    } else if (b > d) {
      puts("No");
    } else if (c >= b) {
      puts("Yes");

    } else if ((b - g + (a % g)) > c) {
      puts("No");
    } else {
      puts("Yes");
    }
  }
}
Problem p03297 (C++)

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

using namespace std;

long long gcd(long long a, long long b) {
  if (a < b) {
    return gcd(b, a);
  }
  return b == 0 ? a : gcd(b, a % b);
}

bool solve(long long a, long long b, long long c, long long d) {
  if (a < b || d < b) {
    return false;
  }
  if (c >= b) {
    return true;
  }
  auto dv = gcd(d, b);
  long long mx = 0;
  for (int i = 0; i < b; i++) {
    mx = max(mx, (a + i * d) % b);
  }
  return mx <= c;
}

int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    cout << (solve(a, b, c, d) ? "Yes" : "No") << endl;
  }
}

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

using namespace std;

long long gcd(long long a, long long b) {
  if (a < b) {
    return gcd(b, a);
  }
  return b == 0 ? a : gcd(b, a % b);
}

bool solve(long long a, long long b, long long c, long long d) {
  if (a < b || d < b) {
    return false;
  }
  if (c >= b) {
    return true;
  }
  auto dv = gcd(d, b);
  long long mx = b + a % dv - dv;
  return mx <= c;
}

int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    cout << (solve(a, b, c, d) ? "Yes" : "No") << endl;
  }
}
Problem p03297 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ld long double
#define int long long
#define dbgvi(x)                                                               \
  cout << #x << " = ";                                                         \
  for (int i = 0; i < (x.size()); ++i)                                         \
    cout << x[i] << " ";
using namespace std;

void no() { cout << "No\n"; }

void yes() { cout << "Yes\n"; }

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

void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  if (b > a || b > d) {
    no();
    return;
  }
  if (c >= b) {
    yes();
    return;
  }
  int l = c + 1;
  int r = b - 1;
  if (l > r) {
    yes();
    return;
  }
  l = (l - (a % b) + b) % b;
  r = (r - (a % b) + b) % b;
  assert(l <= r);
  int g = gcd(b, d);
  int k = ((l + g - 1) / g) * g;
  if (k <= r) {
    no();
    return;
  } else {
    yes();
    return;
  }
}

signed main() {
  int t = 1;
  cout << fixed << setprecision(10);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> t;
  // freopen("skyscraper.in", "r", stdin);
  // freopen("skyscraper.out", "w", stdout);
  // cin >> t;
  while (t--) {
    solve();
  }
}


Fixed submission:
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ld long double
#define int long long
#define dbgvi(x)                                                               \
  cout << #x << " = ";                                                         \
  for (int i = 0; i < (x.size()); ++i)                                         \
    cout << x[i] << " ";
using namespace std;

void no() { cout << "No\n"; }

void yes() { cout << "Yes\n"; }

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

void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  if (b > a || b > d) {
    no();
    return;
  }
  if (c >= b) {
    yes();
    return;
  }
  int l = c + 1;
  int r = b - 1;
  if (l > r) {
    yes();
    return;
  }
  l = (l - (a % b) + b) % b;
  r = (r - (a % b) + b) % b;
  if (l > r) {
    r += b;
  }
  int g = gcd(b, d);
  int k = ((l + g - 1) / g) * g;
  if (k <= r) {
    no();
    return;
  } else {
    yes();
    return;
  }
}

signed main() {
  int t = 1;
  cout << fixed << setprecision(10);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> t;
  // freopen("skyscraper.in", "r", stdin);
  // freopen("skyscraper.out", "w", stdout);
  // cin >> t;
  while (t--) {
    solve();
  }
}
Problem p03297 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
using namespace std;
#define LL long long
LL gcd(LL a, LL b) {
  LL tmp;
  while (b) {
    tmp = a;
    a = b, b = tmp % b;
  }
  return a;
}
int main() {
  LL A, B, C, D;
  int tm;
  scanf("%d", &tm);
  while (tm--) {
    scanf("%lld%lld%lld%lld", &A, &B, &C, &D);
    bool f = 1;
    if (A < B)
      f = 0;
    else if (D < B)
      f = 0;
    else if (C >= B - 1)
      ;
    else {
      LL E = gcd(B, D);
      for (LL i = A - B + 1; i < A - C; i++)
        if (i % E == 0) {
          f = 0;
          break;
        }
    }
    if (f)
      printf("Yes\n");
    else
      printf("No\n");
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
#define LL long long
LL gcd(LL a, LL b) {
  LL tmp;
  while (b) {
    tmp = a;
    a = b, b = tmp % b;
  }
  return a;
}
int main() {
  LL A, B, C, D;
  int tm;
  scanf("%d", &tm);
  while (tm--) {
    scanf("%lld%lld%lld%lld", &A, &B, &C, &D);
    bool f = 1;
    if (A < B)
      f = 0;
    else if (D < B)
      f = 0;
    else if (C >= B - 1)
      ;
    else {
      LL E = gcd(B, D);
      if ((A - B) / E < (A - C - 1) / E)
        f = 0;
    }
    if (f)
      printf("Yes\n");
    else
      printf("No\n");
  }
}
Problem p03297 (C++)

Buggy submission (status: Time Limit Exceeded):
//----------------------------おまじない
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
//----------------------------
#define FOR(i, j, n) for (int i = (j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPN(i, n) for (int i = (n); i > 0; i--)
#define I(n) scanf("%d", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#include <bits/stdc++.h>
using namespace std;
//------------------------------typedef集
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;
typedef long long ll;
const int mod = 1000000009;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b) {
    return gcd(b, a % b);
  } else {
    return b;
  }
}

int t;
ll a, b, c, d;

int main() {
  I(t);
  REP(_, t) {
    LL(a);
    LL(b);
    LL(c);
    LL(d);
    if (a < b) {
      cout << "No" << endl;
      continue;
    }

    ll dgcd = gcd(b, d);
    a %= b;
    a += b;
    while (a >= b)
      a -= dgcd;

    if (d >= b && c >= a) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

Fixed submission:
//----------------------------おまじない
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
//----------------------------
#define FOR(i, j, n) for (int i = (j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPN(i, n) for (int i = (n); i > 0; i--)
#define I(n) scanf("%d", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#include <bits/stdc++.h>
using namespace std;
//------------------------------typedef集
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;
typedef long long ll;
const int mod = 1000000009;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b) {
    return gcd(b, a % b);
  } else {
    return b;
  }
}

int t;
ll a, b, c, d;

int main() {
  I(t);
  REP(_, t) {
    LL(a);
    LL(b);
    LL(c);
    LL(d);
    if (a < b) {
      cout << "No" << endl;
      continue;
    }

    ll dgcd = gcd(b, d);
    a %= b;
    a %= dgcd;
    a += b - dgcd;

    if (d >= b && c >= a) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
Problem p03297 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <assert.h>
#include <bits/stdc++.h>

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int query = 0; query < t; query++) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    map<long long, bool> m;
    while (a >= 0 && !m[a]) {
      m[a] = true;
      a -= b;
      a = a - max((long long)0, (a - c)) / b * b;
      if (a <= c && a >= 0)
        a += d;
    }
    if (a < 0)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }

  return 0;
}


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

using namespace std;

int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    long long A, B, C, D;
    cin >> A >> B >> C >> D;
    if (A < B || D < B || C < A % B || C < B - __gcd(B, D))
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }

  return 0;
}
Problem p03297 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;

ll gcd(ll m, ll n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

bool isMugen(ll A, ll B, ll C, ll D) {
  if (A < B) {
    return false;
  }

  if (D < B) {
    return false;
  }
  if (C >= B) {
    return true;
  }

  ll E = A % B;
  ll F = D % B;

  if (C < E) {
    return false;
  }
  if (F == 0) {
    return true;
  }

  ll H = gcd(B, F);
  if (B - H > C) {
    return false;
  }

  return true;
}

int main() {
  int T;
  cin >> T;
  ll A[300];
  ll B[300];
  ll C[300];
  ll D[300];

  for (int i = 0; i < T; ++i) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
  }

  for (int i = 0; i < T; ++i) {
    if (isMugen(A[i], B[i], C[i], D[i])) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}

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

using namespace std;
using ll = long long;
using ld = long double;

ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }

bool isMugen(ll A, ll B, ll C, ll D) {
  if (A < B) {
    return false;
  }

  if (D < B) {
    return false;
  }
  if (C >= B) {
    return true;
  }

  ll E = A % B;
  ll F = D % B;

  if (C < E) {
    return false;
  }
  if (F == 0) {
    return true;
  }

  ll H = gcd(B, F);
  if (B - H > C) {
    return false;
  }

  return true;
}

int main() {
  int T;
  cin >> T;
  ll A[300];
  ll B[300];
  ll C[300];
  ll D[300];

  for (int i = 0; i < T; ++i) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
  }

  for (int i = 0; i < T; ++i) {
    if (isMugen(A[i], B[i], C[i], D[i])) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}
Problem p03297 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint ungigned
#define db double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define vi vector<int>
#define vpi vector<pii>
#define IT iterator

#define PB push_back
#define MK make_pair
#define LB lower_bound
#define UB upper_bound
#define y1 wzpakking
#define fi first
#define se second
#define BG begin
#define ED end

#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define UPD(x, y) (((x) += (y)) >= mo ? (x) -= mo : 233)
#define CLR(a, v) memset(a, v, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define sqr(x) (1ll * x * x)

#define LS3 k * 2, l, mid
#define RS3 k * 2 + 1, mid + 1, r
#define LS5 k * 2, l, mid, x, y
#define RS5 k * 2 + 1, mid + 1, r, x, y
#define GET                                                                    \
  pushdown(k);                                                                 \
  int mid = (l + r) / 2
#define INF (1ll << 60)
using namespace std;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    ll a, b, c, d;
    scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
    if (a < b || b > d) {
      puts("No");
      continue;
    }
    ll G = gcd(b, d);
    a %= G;
    ll mn = c - b;
    mn = mn / G * G + a;
    for (; mn <= c; mn += G)
      ;
    if (mn < b)
      puts("No");
    else
      puts("Yes");
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint ungigned
#define db double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define vi vector<int>
#define vpi vector<pii>
#define IT iterator

#define PB push_back
#define MK make_pair
#define LB lower_bound
#define UB upper_bound
#define y1 wzpakking
#define fi first
#define se second
#define BG begin
#define ED end

#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define UPD(x, y) (((x) += (y)) >= mo ? (x) -= mo : 233)
#define CLR(a, v) memset(a, v, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define sqr(x) (1ll * x * x)

#define LS3 k * 2, l, mid
#define RS3 k * 2 + 1, mid + 1, r
#define LS5 k * 2, l, mid, x, y
#define RS5 k * 2 + 1, mid + 1, r, x, y
#define GET                                                                    \
  pushdown(k);                                                                 \
  int mid = (l + r) / 2
#define INF (1ll << 60)
using namespace std;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    ll a, b, c, d;
    scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
    if (a < b || b > d) {
      puts("No");
      continue;
    }
    ll G = gcd(b, d);
    a %= G;
    ll mn = c;
    mn = mn / G * G + a;
    for (; mn <= c; mn += G)
      ;
    if (mn < b)
      puts("No");
    else
      puts("Yes");
  }
}
Problem p03297 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vii = vector<ii>;
using vvii = vector<vii>;

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class TIn, class TOut = null_type>
using order_tree =
    tree<TIn, TOut, less<TIn>, rb_tree_tag, tree_order_statistics_node_update>;

constexpr int INF = 2000000010;
constexpr ll LLINF = 9000000000000000010LL;

ll mod(ll a, ll M) { return (a % M + M) % M; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int T;
  cin >> T;
  while (T--) {
    ll A, B, C, D;
    cin >> A >> B >> C >> D;

    if (D < B || A < B) {
      cout << "No\n";
      continue;
    }
    if (D == B) {
      cout << (A % B <= C ? "Yes\n" : "No\n");
      continue;
    }
    if (B <= C) {
      cout << "Yes\n";
      continue;
    }

    A -= B * ((max(0LL, A - C) + B - 1LL) / B);
    if (A < 0LL) {
      cout << "No\n";
      continue;
    }

    ll x = D - (D / B) * B;
    if (x == 0) {
      cout << "Yes\n";
      continue;
    }

    bool ok = true;
    for (int i = 0; ok && i < 10000000; ++i) {
      ok = ok && A >= 0LL;
      A += (C + 1LL - A + x - 1LL) / x * x - B;
    }

    cout << (ok ? "Yes\n" : "No\n");
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vii = vector<ii>;
using vvii = vector<vii>;

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class TIn, class TOut = null_type>
using order_tree =
    tree<TIn, TOut, less<TIn>, rb_tree_tag, tree_order_statistics_node_update>;

constexpr int INF = 2000000010;
constexpr ll LLINF = 9000000000000000010LL;

ll mod(ll a, ll M) { return (a % M + M) % M; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int T;
  cin >> T;
  while (T--) {
    ll A, B, C, D;
    cin >> A >> B >> C >> D;

    if (D < B || A < B) {
      cout << "No\n";
      continue;
    }
    if (D == B) {
      cout << (A % B <= C ? "Yes\n" : "No\n");
      continue;
    }
    if (B <= C) {
      cout << "Yes\n";
      continue;
    }

    A -= B * ((max(0LL, A - C) + B - 1LL) / B);
    if (A < 0LL) {
      cout << "No\n";
      continue;
    }

    ll x = D - (D / B) * B;
    if (x == 0) {
      cout << "Yes\n";
      continue;
    }

    bool ok = true;
    for (int i = 0; ok && i < 1000000; ++i) {
      ok = ok && A >= 0LL;
      A += (C + 1LL - A + x - 1LL) / x * x - B;
    }

    cout << (ok ? "Yes\n" : "No\n");
  }

  return 0;
}
Problem p03297 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

int mgcd(int a, int b) {
#ifdef LOCAL
  return gcd(a, b);
#else
  return __gcd(a, b);
#endif
}

void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  if (d < b) {
    cout << "No\n";
    return;
  }
  if (a < b) {
    cout << "No\n";
    return;
  }
  d %= b;
  if (c >= b) {
    cout << "Yes\n";
    return;
  }
  a %= b;
  int g = mgcd(d, b);
  int p = b - 1;
  while ((p % g) != (a % g)) {
    p--;
  }
  if (abs(b - p) < b - c) {
    cout << "No\n";
    return;
  }
  cout << "Yes\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int q;
  cin >> q;
  while (q--) {
    solve();
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

int mgcd(int a, int b) {
#ifdef LOCAL
  return gcd(a, b);
#else
  return __gcd(a, b);
#endif
}

void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  if (d < b) {
    cout << "No\n";
    return;
  }
  if (a < b) {
    cout << "No\n";
    return;
  }
  d %= b;
  if (c >= b) {
    cout << "Yes\n";
    return;
  }
  a %= b;
  int g = mgcd(d, b);
  int p = ((b - 1) / g) * g + a % g;
  while (p > b - 1) {
    p -= g;
  }
  if (abs(b - p) < b - c) {
    cout << "No\n";
    return;
  }
  cout << "Yes\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int q;
  cin >> q;
  while (q--) {
    solve();
  }
}
Problem p03297 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

void solve() {
  int t;
  cin >> t;
  while (t--) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a < b) {
      cout << "No\n";
    } else if (b > d) {
      cout << "No\n";
    } else if (c + 1 >= b) {
      cout << "Yes\n";
    } else {
      ll g = gcd(b, d);
      assert(a + g * ((b - a - 1) / g) < b);
      if (a + g * ((b - a - 1) / g) > c) {
        cout << "No\n";
      } else {
        cout << "Yes\n";
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

void solve() {
  int t;
  cin >> t;
  while (t--) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a < b) {
      cout << "No\n";
    } else if (b > d) {
      cout << "No\n";
    } else if (c + 1 >= b) {
      cout << "Yes\n";
    } else {
      ll g = gcd(b, d);
      ll x = a + g * ((b - a - 1) / g);
      while (x >= b) {
        x -= g;
      }
      if (x > c) {
        cout << "No\n";
      } else {
        cout << "Yes\n";
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p03297 (C++)

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

int main() {
  unsigned long long t, a, b, c, d;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> a >> b >> c >> d;
    if (b > d || a < b) {
      cout << "No" << endl;
    } else if (b <= c) {
      cout << "Yes" << endl;
    } else {
      bool f = 1;
      unsigned long long x = __gcd(b, d);
      for (unsigned long long j = b - 1; j > c; j--) {
        if ((a - j) % x == 0) {
          cout << "No" << endl;
          f = 0;
          break;
        }
      }
      if (f) {
        cout << "Yes" << endl;
      }
    }
  }
}

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

int main() {
  unsigned long long t, a, b, c, d;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> a >> b >> c >> d;
    if (b > d || a < b) {
      cout << "No" << endl;
    } else if (b <= c) {
      cout << "Yes" << endl;
    } else {
      bool f = 1;
      unsigned long long x = __gcd(b, d);
      if (b - x + a % x > c) {
        cout << "No" << endl;
      } else {
        cout << "Yes" << endl;
      }
    }
  }
}
Problem p03298 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
using namespace std;

typedef long long ll;
const int MAXN = 18 + 10;
int N;
char S[MAXN];
map<pair<string, string>, ll> mp;
ll ans;

int main() {
  register int s, i;
  scanf("%d%s", &N, S);
  for (s = 0; s < (1 << N); ++s) {
    string s1, s2;
    for (i = 0; i < N; ++i)
      if (s >> i & 1)
        s1.push_back(S[i]);
      else
        s2.push_back(S[i]);
    ++mp[make_pair(s1, s2)];
  }
  for (s = 0; s < (1 << N); ++s) {
    string s1, s2;
    for (i = N - 1; ~i; --i)
      if (s >> i & 1)
        s2.push_back(S[N + i]);
      else
        s1.push_back(S[N + i]);
    ans += mp[make_pair(s1, s2)];
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
using namespace std;

typedef long long ll;
const int MAXN = 36 + 10;
int N;
char S[MAXN];
map<pair<string, string>, ll> mp;
ll ans;

int main() {
  register int s, i;
  scanf("%d%s", &N, S);
  for (s = 0; s < (1 << N); ++s) {
    string s1, s2;
    for (i = 0; i < N; ++i)
      if (s >> i & 1)
        s1.push_back(S[i]);
      else
        s2.push_back(S[i]);
    ++mp[make_pair(s1, s2)];
  }
  for (s = 0; s < (1 << N); ++s) {
    string s1, s2;
    for (i = N - 1; ~i; --i)
      if (s >> i & 1)
        s2.push_back(S[N + i]);
      else
        s1.push_back(S[N + i]);
    ans += mp[make_pair(s1, s2)];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03298 (C++)

Buggy submission (status: Runtime Error):
/*input
4
cabaacba

*/

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define mx 22
int MOD = 902014037;

ll pw[mx];
int prm = 103;
int n;
char str[mx];

bool isprime(int n) {
  for (int i = 2; 1ll * i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}

map<pair<int, int>, int> mp, mp2;

void rec(int pos, int hash1, int len1, int hash2) {
  if (pos == n) {
    mp[{hash1, hash2}]++;
    return;
  }
  int tmp = hash1;
  hash1 = hash1 + pw[len1] * str[pos] % MOD;
  hash1 %= MOD;
  rec(pos + 1, hash1, len1 + 1, hash2);
  hash2 = (1ll * hash2 * prm + str[pos]) % MOD;
  rec(pos + 1, tmp, len1, hash2);
}

void rec2(int pos, int hash1, int len1, int hash2) {
  if (pos == n * 2) {
    mp2[{hash2, hash1}]++;
    return;
  }
  int tmp = hash1;
  hash1 = hash1 + pw[len1] * str[pos] % MOD;
  hash1 %= MOD;
  rec2(pos + 1, hash1, len1 + 1, hash2);
  hash2 = (1ll * hash2 * prm + str[pos]) % MOD;
  rec2(pos + 1, tmp, len1, hash2);
}

int main() {
  for (int i = 8; i >= 0; i--)
    while (isprime(MOD) == 0)
      MOD--;

  scanf("%d", &n);
  scanf("%s", str);

  pw[0] = 1;
  for (int i = 1; i < mx; i++) {
    pw[i] = pw[i - 1] * prm % MOD;
  }
  rec(0, 0, 0, 0);
  rec2(n, 0, 0, 0);
  ll ans = 0;
  for (auto p : mp) {
    ans += 1ll * mp2[p.first] * p.second;
  }
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
/*input
4
cabaacba

*/

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define mx 22
int MOD = 902014037;

ll pw[mx];
int prm = 103;
int n;
char str[mx * 2];

bool isprime(int n) {
  for (int i = 2; 1ll * i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}

map<pair<int, int>, int> mp, mp2;

void rec(int pos, int hash1, int len1, int hash2) {
  if (pos == n) {
    mp[{hash1, hash2}]++;
    return;
  }
  int tmp = hash1;
  hash1 = hash1 + pw[len1] * str[pos] % MOD;
  hash1 %= MOD;
  rec(pos + 1, hash1, len1 + 1, hash2);
  hash2 = (1ll * hash2 * prm + str[pos]) % MOD;
  rec(pos + 1, tmp, len1, hash2);
}

void rec2(int pos, int hash1, int len1, int hash2) {
  if (pos == n * 2) {
    mp2[{hash2, hash1}]++;
    return;
  }
  int tmp = hash1;
  hash1 = hash1 + pw[len1] * str[pos] % MOD;
  hash1 %= MOD;
  rec2(pos + 1, hash1, len1 + 1, hash2);
  hash2 = (1ll * hash2 * prm + str[pos]) % MOD;
  rec2(pos + 1, tmp, len1, hash2);
}

int main() {
  for (int i = 8; i >= 0; i--)
    while (isprime(MOD) == 0)
      MOD--;

  scanf("%d", &n);
  scanf("%s", str);

  pw[0] = 1;
  for (int i = 1; i < mx; i++) {
    pw[i] = pw[i - 1] * prm % MOD;
  }
  rec(0, 0, 0, 0);
  rec2(n, 0, 0, 0);
  ll ans = 0;
  for (auto p : mp) {
    ans += 1ll * mp2[p.first] * p.second;
  }
  printf("%lld\n", ans);

  return 0;
}
Problem p03298 (C++)

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

map<pair<string, string>, ll> M;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < (1 << n); ++i) {
    string red = "", blue = "";
    for (int j = 0; j < n; ++j) {
      if (i & (1 << j))
        red += s[j];
      else
        blue += s[j];
    }
    ++M[make_pair(red, blue)];
  }
  ll ans = 0;
  for (int i = 0; i < (1 << n); ++i) {
    string red = "", blue = "";
    for (int j = 0; j < (1 << n); ++j) {
      if (1 & (1 << j))
        red += s[j + n];
      else
        blue += s[j + n];
    }
    reverse(red.begin(), red.end());
    reverse(blue.begin(), blue.end());
    ans += M[make_pair(blue, red)];
  }
  cout << ans << endl;
}

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

map<pair<string, string>, ll> M;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < (1 << n); ++i) {
    string red = "", blue = "";
    for (int j = 0; j < n; ++j) {
      if (i & (1 << j))
        red += s[j];
      else
        blue += s[j];
    }
    ++M[make_pair(red, blue)];
  }
  ll ans = 0;
  for (int i = 0; i < (1 << n); ++i) {
    string red = "", blue = "";
    for (int j = 0; j < n; ++j) {
      if (i & (1 << j))
        red += s[j + n];
      else
        blue += s[j + n];
    }
    reverse(red.begin(), red.end());
    reverse(blue.begin(), blue.end());
    ans += M[make_pair(blue, red)];
  }
  cout << ans << endl;
}
Problem p03298 (C++)

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

using namespace std;
const int maxn = 18;
const int base = 31337;

int n;
char niz[maxn];
map<pair<int, int>, int> s;

int main() {
  scanf("%d%s", &n, niz);
  for (int i = 0; i < (1 << n); i++) {
    int hasc = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j))
        hasc *= base, hasc += niz[j];

    int hasp = 0;
    for (int j = n - 1; j >= 0; j--)
      if (!(i & (1 << j)))
        hasp *= base, hasp += niz[j];
    s[make_pair(hasc, hasp)]++;
  }

  long long sol = 0;
  for (int i = 0; i < (1 << n); i++) {
    int hasc = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j))
        hasc *= base, hasc += niz[j + n];

    int hasp = 0;
    for (int j = n - 1; j >= 0; j--)
      if (!(i & (1 << j)))
        hasp *= base, hasp += niz[j + n];

    sol += s[make_pair(hasp, hasc)];
  }
  printf("%lld", sol);
  return 0;
}

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

using namespace std;
const int maxn = 40;
const int base = 31337;

int n;
char niz[maxn];
map<pair<int, int>, int> s;

int main() {
  scanf("%d%s", &n, niz);
  for (int i = 0; i < (1 << n); i++) {
    int hasc = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j))
        hasc *= base, hasc += niz[j];

    int hasp = 0;
    for (int j = n - 1; j >= 0; j--)
      if (!(i & (1 << j)))
        hasp *= base, hasp += niz[j];
    s[make_pair(hasc, hasp)]++;
  }

  long long sol = 0;
  for (int i = 0; i < (1 << n); i++) {
    int hasc = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j))
        hasc *= base, hasc += niz[j + n];

    int hasp = 0;
    for (int j = n - 1; j >= 0; j--)
      if (!(i & (1 << j)))
        hasp *= base, hasp += niz[j + n];

    sol += s[make_pair(hasp, hasc)];
  }
  printf("%lld", sol);
  return 0;
}
Problem p03298 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (long long)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
#define fs first
#define sc second
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int n;
string s;
map<int, map<string, map<string, ll>>> dp;
map<int, map<string, map<string, bool>>> flag;

ll solve(int idx, string r, string b) {
  if (idx >= n * 2) {
    reverse(b.begin(), b.end());
    if (r == b)
      return 1;
    else
      return 0;
  }
  if (flag[idx][r][b])
    return dp[idx][r][b];
  flag[idx][r][b] = true;
  ll res = 0;
  if (idx < n) {
    res += solve(idx + 1, r + s[idx], b);
    res += solve(idx + 1, r, b + s[idx]);
  } else {
    //
    if (r.size() + 1 <= n and s[idx] == b[n - r.size() - 1])
      res += solve(idx + 1, r + s[idx], b);
    if (b.size() + 1 <= n and s[idx] == r[n - b.size() - 1])
      res += solve(idx + 1, r, b + s[idx]);
  }
  return (dp[idx][r][b] = res);
}

int main() {
  cin >> n >> s;
  cout << solve(0, "", "") << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (long long)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
#define fs first
#define sc second
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int n;
string s;
map<int, map<string, map<string, ll>>> dp;
map<int, map<string, map<string, bool>>> flag;

ll solve(int idx, string r, string b) {
  if (idx >= n * 2) {
    reverse(b.begin(), b.end());
    if (r == b)
      return 1;
    else
      return 0;
  }
  if (flag[idx][r][b])
    return dp[idx][r][b];
  flag[idx][r][b] = true;
  ll res = 0;
  if (idx < n) {
    res += solve(idx + 1, r + s[idx], b);
    res += solve(idx + 1, r, b + s[idx]);
  } else {
    //
    if (r.size() + 1 <= n and s[idx] == b[n - r.size() - 1])
      res += solve(idx + 1, r + s[idx], b);
    if (b.size() + 1 <= n and s[idx] == r[n - b.size() - 1])
      res += solve(idx + 1, r, b + s[idx]);
  }
  return (dp[idx][r][b] = res);
}

int main() {
  cin >> n >> s;
  cout << solve(0, "", "") << endl;
  return 0;
}
Problem p03299 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const ll MOD = 1e9 + 7;
int n, sa[105], rnk[105];
ll k, h[105], s[105], f0;
ll quick_pow(ll x, ll a) {
  ll ans = 1;
  while (a) {
    if (a & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    a >>= 1;
  }
  return ans;
}
bool cmp(int a, int b) { return h[a] < h[b]; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &h[i]);
  for (int i = 1; i <= n; i++) {
    sa[i] = i;
    if (h[i] > max(h[i - 1], h[i + 1])) {
      k += h[i] - max(h[i - 1], h[i + 1]);
      h[i] = max(h[i - 1], h[i + 1]);
    }
  }
  sort(sa + 1, sa + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    rnk[sa[i]] = i;
  f0 = 1;
  h[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (rnk[i] < rnk[i - 1])
      f0 = (f0 * 2 + (s[rnk[i - 1]] - s[rnk[i]] + MOD) * 2) % MOD;
    else {
      for (int j = 1; j <= rnk[i - 1]; j++)
        s[j] = s[j] * quick_pow(2, h[i] - h[i - 1]) % MOD;
      for (int j = rnk[i - 1] + 1; j <= rnk[i]; j++)
        s[j] = (s[j - 1] + f0 * 2 *
                               (quick_pow(2, h[i] - h[sa[j - 1]]) -
                                quick_pow(2, h[i] - h[sa[j]]) + MOD)) %
               MOD;
      f0 = f0 * 2 % MOD;
    }
  }
  ll ans = s[rnk[n]] + f0;
  ans = ans % MOD * quick_pow(2, k) % MOD;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const ll MOD = 1e9 + 7;
int n, sa[105], rnk[105];
ll k, h[105], s[105], f0;
ll quick_pow(ll x, ll a) {
  ll ans = 1;
  while (a) {
    if (a & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    a >>= 1;
  }
  return ans;
}
bool cmp(int a, int b) { return h[a] < h[b]; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &h[i]);
  for (int i = 1; i <= n; i++) {
    sa[i] = i;
    if (n != 1 && h[i] > max(h[i - 1], h[i + 1])) {
      k += h[i] - max(h[i - 1], h[i + 1]);
      h[i] = max(h[i - 1], h[i + 1]);
    }
  }
  sort(sa + 1, sa + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    rnk[sa[i]] = i;
  f0 = 1;
  h[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (rnk[i] < rnk[i - 1])
      f0 = (f0 * 2 + (s[rnk[i - 1]] - s[rnk[i]] + MOD) * 2) % MOD;
    else {
      for (int j = 1; j <= rnk[i - 1]; j++)
        s[j] = s[j] * quick_pow(2, h[i] - h[i - 1]) % MOD;
      for (int j = rnk[i - 1] + 1; j <= rnk[i]; j++)
        s[j] = (s[j - 1] + f0 * 2 *
                               (quick_pow(2, h[i] - h[sa[j - 1]]) -
                                quick_pow(2, h[i] - h[sa[j]]) + MOD)) %
               MOD;
      f0 = f0 * 2 % MOD;
    }
  }
  ll ans = s[rnk[n]] + f0;
  ans = ans % MOD * quick_pow(2, k) % MOD;
  printf("%lld\n", ans);
  return 0;
}
Problem p03300 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using ll = int64_t;
using ld = double;
using ull = uint64_t;
using namespace std;
using namespace __gnu_pbds;

const int MAXN = 6228;
int l[MAXN];
int r[MAXN];
int la[MAXN];
int ra[MAXN];
string f[MAXN];
int n;

void pt(int x) {
  int cur = 0;
  while (r[cur] < x) {
    ++cur;
  }

  while (cur < n) {
    cout << "ab";
    int cb = l[cur];
    while (r[cur] < cb) {
      ++cur;
    }
  }

  cout << "\n";
  exit(0);
}

int main() {
#ifdef BZ
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(20);

  string s;
  cin >> n >> s;
  int lc = 0, rc = 0;

  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == 'b') {
      l[lc++] = i;
    } else {
      r[rc++] = i;
    }
  }

  int ac = 0;

  for (int i = 0; i < n; ++i) {
    if (l[i] < r[i]) {
      la[ac] = l[i];
      ra[ac] = r[i];
      ++ac;
    }
  }

  la[ac] = ra[ac] = MAXN;

  if (!ac) {
    pt(0);
  }

  f[ac] = "";
  for (int i = ac - 1; i >= 0; --i) {
    int lp = i, rp = i;
    string cur;
    do {
      if (la[lp] < ra[rp]) {
        cur += 'b';
        ++lp;
      } else {
        cur += 'a';
        ++rp;
      }
    } while (lp != rp);

    cur += f[lp];
    f[i] = max(f[i + 1], cur);
  }

  cout << f[0];
  pt(ra[ac - 1] + 1);
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using ll = int64_t;
using ld = double;
using ull = uint64_t;
using namespace std;
using namespace __gnu_pbds;

const int MAXN = 6228;
int l[MAXN];
int r[MAXN];
int la[MAXN];
int ra[MAXN];
string f[MAXN];
int n;

void pt(int x) {
  int cur = 0;
  while (r[cur] < x) {
    ++cur;
  }

  while (cur < n) {
    cout << "ab";
    int cb = l[cur];
    while (r[cur] < cb) {
      ++cur;
    }
  }

  cout << "\n";
  exit(0);
}

int main() {
#ifdef BZ
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(20);

  string s;
  cin >> n >> s;
  int lc = 0, rc = 0;

  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == 'b') {
      l[lc++] = i;
    } else {
      r[rc++] = i;
    }
  }

  l[n] = r[n] = MAXN;

  int ac = 0;

  for (int i = 0; i < n; ++i) {
    if (l[i] < r[i]) {
      la[ac] = l[i];
      ra[ac] = r[i];
      ++ac;
    }
  }

  la[ac] = ra[ac] = MAXN;

  if (!ac) {
    pt(0);
  }

  f[ac] = "";
  for (int i = ac - 1; i >= 0; --i) {
    int lp = i, rp = i;
    string cur;
    do {
      if (la[lp] < ra[rp]) {
        cur += 'b';
        ++lp;
      } else {
        cur += 'a';
        ++rp;
      }
    } while (lp != rp);

    cur += f[lp];
    f[i] = max(f[i + 1], cur);
  }

  cout << f[0];
  pt(ra[ac - 1] + 1);
}
Problem p03300 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
#define lP pair<ll, ll>
#define iP pair<int, int>
typedef long long ll;
using namespace std;

struct T {
  int l, r;
  string s;
};

int N, r[3000], b[3000], cr, cb, R;
string S;
vector<T> v;
deque<string> q;

int main() {
  cin >> N >> S;
  for (int i = 0; i < N * 2; ++i) {
    if (S[i] == 'a') {
      r[cr] = i;
      ++cr;
    } else {
      b[cb] = i;
      ++cb;
    }
  }
  v.push_back({0, 0, ""});
  for (int i = 0; i < N; ++i) {
    if (b[i] < r[i]) {
      if (r[v.back().r] < b[i]) {
        v.push_back({i, i, ""});
      } else {
        v.back().r = i;
      }
    }
  }
  if (r[0] < b[0]) {
    v.erase(v.begin());
  }
  for (int i = 0; i < v.size(); ++i) {
    vector<bool> B(N, false);
    for (int j = v[i].r; j >= v[i].l; --j) {
      B[b[j]] = B[r[j]] = true;
      string s;
      for (int k = b[j]; k <= r[v[i].r]; ++k) {
        if (B[k]) {
          s.push_back(S[k]);
        }
      }
      v[i].s = max(v[i].s, s);
    }
  }
  for (int i = 0; i < v.size(); ++i) {
    while (!q.empty() && q.back() < v[i].s) {
      q.pop_back();
    }
    q.push_back(v[i].s);
    R = r[v[i].r] + 1;
  }
  while (!q.empty()) {
    cout << q.front();
    q.pop_front();
  }
  for (int i = 0; i < N; ++i) {
    if (r[i] < b[i] && r[i] >= R) {
      cout << "ab";
      R = b[i];
    }
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
#define lP pair<ll, ll>
#define iP pair<int, int>
typedef long long ll;
using namespace std;

struct T {
  int l, r;
  string s;
};

int N, r[3000], b[3000], cr, cb, R;
string S;
vector<T> v;
deque<string> q;

int main() {
  cin >> N >> S;
  for (int i = 0; i < N * 2; ++i) {
    if (S[i] == 'a') {
      r[cr] = i;
      ++cr;
    } else {
      b[cb] = i;
      ++cb;
    }
  }
  v.push_back({0, 0, ""});
  for (int i = 0; i < N; ++i) {
    if (b[i] < r[i]) {
      if (r[v.back().r] < b[i]) {
        v.push_back({i, i, ""});
      } else {
        v.back().r = i;
      }
    }
  }
  if (r[0] < b[0]) {
    v.erase(v.begin());
  }
  for (int i = 0; i < v.size(); ++i) {
    vector<bool> B(N * 2, false);
    for (int j = v[i].r; j >= v[i].l; --j) {
      B[b[j]] = B[r[j]] = true;
      string s;
      for (int k = b[j]; k <= r[v[i].r]; ++k) {
        if (B[k]) {
          s.push_back(S[k]);
        }
      }
      v[i].s = max(v[i].s, s);
    }
  }
  for (int i = 0; i < v.size(); ++i) {
    while (!q.empty() && q.back() < v[i].s) {
      q.pop_back();
    }
    q.push_back(v[i].s);
    R = r[v[i].r] + 1;
  }
  while (!q.empty()) {
    cout << q.front();
    q.pop_front();
  }
  for (int i = 0; i < N; ++i) {
    if (r[i] < b[i] && r[i] >= R) {
      cout << "ab";
      R = b[i];
    }
  }
  cout << endl;
}
Problem p03300 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, id[3010], a[3010], b[3010], p[2];
char s[3010];
string f[3010], g[3010];
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  int numa = 0, numb = 0;
  for (int i = 1; i <= n + n; i++)
    if (s[i] == 'a')
      id[i] = ++numa, a[numa] = i;
    else
      id[i] = ++numb, b[numb] = i;
  int now = 0;
  f[0] = "";
  for (int i = n + n, j; i >= 1; i = j - 1) {
    j = i;
    p[0] = p[1] = 0;
    p[s[j] - 'a'] = 1;
    while (p[0] != p[1])
      p[s[--j] - 'a']++;
    f[++now] = "";
    if (s[j] == 'a') {
      int k = j;
      while (k <= i) {
        if (s[k] == 'b') {
          k++;
          continue;
        }
        f[now] = f[now] + "ab";
        k = b[id[k]] + 1;
      }
    } else {
      int S = id[j], T = id[i];
      for (int k = T; k >= S; k--) {
        int x = k;
        g[k] = "";
        while (x < T && b[x + 1] < a[x])
          x++;
        for (int l = j; l <= i; l++)
          if (id[l] >= k && id[l] <= x)
            g[k] = g[k] + s[l];
        if (x + 1 <= T)
          g[k] = g[k] + g[x + 1];
        if (k + 1 < +T)
          g[k] = max(g[k], g[k + 1]);
      }
      f[now] = g[S];
    }
    f[now] = max(f[now] + f[now - 1], f[now - 1]);
  }
  printf("%s\n", f[now].c_str());
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, id[6010], a[6010], b[6010], p[2];
char s[6010];
string f[6010], g[6010];
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  int numa = 0, numb = 0;
  for (int i = 1; i <= n + n; i++)
    if (s[i] == 'a')
      id[i] = ++numa, a[numa] = i;
    else
      id[i] = ++numb, b[numb] = i;
  int now = 0;
  f[0] = "";
  for (int i = n + n, j; i >= 1; i = j - 1) {
    j = i;
    p[0] = p[1] = 0;
    p[s[j] - 'a'] = 1;
    while (p[0] != p[1])
      p[s[--j] - 'a']++;
    f[++now] = "";
    if (s[j] == 'a') {
      int k = j;
      while (k <= i) {
        if (s[k] == 'b') {
          k++;
          continue;
        }
        f[now] = f[now] + "ab";
        k = b[id[k]] + 1;
      }
    } else {
      int S = id[j], T = id[i];
      for (int k = T; k >= S; k--) {
        int x = k;
        g[k] = "";
        while (x < T && b[x + 1] < a[x])
          x++;
        for (int l = j; l <= i; l++)
          if (id[l] >= k && id[l] <= x)
            g[k] = g[k] + s[l];
        if (x + 1 <= T)
          g[k] = g[k] + g[x + 1];
        if (k + 1 < +T)
          g[k] = max(g[k], g[k + 1]);
      }
      f[now] = g[S];
    }
    f[now] = max(f[now] + f[now - 1], f[now - 1]);
  }
  printf("%s\n", f[now].c_str());
  return 0;
}