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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long lcm(long long x, long long y) {
  long long sum = x * y;
  long long tmp = x;
  long long i = y;
  long long ret;

  if (x > y) {
    tmp = y;
    i = x;
  }

  ret = i % tmp;
  while (ret != 0LL) {
    i = tmp;
    tmp = ret;
    ret = i % tmp;
  }

  return (sum / ret);
}

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

  long long a_to = ((a - 1LL) / c) + ((a - 1LL) / d) - ((a - 1LL) / lcm(c, d));
  long long b_to = (b / c) + (b / d) - (b / lcm(c, d));

  long long ret = (b - a + 1LL) - (b_to - a_to);

  cout << ret << endl;
}


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

long long lcm(long long x, long long y) {
  long long sum = x * y;
  long long tmp = x;
  long long i = y;
  long long ret;

  if (x > y) {
    tmp = y;
    i = x;
  }

  ret = i % tmp;
  while (ret != 0LL) {
    i = tmp;
    tmp = ret;
    ret = i % tmp;
  }

  return (sum / tmp);
}

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

  long long a_to = ((a - 1LL) / c) + ((a - 1LL) / d) - ((a - 1LL) / lcm(c, d));
  long long b_to = (b / c) + (b / d) - (b / lcm(c, d));

  long long ret = (b - a + 1LL) - (b_to - a_to);

  cout << ret << endl;
}
Problem p02995 (C++)

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

int main() {
  long long A, B, C, D;
  cin >> A >> B >> C >> D;
  long long Cdiv = B / C - (A - 1) / C;
  long long Ddiv = B / D - (A - 1) / D;
  long long CDdiv;
  long long minCD = min(C, D);
  long long lcm;
  long long gcd = 1;
  long long Cs = C;
  long long Ds = D;
  int i = 2;
  while (i < Cs + 1 && i < Ds + 1) {
    while (Cs % i == 0 && Ds % i == 0) {
      gcd *= i;
      Cs /= i;
      Ds /= i;
    }
    i++;
  }
  lcm = C * D / gcd;
  CDdiv = B / lcm - (A - 1) / lcm;
  cout << (B - A + 1) - (Cdiv + Ddiv - CDdiv) << endl;
  return 0;
}

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

int main() {
  long long A, B, C, D;
  cin >> A >> B >> C >> D;
  long long Cdiv = B / C - (A - 1) / C;
  long long Ddiv = B / D - (A - 1) / D;
  long long CDdiv;
  long long minCD = min(C, D);
  long long lcm;
  long long gcd = 1;
  long long Cs = C;
  long long Ds = D;
  int i = 2;
  while (i < sqrt(Cs) + 1 && i < sqrt(Ds) + 1) {
    while (Cs % i == 0 && Ds % i == 0) {
      gcd *= i;
      Cs /= i;
      Ds /= i;
    }
    i++;
  }
  lcm = C * D / gcd;
  CDdiv = B / lcm - (A - 1) / lcm;
  cout << (B - A + 1) - (Cdiv + Ddiv - CDdiv) << endl;
  return 0;
}
Problem p02995 (C++)

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

int gcd(int x, int y) {
  if (y)
    return gcd(y, x % y);
  else
    return x;
}

int main(void) {
  long long a, b, po = 0;
  int c, d, t;
  po += b / c - (a - 1) / c;
  po += b / d - (a - 1) / d;
  t = c * d / gcd(c, d);
  po -= b / t - (a - 1) / t;
  cout << b - a + 1 - po << endl;
  return 0;
}

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

int gcd(int x, int y) {
  if (y)
    return gcd(y, x % y);
  else
    return x;
}

int main(void) {
  long long a, b, po = 0, c, d, t;
  cin >> a >> b >> c >> d;
  po += b / c - (a - 1) / c;
  po += b / d - (a - 1) / d;
  t = c * d / gcd(c, d);
  po -= b / t - (a - 1) / t;
  cout << b - a + 1 - po << endl;
  return 0;
}
Problem p02995 (C++)

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

ll Pow(ll a, ll b, ll c) {
  ll ans = 1;
  a %= c;
  while (b) {
    if (b & 1)
      ans = (ans * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return (ans % c);
}

bool cmp(const int &a, const int &b) { return a < b; }

ll gcd(ll x, ll y) { return x % y == 0 ? y : gcd(y, x % y); }

ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {
  ll a, b, c, d;
  scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
  ll tmp = lcm(c, d);
  ll cnt = 0;
  ll i, j, k;
  if (a % tmp)
    i = (a / tmp + 1) * tmp;
  else
    i = a;
  if (a % c)
    j = (a / c + 1) * c;
  else
    j = a;
  if (a % d)
    k = (a / d + 1) * d;
  else
    k = a;
  for (; i <= b; i += tmp)
    cnt--;
  for (; j <= b; j += c)
    cnt++;
  for (; k <= b; k += d)
    cnt++;
  printf("%lld\n", b - a + 1 - cnt);
  return 0;
}


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

ll Pow(ll a, ll b, ll c) {
  ll ans = 1;
  a %= c;
  while (b) {
    if (b & 1)
      ans = (ans * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return (ans % c);
}

bool cmp(const int &a, const int &b) { return a < b; }

ll gcd(ll x, ll y) { return x % y == 0 ? y : gcd(y, x % y); }

ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {
  ll a, b, c, d;
  scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
  ll tmp = lcm(c, d);
  ll t1 = b / c - (a - 1) / c;
  ll t2 = b / d - (a - 1) / d;
  ll t3 = b / tmp - (a - 1) / tmp;
  printf("%lld\n", b - a + 1 - t1 - t2 + t3);
  return 0;
}
Problem p02995 (Python)

Buggy submission (status: Runtime Error):
from math import gcd


def main():
    A, B, C, D = map(int, input().split())
    Q_AC, R_AC = A // C, A % C
    Q_AD, R_AD = A // D, A % D
    Q_BC, R_BC = B // C, B % C
    Q_BD, R_BD = B // D, B % D
    Q_ACD, R_ACD = A // lcm(C, D), A % lcm(C, D)
    Q_BCD, R_BCD = B // lcm(C, D), B % lcm(C, D)
    N_C = num_baisu(Q_AC, Q_BC, R_AC, R_BC)
    N_D = num_baisu(Q_AD, Q_BD, R_AD, R_BD)
    N_CD = num_baisu(Q_ACD, Q_BCD, R_ACD, R_BCD)
    print(B - A + 1 - (N_C + N_D - N_CD))
    return


def lcm(x, y):
    return (x * y) // gcd(x, y)


def num_baisu(Q_A, Q_B, R_A, R_B):
    if R_A == 0 and R_B == 0:
        return Q_B - Q_A + 1
    elif R_A == 0 and R_B != 0:
        return Q_B - Q_A + 1
    elif R_A != 0 and R_B == 0:
        return Q_B - (Q_A + 1) + 1
    else:
        return Q_B - (Q_A + 1) + 1


if __name__ == "__main__":
    main()


Fixed submission:
from fractions import gcd


def main():
    A, B, C, D = map(int, input().split())
    Q_AC, R_AC = A // C, A % C
    Q_AD, R_AD = A // D, A % D
    Q_BC, R_BC = B // C, B % C
    Q_BD, R_BD = B // D, B % D
    Q_ACD, R_ACD = A // lcm(C, D), A % lcm(C, D)
    Q_BCD, R_BCD = B // lcm(C, D), B % lcm(C, D)
    N_C = num_baisu(Q_AC, Q_BC, R_AC, R_BC)
    N_D = num_baisu(Q_AD, Q_BD, R_AD, R_BD)
    N_CD = num_baisu(Q_ACD, Q_BCD, R_ACD, R_BCD)
    print(B - A + 1 - (N_C + N_D - N_CD))
    return


def lcm(x, y):
    return (x * y) // gcd(x, y)


def num_baisu(Q_A, Q_B, R_A, R_B):
    if R_A == 0 and R_B == 0:
        return Q_B - Q_A + 1
    elif R_A == 0 and R_B != 0:
        return Q_B - Q_A + 1
    elif R_A != 0 and R_B == 0:
        return Q_B - (Q_A + 1) + 1
    else:
        return Q_B - (Q_A + 1) + 1


if __name__ == "__main__":
    main()
Problem p02995 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

A, B, C, D = [int(x) for x in input().split(" ")]

c = (B // C) - (A // C) + int(A % C == 0)
d = (B // D) - (A // D) + int(A % D == 0)
lcm_cd = (C * D) // gcd(C, D)
cd = (B // (lcm_cd)) - (A // (lcm_cd)) + int(A % (lcm_cd) == 0)

c_or_d = c + d - cd

print((B - A + 1) - c_or_d)


Fixed submission:
from fractions import gcd

A, B, C, D = [int(x) for x in input().split(" ")]

c = (B // C) - (A // C) + int(A % C == 0)
d = (B // D) - (A // D) + int(A % D == 0)
lcm_cd = (C * D) // gcd(C, D)
cd = (B // (lcm_cd)) - (A // (lcm_cd)) + int(A % (lcm_cd) == 0)

c_or_d = c + d - cd

print((B - A + 1) - c_or_d)
Problem p02995 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

A, B, C, D = map(int, input().split())
C_div = B // C - (A - 1) // C
D_div = B // D - (A - 1) // D
CD = C * D // gcd(C, D)
CD_div = B // CD - (A - 1) // CD
print(B - A + 1 - C_div - D_div + CD_div)


Fixed submission:
from fractions import gcd

A, B, C, D = map(int, input().split())
C_div = B // C - (A - 1) // C
D_div = B // D - (A - 1) // D
CD = C * D // gcd(C, D)
CD_div = B // CD - (A - 1) // CD
print(B - A + 1 - C_div - D_div + CD_div)
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e18
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)

int gcd(int x, int y) {
  if (max(x, y) % min(x, y) == 0)
    return y;
  return gcd(min(x, y), max(x, y) % min(x, y));
}

int lcm(int x, int y) { return x / gcd(x, y) * y; }

int func(int x, int c, int d) { return x - (x / c + x / d - x / (lcm(c, d))); }

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

  int ans = func(b, c, d) - func(a - 1, c, d);

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e18
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)

int gcd(int x, int y) { return (y ? gcd(y, x % y) : x); }

int lcm(int x, int y) { return x / gcd(x, y) * y; }

int func(int x, int c, int d) { return x - (x / c + x / d - x / (lcm(c, d))); }

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

  int ans = func(b, c, d) - func(a - 1, c, d);

  cout << ans << endl;
}
Problem p02995 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

A, B, C, D = map(int, input().split())
G = gcd(C, D)
L = C * D // G
ca = (A - 1) // C
cb = B // C
da = (A - 1) // D
db = B // D
la = (A - 1) // L
lb = B // L
div = cb + db - (ca + da) - (lb - la)
print(B - (A - 1) - div)


Fixed submission:
from fractions import gcd

A, B, C, D = map(int, input().split())
G = gcd(C, D)
L = C * D // G
ca = (A - 1) // C
cb = B // C
da = (A - 1) // D
db = B // D
la = (A - 1) // L
lb = B // L
div = cb + db - (ca + da) - (lb - la)
print(B - (A - 1) - div)
Problem p02995 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

a, b, c, d = map(int, input().split())

e = (c * d) // gcd(c, d)

c_min = a + (c - a % c) if (a % c) != 0 else a
c_max = b - b % c
d_min = a + (d - a % d) if (a % d) != 0 else a
d_max = b - b % d
e_min = a + (e - a % e) if (a % e) != 0 else a
e_max = b - b % e

n_c = (c_max - c_min) // c + 1 if c_min <= c_max else 0
n_d = (d_max - d_min) // d + 1 if d_min <= d_max else 0
n_e = (e_max - e_min) // e + 1 if e_min <= e_max else 0

ans = b - a + 1 - n_c - n_d + n_e

print(ans)


Fixed submission:
from fractions import gcd

a, b, c, d = map(int, input().split())

e = (c * d) // gcd(c, d)

c_min = a + (c - a % c) if (a % c) != 0 else a
c_max = b - b % c
d_min = a + (d - a % d) if (a % d) != 0 else a
d_max = b - b % d
e_min = a + (e - a % e) if (a % e) != 0 else a
e_max = b - b % e

n_c = (c_max - c_min) // c + 1 if c_min <= c_max else 0
n_d = (d_max - d_min) // d + 1 if d_min <= d_max else 0
n_e = (e_max - e_min) // e + 1 if e_min <= e_max else 0

ans = b - a + 1 - n_c - n_d + n_e

print(ans)
Problem p02995 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

a, b, c, d = map(int, input().split())


def cnt_div(a, b, x):
    q, r = divmod(a, x)

    if r == 0:
        m = q - 1
    else:
        m = q

    n = b // x

    return n - m


num_c = cnt_div(a, b, c)
num_d = cnt_div(a, b, d)
lcm = int((c * d) / gcd(c, d))
num_c_d_gcd = cnt_div(a, b, lcm)

print((b - a + 1) - (num_c + num_d - num_c_d_gcd))


Fixed submission:
from fractions import gcd

a, b, c, d = map(int, input().split())


def cnt_div(a, b, x):
    q, r = divmod(a, x)

    if r == 0:
        m = q - 1
    else:
        m = q

    n = b // x

    return n - m


num_c = cnt_div(a, b, c)
num_d = cnt_div(a, b, d)
lcm = int((c * d) / gcd(c, d))
num_c_d_gcd = cnt_div(a, b, lcm)

print((b - a + 1) - (num_c + num_d - num_c_d_gcd))
Problem p02995 (C++)

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

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, n) for (long long i = (s); i < (long long)(n); i++)
#define repi(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2i(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()

#define deg2rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad2deg(rad) (((rad) / 2 / M_PI) * 360)

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

const ll INF = (1LL << 60);
const int INFi = (1 << 29);

/*素数判定*/
bool is_prime(ll n) {
  if (n == 1)
    return false;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

/*約数列挙*/
vll enum_divisors(ll n) {
  vll l;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      l.push_back(i);
      if (n / i != i)
        l.push_back(n / i);
    }
  }
  sort(all(l));
  return l;
}

/*素因数分解*/
vector<P> prime_factorize(ll n) {
  vector<P> l;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    ll e = 0;
    while (n % i == 0) {
      e++;
      n /= i;
    }
    l.push_back({i, e});
  }
  if (n != 1)
    l.push_back({n, 1});
  return l;
}

int main() {

  ll a, b, c, d;
  cin >> a >> b >> c >> d;

  ll cnt_a = 0, cnt_b = 0;
  rep(i, a) if (i % c != 0 && i % d != 0) cnt_a++;
  rep(i, b + 1) if (i % c != 0 && i % d != 0) cnt_b++;

  cout << cnt_b - cnt_a << endl;

  return 0;
}

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

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, n) for (long long i = (s); i < (long long)(n); i++)
#define repi(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2i(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()

#define deg2rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad2deg(rad) (((rad) / 2 / M_PI) * 360)

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

const ll INF = (1LL << 60);
const int INFi = (1 << 29);

/*素数判定*/
bool is_prime(ll n) {
  if (n == 1)
    return false;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

/*約数列挙*/
vll enum_divisors(ll n) {
  vll l;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      l.push_back(i);
      if (n / i != i)
        l.push_back(n / i);
    }
  }
  sort(all(l));
  return l;
}

/*素因数分解*/
vector<P> prime_factorize(ll n) {
  vector<P> l;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    ll e = 0;
    while (n % i == 0) {
      e++;
      n /= i;
    }
    l.push_back({i, e});
  }
  if (n != 1)
    l.push_back({n, 1});
  return l;
}

int main() {

  ll a, b, c, d;
  cin >> a >> b >> c >> d;

  a--;
  cout << (b - ((b / c + b / d) - (b / (c * d / __gcd(c, d))))) -
              (a - ((a / c + a / d) - (a / (c * d / __gcd(c, d)))))
       << endl;

  return 0;
}
Problem p02995 (C++)

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

long long int gcd(long long int x, long long int y) {
  long long int tmp;
  long long int r;
  if (x > y) {
    tmp = x;
    x = y;
    y = tmp;
  }
  r = y % x;
  while (r != 0) {
    y = x;
    x = r;
    r = y % x;
  }
  return x;
}

int main() {
  long long int a, b, c, d, i;
  long long int ans = (b - a + 1) - (b / c - a / c) - (b / d - a / d);
  long long int g = gcd(c, d);
  ans += (b / g - a / g);
  cout << ans << endl;
}

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

long long int gcd(long long int x, long long int y) {
  long long int tmp;
  long long int r;
  if (x > y) {
    tmp = x;
    x = y;
    y = tmp;
  }
  r = y % x;
  while (r != 0) {
    y = x;
    x = r;
    r = y % x;
  }
  return x;
}

int main() {
  long long int a, b, c, d, i;
  cin >> a >> b >> c >> d;
  long long int ans =
      (b - a + 1) - (b / c - (a - 1) / c) - (b / d - (a - 1) / d);
  long long int g = c * d / gcd(c, d);
  ans += (b / g - (a - 1) / g);
  cout << ans << endl;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iomanip>  //小数点以下を表示させる(setprecision())
#include <iostream> //入出力
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define debug(x)                                                               \
  cerr << x << " "                                                             \
       << "(L:" << __LINE__ << ")" << '\n'

// DP法
template <class T>
inline bool chmin(T &a, T b) // change minimum
{
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) // change max
{
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

typedef long long ll; // 64bit型
using namespace std;

// const long long INF = 1LL << 60; //無限大の値
int INF = 1000000;

ll high_gcd(ll a, ll b) {
  if (b == 0)
    return 0;
  return high_gcd(b, a % b);
}

ll kosuu(ll a, ll b, ll c) {
  ll d = (a + c - 1) / c;
  ll e = b / c;

  ll ans = e - d + 1;

  return ans;
}

int main() {

  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  if (C < D) {
    swap(C, D);
  }

  ll gcd = high_gcd(C, D);
  ll lcm = C / gcd * D;

  ll ans = B - A + 1;

  ll kazu = 0;

  kazu = kosuu(A, B, C);
  ans -= kazu;

  kazu = kosuu(A, B, D);
  ans -= kazu;

  kazu = kosuu(A, B, lcm);
  ans += kazu;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iomanip>  //小数点以下を表示させる(setprecision())
#include <iostream> //入出力
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define debug(x)                                                               \
  cerr << x << " "                                                             \
       << "(L:" << __LINE__ << ")" << '\n'

// DP法
template <class T>
inline bool chmin(T &a, T b) // change minimum
{
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) // change max
{
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

typedef long long ll; // 64bit型
using namespace std;

// const long long INF = 1LL << 60; //無限大の値
int INF = 1000000;

ll high_gcd(ll a, ll b) { return b ? high_gcd(b, a % b) : a; }

ll kosuu(ll a, ll b, ll c) {
  ll d = (a + c - 1) / c;
  ll e = b / c;

  ll ans = e - d + 1;

  return ans;
}

int main() {

  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  if (C < D) {
    swap(C, D);
  }

  ll gcd = high_gcd(C, D);
  ll lcm = C / gcd * D;

  ll ans = B - A + 1;

  ll kazu = 0;

  kazu = kosuu(A, B, C);
  ans -= kazu;

  kazu = kosuu(A, B, D);
  ans -= kazu;

  kazu = kosuu(A, B, lcm);
  ans += kazu;
  cout << ans << endl;

  return 0;
}
Problem p02995 (C++)

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

using namespace std;

int main() {
  int64_t a, b, c, d, g, x = 0;
  cin >> a >> b >> c >> d;

  for (int64_t h = min(c, d) / 2; h > 0; h--) {
    if (c % h == 0 && d % h == 0) {
      g = h;
      break;
    }
  }

  x = c * d / g;

  cout << (b - a + 1) - (b / c + b / d + (a - 1) / x) +
              ((a - 1) / c + (a - 1) / d + b / x)
       << endl;
}

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

using namespace std;

int main() {
  int64_t a, b, c, d, g, x = 0;
  cin >> a >> b >> c >> d;

  x = c * d / __gcd(c, d);

  cout << (b - a + 1) - (b / c + b / d + (a - 1) / x) +
              ((a - 1) / c + (a - 1) / d + b / x)
       << endl;
}
Problem p02995 (C++)

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

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  ll r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  ll cnt_b = 0;
  ll cnt_a = 0;
  ll t_a = 0, t_b = 0;
  ll g = gcd(C, D);
  ll l = C * D / g;
  for (ll i = l; i <= B; i += l) {
    t_b++;
  }
  for (ll i = l; i < A; i += l) {
    t_a++;
  }

  ll num_c = B / C;
  ll num_d = B / D;
  cnt_b = B - num_c - num_d + t_b;
  ll c = (A - 1) / C;
  ll d = (A - 1) / D;
  cnt_a = A - 1 - c - d + t_a;
  cout << cnt_b - cnt_a << endl;

  return 0;
}


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

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  ll r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  ll cnt_b = 0;
  ll cnt_a = 0;
  ll t_a = 0, t_b = 0;
  ll g = gcd(C, D);
  ll l = C * D / g;
  t_b = B / l;
  t_a = (A - 1) / l;
  ll num_c = B / C;
  ll num_d = B / D;
  cnt_b = B - num_c - num_d + t_b;
  ll c = (A - 1) / C;
  ll d = (A - 1) / D;
  cnt_a = A - 1 - c - d + t_a;
  cout << cnt_b - cnt_a << endl;

  return 0;
}
Problem p02995 (C++)

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

template <typename T> T gcd(T a, T b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

void Main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long g = gcd(c, d);
  long long l = c / g * d;
  long long ans = 0;
  for (long long i = l * (a / l + 1); i <= l * (b / l + 1); i += l) {
    long long diff = min(b, i) - max(a, i - l);
    ans += diff;
    ans -= diff / c;
    ans -= diff / d;
    if (diff == l)
      ans++;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


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

template <typename T> T gcd(T a, T b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

void Main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long g = gcd(c, d);
  long long l = c / g * d;
  long long bb = b - b / c - b / d + b / l;
  a--;
  long long aa = a - a / c - a / d + a / l;
  cout << bb - aa << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
// #include "graph.cpp"
#define ll long long
#define loop(i, n) for (int i = 0; i < n; ++i)
#define rep(i, x, n) for (int i = x; i <= n; ++i)
#define iteloop(type, data, name, it)                                          \
  for (type<data>::iterator it = name.begin(); it != name.end(); ++it)
using namespace std;
using namespace __gnu_pbds;
ll a, b, c, d;
// Recursive function to return gcd of a and b
ll gcd(ll an, ll bn) {
  if (bn == 0)
    return an;
  return gcd(bn, an % bn);
}
int main() {
  cin.sync_with_stdio(false), cin.tie(0), cout.tie(0);
  freopen("in.txt", "rt", stdin);
  cin >> a >> b >> c >> d;
  ll div1 = a / c;
  ll div2 = b / c;
  ll divC = div2 - div1;
  if (a % c == 0) {
    divC++;
  }
  div1 = a / d;
  div2 = b / d;
  ll divD = div2 - div1;
  if (a % d == 0) {
    divD++;
  }
  ll gcdNum = gcd(c, d);
  ll lcm = (c * d) / gcdNum;

  div1 = a / lcm;
  div2 = b / lcm;
  ll divLcm = div2 - div1;
  if (a % lcm == 0) {
    divLcm++;
  }
  ll range = b - a + 1;
  range -= divC;
  range -= divD;
  range += divLcm;
  cout << range << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
// #include "graph.cpp"
#define ll long long
#define loop(i, n) for (int i = 0; i < n; ++i)
#define rep(i, x, n) for (int i = x; i <= n; ++i)
#define iteloop(type, data, name, it)                                          \
  for (type<data>::iterator it = name.begin(); it != name.end(); ++it)
using namespace std;
using namespace __gnu_pbds;
ll a, b, c, d;
// Recursive function to return gcd of a and b
ll gcd(ll an, ll bn) {
  if (bn == 0)
    return an;
  return gcd(bn, an % bn);
}
int main() {
  cin.sync_with_stdio(false), cin.tie(0), cout.tie(0);
  //  freopen("in.txt","rt",stdin);
  cin >> a >> b >> c >> d;
  ll div1 = a / c;
  ll div2 = b / c;
  ll divC = div2 - div1;
  if (a % c == 0) {
    divC++;
  }
  div1 = a / d;
  div2 = b / d;
  ll divD = div2 - div1;
  if (a % d == 0) {
    divD++;
  }
  ll gcdNum = gcd(c, d);
  ll lcm = (c * d) / gcdNum;

  div1 = a / lcm;
  div2 = b / lcm;
  ll divLcm = div2 - div1;
  if (a % lcm == 0) {
    divLcm++;
  }
  ll range = b - a + 1;
  range -= divC;
  range -= divD;
  range += divLcm;
  cout << range << endl;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void) {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  int count = 0;
  for (int i = a; i <= b; i++) {
    if (i % c != 0 && i % d != 0)
      count++;
  }
  cout << count << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void) {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll div_c = b / c - (a - 1) / c;
  ll div_d = b / d - (a - 1) / d;
  ll count = div_c + div_d - (b / (c * d / (__gcd(c, d)))) +
             ((a - 1) / (c * d / (__gcd(c, d))));

  ll ans = abs(b - a + 1 - count);

  cout << ans << endl;
}
Problem p02995 (C++)

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

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

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll nC = 0, nD = 0, nCD = 0;
  ll i = 1;
  while (i * c <= b) {
    if (i * c >= a)
      ++nC;
    ++i;
  }
  i = 1;
  while (i * d <= b) {
    if (i * d >= a)
      ++nD;
    ++i;
  }
  ll gCD = gcd(c, d);
  i = 1;
  while (i * c * d / gCD <= b) {
    if (i * c * d / gCD >= max({a, c, d}))
      ++nCD;
    ++i;
  }

  ll ans = b - a + 1;
  ans -= nC + nD - nCD;
  cout << ans << endl;

  return 0;
}


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

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

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll nC, nD, nCD;
  nC = b / c - (a - 1) / c;
  nD = b / d - (a - 1) / d;
  nCD = b / (c * d / gcd(c, d)) - (a - 1) / (c * d / gcd(c, d));

  ll ans = b - a + 1;
  ans -= nC + nD - nCD;
  cout << ans << endl;

  return 0;
}
Problem p02995 (C++)

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

using namespace std;

/* a と b の最大公約数を返す関数 */
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

int main() {

  long long A, B, C, D, G;
  long long L;
  long long cnt;

  cin >> A >> B >> C >> D;

  G = GCD(A, B);
  L = C * D / G;

  A = A - 1;
  // cnt = (B - B / C - B / D + B / L)-((A-1)- (A - 1)/C-(A-1)/D+(A-1)/L);
  cnt = (B - B / C - B / D + B / L) - (A - A / C - A / D + A / L);

  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

/* a と b の最大公約数を返す関数 */
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

int main() {

  long long A, B, C, D, G;
  long long L;
  long long cnt;

  cin >> A >> B >> C >> D;

  G = GCD(C, D);
  L = C * D / G;

  A = A - 1;
  // cnt = (B - B / C - B / D + B / L)-((A-1)- (A - 1)/C-(A-1)/D+(A-1)/L);
  cnt = (B - B / C - B / D + B / L) - (A - A / C - A / D + A / L);

  cout << cnt << endl;

  return 0;
}
Problem p02995 (C++)

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

int main() {

  ll a, b, c, d;
  cin >> a >> b >> c >> d;

  if (c < d)
    swap(c, d);

  ll x = c, y = d;
  ll r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }

  ll lcm = c * d / y;
  ll ans = b - a;

  ll i;
  for (i = a; i < a + c; i++) {
    if (i % c == 0) {
      ans -= (b - i) / c;
      break;
    }
  }
  for (i = a; i < a + d; i++) {
    if (i % d == 0) {
      ans -= (b - i) / d;
      break;
    }
  }
  for (i = a; i < a + lcm; i++) {
    if (i % lcm == 0) {
      ans += (b - i) / lcm;
      break;
    }
  }

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

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 main() {

  ll a, b, c, d;
  cin >> a >> b >> c >> d;

  if (c < d)
    swap(c, d);

  ll x = c, y = d;
  ll r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }

  ll lcm = c * d / y;
  ll ans = b - b / c - b / d + b / lcm;
  ans -= (a - 1) - (a - 1) / c - (a - 1) / d + (a - 1) / lcm;

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

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

int main(int argc, char *argv[]) {
  int N;
  std::cin >> N;
  std::map<long int, std::vector<long int>, std::greater<long int>> jobs;
  long int A, B;
  for (int i = 0; i < N; ++i) {
    std::cin >> A >> B;
    if (jobs.find(B) == jobs.end()) {
      jobs[B] = std::vector<long int>{A};
      continue;
    }
    jobs[B].emplace_back(A);
  }

  std::priority_queue<long int> queue;
  auto jobsIter = jobs.begin();
  long int t = (*jobsIter).first;
  for (auto A : (*jobsIter).second) {
    queue.push(A);
  }
  ++jobsIter;
  while (!queue.empty()) {
    t -= queue.top();
    queue.pop();
    if (jobsIter != jobs.end()) {
      while ((*jobsIter).first >= t) {
        for (auto A : (*jobsIter).second) {
          queue.push(A);
        }
        ++jobsIter;
      }
      if (queue.empty()) {
        t = (*jobsIter).first;
        for (auto A : (*jobsIter).second) {
          queue.push(A);
        }
        ++jobsIter;
      }
    }
  }
  if (t >= 0) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }

  return 0;
}


Fixed submission:
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

int main(int argc, char *argv[]) {
  int N;
  std::cin >> N;
  std::map<long int, std::vector<long int>, std::greater<long int>> jobs;
  long int A, B;
  for (int i = 0; i < N; ++i) {
    std::cin >> A >> B;
    if (jobs.find(B) == jobs.end()) {
      jobs[B] = std::vector<long int>{A};
      continue;
    }
    jobs[B].emplace_back(A);
  }

  std::priority_queue<long int> queue;
  auto jobsIter = jobs.begin();
  long int t = (*jobsIter).first;
  for (auto A : (*jobsIter).second) {
    queue.push(A);
  }
  ++jobsIter;
  while (!queue.empty()) {
    t -= queue.top();
    queue.pop();
    if (jobsIter != jobs.end()) {
      while ((*jobsIter).first >= t && jobsIter != jobs.end()) {
        for (auto A : (*jobsIter).second) {
          queue.push(A);
        }
        ++jobsIter;
      }
      if (queue.empty()) {
        t = (*jobsIter).first;
        for (auto A : (*jobsIter).second) {
          queue.push(A);
        }
        ++jobsIter;
      }
    }
  }
  if (t >= 0) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }

  return 0;
}
Problem p02996 (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;
typedef long double ld;
const double pi = acos(-1.0);
const ll mod = pow(10, 9) + 7;
const ll INF = pow(2, 31) - 1;
typedef pair<int, int> P;
typedef vector<int> vi;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  ll lc = (a / g) * b;
  return lc;
}

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int time = 0;
  P p[20005];
  rep(i, n) { cin >> p[i].second >> p[i].first; }
  sort(p, p + n);
  int ind = 0;
  rep(i, n) {
    time += p[i].second;
    if (time > p[i].first) {
      break;
    }
    ind++;
  }
  if (ind == n)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
const double pi = acos(-1.0);
const ll mod = pow(10, 9) + 7;
const ll INF = pow(2, 31) - 1;
typedef pair<int, int> P;
typedef vector<int> vi;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  ll lc = (a / g) * b;
  return lc;
}

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int time = 0;
  P p[200005];
  rep(i, n) { cin >> p[i].second >> p[i].first; }
  sort(p, p + n);
  int ind = 0;
  rep(i, n) {
    time += p[i].second;
    if (time > p[i].first) {
      break;
    }
    ind++;
  }
  if (ind == n)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct node {
  int c, lim;
  bool operator<(const node &y) { return lim < y.lim; }
};
node a[100100];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].c, &a[i].lim);
  }
  sort(a + 1, a + n + 1);
  int cnt = 0;
  int fg = 1;
  for (int i = 1; i <= n; i++) {
    cnt += a[i].c;
    if (cnt > a[i].lim) {
      fg = 0;
      break;
    }
  }
  if (fg) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct node {
  int c, lim;
  bool operator<(const node &y) { return lim < y.lim; }
};
node a[200200];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].c, &a[i].lim);
  }
  sort(a + 1, a + n + 1);
  int cnt = 0;
  int fg = 1;
  for (int i = 1; i <= n; i++) {
    cnt += a[i].c;
    if (cnt > a[i].lim) {
      fg = 0;
      break;
    }
  }
  if (fg) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef struct ss {
  int a, b;
  bool operator<(const ss &x) const {
    if (x.b != b)
      return b < x.b;
  }
} s;
s node[200007];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &node[i].a, &node[i].b);
  }
  sort(node + 1, node + 1 + n);
  long long time = 0;
  for (int i = 1; i <= n; ++i) {
    time += 1ll * node[i].a;
    if (time > 1ll * node[i].b) {
      printf("No");
      return 0;
    }
  }
  printf("Yes");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef struct ss {
  int a, b;
  bool operator<(const ss &x) const {
    if (x.b != b)
      return b < x.b;
    else
      return a < x.a;
  }
} s;
s node[200007];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &node[i].a, &node[i].b);
  }
  sort(node + 1, node + 1 + n);
  long long time = 0;
  for (int i = 1; i <= n; ++i) {
    time += 1ll * node[i].a;
    if (time > 1ll * node[i].b) {
      printf("No");
      return 0;
    }
  }
  printf("Yes");
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
const int maxn = 200007;
struct thing {
  int t;
  int e;
} a[maxn];

bool cmp(thing x, thing y) {
  if (x.e > y.e) {
    return false;
  } else
    return true;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i].t >> a[i].e;
  }
  sort(a, a + N, cmp);
  long long res = 0;
  int flag = 0;
  for (int i = 0; i < N; i++) {
    res += a[i].t;
    if (res > a[i].e) {
      flag = 1;
      break;
    }
  }
  if (flag) {
    cout << "No" << endl;
  } else
    cout << "Yes" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
const int maxn = 200007;
struct thing {
  int t;
  int e;
} a[maxn];

bool cmp(thing x, thing y) {
  if (x.e > y.e) {
    return false;
  } else if (x.e == y.e) {
    return x.t < y.t;
  } else
    return true;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i].t >> a[i].e;
  }
  sort(a, a + N, cmp);
  long long res = 0;
  int flag = 0;
  for (int i = 0; i < N; i++) {
    res += a[i].t;
    if (res > a[i].e) {
      flag = 1;
      break;
    }
  }
  if (flag) {
    cout << "No" << endl;
  } else
    cout << "Yes" << endl;
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define maxn 20005

using namespace std;
int n, sum = 0;

struct job {
  int a, b;
} jobs[maxn];

int cmp(job x, job y) { return x.b < y.b; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> jobs[i].a >> jobs[i].b;
  }
  sort(jobs + 1, jobs + n + 1, cmp);

  for (int i = 1; i <= n; i++) {
    sum += jobs[i].a;
    if (sum > jobs[i].b) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define maxn 200005

using namespace std;
int n, sum = 0;

struct job {
  int a, b;
} jobs[maxn];

int cmp(job x, job y) { return x.b < y.b; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> jobs[i].a >> jobs[i].b;
  }
  sort(jobs + 1, jobs + n + 1, cmp);

  for (int i = 1; i <= n; i++) {
    sum += jobs[i].a;
    if (sum > jobs[i].b) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes";
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
struct node {
  int st;
  int ed;  // start&end
} a[1005]; // 结构体
bool cmp(node x, node y) { return x.ed < y.ed; }
int main() {
  int n, i, t = 0, ans = 1, total = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d%d", &a[i].st, &a[i].ed);
  sort(a + 1, a + n + 1, cmp);
  t = a[1].ed;
  ans = 1;
  for (i = 1; i <= n + 1; i++) {
    if (total <= t) {
      t = a[i].ed;
    } else {
      printf("No");
      return 0;
    }
    total += a[i].st;
  }
  printf("Yes");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
struct node {
  int st;
  int ed;
} a[200005];
bool cmp(node x, node y) { return x.ed < y.ed; }
int main() {
  int n, i, t = 0, ans = 1, total = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d%d", &a[i].st, &a[i].ed);
  sort(a + 1, a + n + 1, cmp);
  t = a[1].ed;
  ans = 1;
  for (i = 1; i <= n + 1; i++) {
    if (total <= t) {
      t = a[i].ed;
    } else {
      printf("No");
      return 0;
    }
    total += a[i].st;
  }
  printf("Yes");
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
using lint = int64_t;

int main() {
  int N;
  pair<lint, lint> p[100010];

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> p[i].second;
    cin >> p[i].first;
  }
  sort(p + 1, p + 1 + N);

  int time = 0;
  lint sum = 0;
  for (int i = 1; i <= N; i++) {
    sum += p[i].second;
    time = p[i].first;
    if (sum > time) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}


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

int main() {
  int N;
  pair<lint, lint> p[200010];

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> p[i].second;
    cin >> p[i].first;
  }
  sort(p + 1, p + 1 + N);

  int time = 0;
  lint sum = 0;
  for (int i = 1; i <= N; i++) {
    sum += p[i].second;
    time = p[i].first;
    if (sum > time) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
int n;
pair<int, int> a[100000];
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i].second >> a[i].first;
  sort(a, a + n);
  int tv = 0, f = 0;
  for (int i = 0; i < n; ++i) {
    tv += a[i].second;
    if (a[i].first < tv) {
      f = 1;
      break;
    }
  }
  cout << (!f ? "Yes" : "No") << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
int n;
pair<int, int> a[200000];
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i].second >> a[i].first;
  sort(a, a + n);
  int tv = 0, f = 0;
  for (int i = 0; i < n; ++i) {
    tv += a[i].second;
    if (a[i].first < tv) {
      f = 1;
      break;
    }
  }
  cout << (!f ? "Yes" : "No") << endl;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
ll n;
vector<pair<ll, ll>> v(100005);
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v[i].second >> v[i].first;
  }
  while (v.size() > n)
    v.pop_back();
  sort(v.begin(), v.end());
  ll cur;
  for (int i = 0; i < n; i++) {
    if (cur + v[i].second > v[i].first)
      return cout << "No", 0;
    cur += v[i].second;
  }
  cout << "Yes" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
ll n;
vector<pair<ll, ll>> v(200005);
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v[i].second >> v[i].first;
  }
  while (v.size() > n)
    v.pop_back();
  sort(v.begin(), v.end());
  ll cur;
  for (int i = 0; i < n; i++) {
    if (cur + v[i].second > v[i].first)
      return cout << "No", 0;
    cur += v[i].second;
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define D first
#define T second
using namespace std;

long long n, s;
pair<int, int> a[100005];
int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; i++)
    scanf("%d%d", &a[i].T, &a[i].D);
  sort(a, a + n);
  for (i = 0; i < n; i++) {
    s += a[i].T;
    if (s > a[i].D) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define D first
#define T second
using namespace std;

long long n, s;
pair<int, int> a[200005];
int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; i++)
    scanf("%d%d", &a[i].T, &a[i].D);
  sort(a, a + n);
  for (i = 0; i < n; i++) {
    s += a[i].T;
    if (s > a[i].D) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 100000;

int main() {
  int N;
  pair<int, int> x[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i].second >> x[i].first;
  }

  sort(x, x + N);

  int sum = 0;
  int lim = 0;

  for (int i = 0; i < N; i++) {
    sum += x[i].second;
    lim = x[i].first;

    if (lim < sum) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 200001;

int main() {
  int N;
  pair<int, int> x[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i].second >> x[i].first;
  }

  sort(x, x + N);

  int sum = 0;
  int lim = 0;

  for (int i = 0; i < N; i++) {
    sum += x[i].second;
    lim = x[i].first;

    if (lim < sum) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;

  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll int b[200005];
ll int a[200005];
bool compare(const pair<ll int, int> p1, const pair<ll int, int> p2) {
  if (b[p1.second] == b[p2.second]) {
    return p1.first < p2.second;
  }
  return b[p1.second] < b[p2.second];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;

    vector<pair<ll int, int>> v;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
      v.push_back({b[i] - a[i], i});
    }
    sort(v.begin(), v.end(), compare);
    ll int time = 0;
    bool rt = true;

    for (int i = 0; i < n; i++) {
      if (time <= v[i].first) {
      } else {

        rt = false;
        break;
      }
      time += a[v[i].second];
    }
    if (rt) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll int b[200005];
ll int a[200005];
bool compare(const pair<ll int, int> p1, const pair<ll int, int> p2) {
  if (b[p1.second] == b[p2.second]) {
    return p1.first < p2.first;
  }
  return b[p1.second] < b[p2.second];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;

    vector<pair<ll int, int>> v;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
      v.push_back({b[i] - a[i], i});
    }
    sort(v.begin(), v.end(), compare);
    ll int time = 0;
    bool rt = true;

    for (int i = 0; i < n; i++) {
      if (time <= v[i].first) {
      } else {

        rt = false;
        break;
      }
      time += a[v[i].second];
    }
    if (rt) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
Problem p02996 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> X;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    X.push_back(make_pair(b, a));
    sort(X.begin(), X.end());
    long long sum = 0;
    for (auto j : X) {
      sum += j.second;
      if (sum > j.first) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> X;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    X.push_back(make_pair(b, a));
  }
  sort(X.begin(), X.end());
  long long sum = 0;
  for (auto j : X) {
    sum += j.second;
    if (sum > j.first) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}