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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define repitr(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  ll t, a, tot;
  rep(i, n) {
    int it, ia;
    cin >> it >> ia;
    if (i == 0) {
      t = it;
      a = ia;
      tot = t + a;
      continue;
    }
    int rt = it + ia;
    ll bai;
    bai = tot / rt;
    if (bai == 0)
      bai++;
    ll nt, na, up = bai;
    while (1) {
      nt = it * up;
      na = ia * up;
      int ntot = nt + na;
      if (ntot >= tot && nt >= t && na >= a) {
        tot = ntot;
        t = nt;
        a = na;
        break;
      }
      up++;
    }
  }
  cout << tot << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define repitr(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  ll t, a, tot;
  rep(i, n) {
    int it, ia;
    cin >> it >> ia;
    if (i == 0) {
      t = it;
      a = ia;
      tot = t + a;
      continue;
    }
    ll n = max((t + it - 1) / it, (a + ia - 1) / ia);
    t = n * it;
    a = n * ia;
    tot = t + a;
  }
  cout << tot << endl;
  return 0;
}
Problem p03966 (C++)

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

int main() {
  int N;
  cin >> N;

  long long T, A;
  long long t = 0;
  long long a = 0;
  for (int i = 0; i < N; i++) {
    cin >> T >> A;
    long long coef = 1;
    while (t > coef * T || a > coef * A) {
      coef++;
    }
    t = coef * T;
    a = coef * A;
  }
  cout << t + a << endl;
}


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

int main() {
  int N;
  cin >> N;

  long long T, A;
  long long t = 0;
  long long a = 0;
  for (int i = 0; i < N; i++) {
    cin >> T >> A;
    long long coef = max(1ll, max(t / T, a / A));
    while (t > coef * T || a > coef * A) {
      coef++;
    }
    t = coef * T;
    a = coef * A;
  }
  cout << t + a << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAX = 1000000;
const int INF = 1e9;
const double pi = acos(-1);

int main() {
  int n;
  cin >> n;
  ll T, A;
  rep(i, n) {
    if (i == 0) {
      cin >> T;
      cin >> A;
    } else {
      ll nt, na;
      cin >> nt >> na;
      if (nt >= T && na >= A) {
        T = nt;
        A = na;
        continue;
      } else {
        ll p = nt, q = na;
        ll a = 1, b = 1;
        while (nt < T) {
          nt += p;
          a++;
        }
        while (na < A) {
          na += q;
          b++;
        }
        T = max(a, b) * p;
        A = max(a, b) * q;
      }
    }
  }
  cout << T + A << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAX = 1000000;
const int INF = 1e9;
const double pi = acos(-1);

int main() {
  int n;
  cin >> n;
  ll T, A;
  rep(i, n) {
    if (i == 0) {
      cin >> T;
      cin >> A;
    } else {
      ll nt, na;
      cin >> nt >> na;
      if (nt >= T && na >= A) {
        T = nt;
        A = na;
        continue;
      } else {
        ll p = nt, q = na;
        ll r = max((T + nt - 1) / nt, (A + na - 1) / na);
        T = r * p;
        A = r * q;
      }
    }
  }
  cout << T + A << endl;

  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
const double pi = acos(-1);
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
#define OUTPUT(i) (cout << (ll)i << endl)
#define ALL(a) ((a).begin(), (a).end())

bool operator<(const pair<ll, ll> &a, const pair<ll, ll> &b) {

  if (a.first == b.first)
    return a.second < b.second;

  return a.first < b.first;
}

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

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

  // cout << fixed << setprecision(15);

  int N;
  cin >> N;

  ll x, y;

  for (int i = 0; i < N; ++i) {
    ll T, A;
    cin >> T >> A;
    if (i == 0) {
      x = T;
      y = A;
      continue;
    }

    if (T == A) {
      x = max(x, y);
      y = max(y, x);
      continue;
    }

    ll t = min(x / T, y / A);
    while (1) {
      if (t * T >= x && t * A >= y)
        break;
      t++;
    }
    x = t * T;
    y = t * A;
  }
  cout << x + y << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
const double pi = acos(-1);
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
#define OUTPUT(i) (cout << (ll)i << endl)
#define ALL(a) ((a).begin(), (a).end())

bool operator<(const pair<ll, ll> &a, const pair<ll, ll> &b) {

  if (a.first == b.first)
    return a.second < b.second;

  return a.first < b.first;
}

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

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

  // cout << fixed << setprecision(15);

  int N;
  cin >> N;

  ll x, y;

  for (int i = 0; i < N; ++i) {
    ll T, A;
    cin >> T >> A;
    if (i == 0) {
      x = T;
      y = A;
      continue;
    }

    if (T == A) {
      x = max(x, y);
      y = max(y, x);
      continue;
    }

    ll t = max(x / T, y / A);
    while (1) {
      if (t * T >= x && t * A >= y)
        break;
      t++;
    }
    x = t * T;
    y = t * A;
  }
  cout << x + y << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ll n;
  cin >> n;
  ll t[n], a[n];
  for (ll i = 0; i < n; i++)
    cin >> t[i] >> a[i];
  ll ans1 = t[0], ans = a[0];
  for (ll i = 0; i < n - 1; i++) {
    ll k = t[i + 1], l = a[i + 1];
    if (k < ans1 || l < ans) {
      while (1) {
        if (k >= ans1 && l >= ans)
          break;
        k += t[i + 1];
        l += a[i + 1];
      }
      ans1 = k;
      ans = l;
    } else {
      ans1 = t[i + 1];
      ans = a[i + 1];
    }
  }
  cout << ans1 + ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ll n;
  cin >> n;
  ll t[n], a[n];
  for (ll i = 0; i < n; i++)
    cin >> t[i] >> a[i];
  ll ans1 = t[0], ans = a[0];
  for (ll i = 0; i < n - 1; i++) {
    ll k = t[i + 1], l = a[i + 1];
    if (k < ans1 || l < ans) {
      ll p = ans1 / t[i + 1], q = ans / a[i + 1];
      if (ans1 % t[i + 1] != 0)
        p++;
      if (ans % a[i + 1] != 0)
        q++;
      ans1 = max(p, q) * t[i + 1];
      ans = max(p, q) * a[i + 1];
    } else {
      ans1 = t[i + 1];
      ans = a[i + 1];
    }
  }
  cout << ans1 + ans << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
const int maxn = 1 << 18;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  ll pa = 0, pb = 0, a = 0, b = 0, n, t = 1;
  cin >> n;
  while (n--) {
    cin >> a >> b;
    t = 1;
    while (t * a < pa || t * b < pb)
      t++;
    tie(pa, pb) = tuple<ll, ll>{t * a, t * b};
  }
  cout << pa + pb;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
const int maxn = 1 << 18;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  ll pa = 0, pb = 0, a = 0, b = 0, n, t = 1;
  cin >> n;
  while (n--) {
    cin >> a >> b;
    t = max({(pa + a - 1) / a, (pb + b - 1) / b, 1ll});
    tie(pa, pb) = tuple<ll, ll>{t * a, t * b};
  }
  cout << pa + pb;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (int i = 0; i < n; i++)
#define IREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPEACH(i, k) for (auto &i : k)
#define all(a) a.begin(), a.end()
#define MOD 1000000007
#define int long long
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;

signed main() {
  int n;
  cin >> n;
  vector<int> t(n);
  vector<int> a(n);
  REP(i, n) {
    cin >> t[i] >> a[i];
    if (i == 0) {
      continue;
    }
    if (t[i] >= t[i - 1] && a[i] >= a[i - 1]) {
      continue;
    } else {
      int x = t[i];
      int y = a[i];
      while (!(t[i] >= t[i - 1] && a[i] >= a[i - 1])) {
        t[i] += x;
        a[i] += y;
      }
    }
  }
  cout << a[n - 1] + t[n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (int i = 0; i < n; i++)
#define IREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPEACH(i, k) for (auto &i : k)
#define all(a) a.begin(), a.end()
#define MOD 1000000007
#define int long long
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;

signed main() {
  int n;
  cin >> n;
  vector<int> t(n);
  vector<int> a(n);
  REP(i, n) {
    cin >> t[i] >> a[i];
    if (i == 0) {
      continue;
    }
    if (t[i] >= t[i - 1] && a[i] >= a[i - 1]) {
      continue;
    } else {
      int x = t[i];
      int y = a[i];
      int m = (max((t[i - 1] + x - 1) / x, (a[i - 1] + y - 1) / y));
      t[i] = x * m;
      a[i] = y * m;
    }
  }
  cout << a[n - 1] + t[n - 1] << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
ll MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  ll vote0, vote1;
  vote0 = vote1 = 1;
  for (int i = 0; i < n; i++) {
    ll t, a;
    cin >> t >> a;
    for (ll j = 1;; j++) {
      if (vote0 <= t * j && vote1 <= a * j) {
        vote0 = t * j;
        vote1 = a * j;
        break;
      }
    }
  }
  cout << vote0 + vote1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
ll MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  ll vote0, vote1;
  vote0 = vote1 = 1;
  for (int i = 0; i < n; i++) {
    ll t, a;
    cin >> t >> a;
    for (ll j = max(vote0 / t, vote1 / a);; j++) {
      if (vote0 <= t * j && vote1 <= a * j) {
        vote0 = t * j;
        vote1 = a * j;
        break;
      }
    }
  }
  cout << vote0 + vote1 << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1009];
ll b[1009];
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a * b) / (gcd(a, b)); }
int main() {
  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  ll x = 1;
  ll y = 1;
  cout << endl;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= x && b[i] >= y) {
      x = a[i];
      y = b[i];
    } else if (a[i] < x || b[i] < y) {
      ll q = a[i];
      ll w = b[i];
      while (q < x || w < y) {
        q += a[i];
        w += b[i];
      }
      x = q;
      y = w;
    }
    // cout<<x<<" "<<y<<endl;
  }
  cout << x + y << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1009];
ll b[1009];
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a * b) / (gcd(a, b)); }
int main() {
  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  ll x = 1;
  ll y = 1;
  cout << endl;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= x && b[i] >= y) {
      x = a[i];
      y = b[i];
      continue;
    } else if (a[i] < x && b[i] < y) {
      ll s1 = (x + a[i] - 1) / a[i];
      ll s2 = (y + b[i] - 1) / b[i];
      ll m = max(s1, s2);
      x = a[i] * m;
      y = b[i] * m;
      continue;
    } else if (a[i] < x && b[i] >= y) {
      ll m = (x + a[i] - 1) / a[i];
      x = a[i] * m;
      y = b[i] * m;
      continue;
    } else if (a[i] >= x && b[i] < y) {
      ll m = (y + b[i] - 1) / b[i];
      x = a[i] * m;
      y = b[i] * m;
      continue;
    }
    // cout<<x<<" "<<y<<endl;
  }
  cout << x + y << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const constexpr int INF = 1e9;
// typedef std::pair<std::string,double> P;

int N;
string s;
int circle[201][201];
int rect[201][201];
void solve() {
  long long T = 0, A = 0;
  cin >> T >> A;
  for (int i = 0; i < N - 1; ++i) {
    long long t, a;
    cin >> t >> a;
    long long j = 1;
    while (1) {
      if (t * j >= T && a * j >= A)
        break;
      j++;
    }
    T = t * j;
    A = a * j;
  }
  cout << T + A << endl;
}
int main() {
  cin >> N;
  //(1) K=10 (2)11
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const constexpr int INF = 1e9;
// typedef std::pair<std::string,double> P;

int N;
string s;
int circle[201][201];
int rect[201][201];
void solve() {
  long long T = 0, A = 0;
  cin >> T >> A;
  for (int i = 0; i < N - 1; ++i) {
    long long t, a;
    cin >> t >> a;
    long long n;
    n = max((T % t == 0) ? T / t : T / t + 1, (A % a == 0) ? A / a : A / a + 1);
    T = n * t;
    A = n * a;
  }
  cout << T + A << endl;
}
int main() {
  cin >> N;
  //(1) K=10 (2)11
  solve();
  return 0;
}
Problem p03966 (C++)

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

ll gcd(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  ll tmp = a % b;
  while (tmp != 0) {
    a = b;
    b = tmp;
    tmp = a % b;
  }
  return b;
}

int main() {
  int N;
  cin >> N;
  int T[N], A[N];
  for (int i = 0; i < N; i++)
    cin >> T[i] >> A[i];

  ll taka = 1, aoki = 1;
  for (int i = 0; i < N; i++) {
    if (T[i] < A[i]) {
      ll k = taka / T[i] + (bool)(taka % T[i]);
      while (aoki > A[i] * k)
        k++;
      taka = T[i] * k;
      aoki = A[i] * k;
    } else {
      ll k = aoki / A[i] + (bool)(aoki % A[i]);
      while (taka > T[i] * k)
        k++;
      taka = T[i] * k;
      aoki = A[i] * k;
    }
  }
  cout << taka + aoki << endl;
}

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

ll gcd(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  ll tmp = a % b;
  while (tmp != 0) {
    a = b;
    b = tmp;
    tmp = a % b;
  }
  return b;
}

int main() {
  int N;
  cin >> N;
  int T[N], A[N];
  for (int i = 0; i < N; i++)
    cin >> T[i] >> A[i];

  ll taka = 1, aoki = 1;
  for (int i = 0; i < N; i++) {
    ll k = max(taka / T[i] + (bool)(taka % T[i]),
               aoki / A[i] + (bool)(aoki % A[i]));
    taka = T[i] * k;
    aoki = A[i] * k;
  }
  cout << taka + aoki << endl;
}
Problem p03966 (C++)

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

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

#define in_range(x, y, w, h)                                                   \
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int main() {
  ll N;
  cin >> N;

  ll minT = 1LL, minA = 1LL;
  rep(i, N) {
    ll T, A;
    cin >> T >> A;

    if (T >= minT && A >= minA) {
      minT = T, minA = A;
    } else {
      ll t = (minT + T - 1) / T, a = (minA + A - 1) / A;

      bool ok = false;
      if (minT <= t * T && minA <= t * A) {
        minT = t * T, minA = t * A;
        ok = true;
      }
      if (minT <= a * T && minA <= a * A) {
        minT = a * T, minA = a * A;
        assert(!ok);
      }
    }
  }

  cout << minT + minA << endl;
}


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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

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

#define in_range(x, y, w, h)                                                   \
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int main() {
  ll N;
  cin >> N;

  ll minT = 1LL, minA = 1LL;
  rep(i, N) {
    ll T, A;
    cin >> T >> A;

    if (T >= minT && A >= minA) {
      minT = T, minA = A;
    } else {
      ll t = (minT + T - 1) / T, a = (minA + A - 1) / A;

      bool ok = false;
      if (minT <= t * T && minA <= t * A) {
        minT = t * T, minA = t * A;
        ok = true;
      }
      if (minT <= a * T && minA <= a * A) {
        if (ok && minT + minA <= a * T + a * A) {
          // do nothing
        } else {
          minT = a * T, minA = a * A;
        }
      }
    }
  }

  cout << minT + minA << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N;
  cin >> N;
  long x = 0, y = 0;
  for (long i = 0; i < N; i++) {
    long t, a;
    cin >> t >> a;
    long c = 1;
    while (t * c < x || a * c < y)
      c++;
    x = t * c;
    y = a * c;
  }
  cout << x + y;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N;
  cin >> N;
  long x = 0, y = 0;
  for (long i = 0; i < N; i++) {
    long t, a;
    cin >> t >> a;
    long c = max(max(x / t, y / a), 1L);
    while (t * c < x || a * c < y)
      c++;
    x = t * c;
    y = a * c;
  }
  cout << x + y;
}
Problem p03966 (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(void) {
  int n;
  cin >> n;
  vector<pair<int, int>> v(n);
  rep(i, n) {
    int t, a;
    cin >> t >> a;
    v[i] = make_pair(t, a);
  }
  ll t = (ll)v[0].first;
  ll a = (ll)v[0].second;
  for (int i = 1; i <= n - 1; i++) {
    ll t_ratio = (ll)v[i].first;
    ll a_ratio = (ll)v[i].second;
    while (t_ratio < t || a_ratio < a) {
      t_ratio += (ll)v[i].first;
      a_ratio += (ll)v[i].second;
    }
    t = t_ratio;
    a = a_ratio;
  }
  cout << t + a << 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(void) {
  int n;
  cin >> n;
  vector<pair<int, int>> v(n);
  rep(i, n) {
    int t, a;
    cin >> t >> a;
    v[i] = make_pair(t, a);
  }
  ll t = (ll)v[0].first;
  ll a = (ll)v[0].second;
  for (int i = 1; i <= n - 1; i++) {
    ll t_ratio = (ll)v[i].first;
    ll a_ratio = (ll)v[i].second;
    ll tnum = max(0LL, (t - t_ratio + (ll)v[i].first - 1) / ((ll)v[i].first));
    ll anum = max(0LL, (a - a_ratio + (ll)v[i].second - 1) / ((ll)v[i].second));
    t = t_ratio * (max(tnum, anum) + 1);
    a = a_ratio * (max(tnum, anum) + 1);
  }
  cout << t + a << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n;
  cin >> n;
  long long int t[1001], a[1001];
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  long long int takahashi = t[0], aoki = a[0];
  for (int i = 1; i < n; i++) {
    if (takahashi % t[i] != 0)
      takahashi += t[i] - (takahashi % t[i]);
    if (aoki % a[i] != 0)
      aoki += a[i] - (aoki % a[i]);
    while (takahashi * a[i] != aoki * t[i]) {
      if (takahashi * a[i] > aoki * t[i])
        aoki += a[i];
      else
        takahashi += t[i];
    }
  }
  cout << takahashi + aoki << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n;
  cin >> n;
  long long int t[1001], a[1001];
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  long long int takahashi = t[0], aoki = a[0];
  for (int i = 1; i < n; i++) {
    if (takahashi % t[i] != 0)
      takahashi += t[i] - (takahashi % t[i]);
    if (aoki % a[i] != 0)
      aoki += a[i] - (aoki % a[i]);
    if (takahashi / t[i] > aoki / a[i]) {
      aoki = (takahashi / t[i]) * a[i];
    } else if (takahashi / t[i] < aoki / a[i]) {
      takahashi = (aoki / a[i]) * t[i];
    }
  }
  cout << takahashi + aoki << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, m, n) for (i = m; i <= n; i++)
#define rsp(it, s) for (set<int>::iterator it = s.begin(); it != s.end(); it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll, int>
#define Lson L, mid, ls[rt]
#define Rson mid + 1, R, rs[rt]
#define sys system("pause")
const int maxn = 1e5 + 10;
using namespace std;
ll gcd(ll p, ll q) { return q == 0 ? p : gcd(q, p % q); }
ll qpow(ll p, ll q) {
  ll f = 1;
  while (q) {
    if (q & 1)
      f = f * p;
    p = p * p;
    q >>= 1;
  }
  return f;
}
inline ll read() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, m, k, t;
ll a, b, pa, pb;
int main() {
  int i, j;
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%lld%lld", &a, &b);
    if (i == 1)
      pa = a, pb = b;
    else {
      j = 1;
      while (a * j < pa || b * j < pb)
        j++;
      a *= j, b *= j;
      pa = a, pb = b;
    }
  }
  printf("%lld\n", a + b);
  // system("Pause");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, m, n) for (i = m; i <= n; i++)
#define rsp(it, s) for (set<int>::iterator it = s.begin(); it != s.end(); it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll, int>
#define Lson L, mid, ls[rt]
#define Rson mid + 1, R, rs[rt]
#define sys system("pause")
const int maxn = 1e5 + 10;
using namespace std;
ll gcd(ll p, ll q) { return q == 0 ? p : gcd(q, p % q); }
ll qpow(ll p, ll q) {
  ll f = 1;
  while (q) {
    if (q & 1)
      f = f * p;
    p = p * p;
    q >>= 1;
  }
  return f;
}
inline ll read() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, m, k, t;
ll a, b, pa, pb;
int main() {
  int i, j;
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%lld%lld", &a, &b);
    if (i == 1)
      pa = a, pb = b;
    else {
      ll j = max((pa % a == 0 ? pa / a : pa / a + 1),
                 (pb % b == 0 ? pb / b : pb / b + 1));
      a *= j, b *= j;
      pa = a, pb = b;
    }
  }
  printf("%lld\n", a + b);
  // system("Pause");
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll a = 1, b = 1;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    ll mult = 1;
    if (x * b == y * a)
      continue;
    while (a > x * mult || b > y * mult) {
      mult++;
    }
    x *= mult;
    y *= mult;
    a = x;
    b = y;
  }
  ll ans = a + b;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll a = 1, b = 1;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    ll mult = 1;
    if (x * b == y * a)
      continue;
    if (a / x > b / y) {
      if ((a - 1) / x != a / x) {
        mult = a / x;
      } else {
        mult = (a / x) + 1;
      }
    } else if (a / x < b / y) {
      if ((b - 1) / y != b / y) {
        mult = b / y;
      } else {
        mult = (b / y) + 1;
      }
    } else {
      if ((a - 1) / x == a / x || (b - 1) / y == b / y) {
        mult = (b / y) + 1;
      } else {
        mult = (a / x);
      }
    }
    x *= mult;
    y *= mult;
    a = x;
    b = y;
  }
  ll ans = a + b;
  cout << ans << endl;
}
Problem p03966 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<pair<ll, ll>> p(n);
  rep(i, n) {
    ll t, a;
    cin >> t >> a;
    p[i] = make_pair(t, a);
  }
  ll x, y;
  x = p[0].first;
  y = p[0].second;
  for (int i = 1; i < n; i++) {
    while (true) {
      if (p[i].first < x || p[i].second < y) {
        ll s = max(ceil((double)x / p[i].first), ceil((double)y / p[i].second));
        p[i].first *= s;
        p[i].second *= s;
      } else {
        x = p[i].first;
        y = p[i].second;
        break;
      }
    }
  }
  cout << x + y << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<pair<ll, ll>> p(n);
  rep(i, n) {
    ll t, a;
    cin >> t >> a;
    p[i] = make_pair(t, a);
  }
  ll x = 1, y = 1;
  for (int i = 0; i < n; i++) {
    ll s = max((x - 1) / p[i].first, (y - 1) / p[i].second);
    s++;
    p[i].first *= s;
    p[i].second *= s;
    x = p[i].first;
    y = p[i].second;
  }
  cout << x + y << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, pii> edge;

int n;
LL T = 0, A = 0;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    LL t, a;
    cin >> t >> a;
    LL ct = t, ca = a;
    while (ct < T || ca < A) {
      ct += t;
      ca += a;
    }
    T = ct;
    A = ca;
  }
  cout << T + A << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, pii> edge;

int n;
LL T = 0, A = 0;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    LL t, a;
    cin >> t >> a;
    LL ct = t, ca = a;
    LL cnt = max((T + t - 1) / t, (A + a - 1) / a);
    T = (i == 0 ? t : t * cnt);
    A = (i == 0 ? a : a * cnt);
  }
  cout << T + A << endl;
}
Problem p03966 (C++)

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

constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3fLL;

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

  int n;
  cin >> n;
  vector<ll> ts(n), as(n);
  for (int i = 0; i < n; i++)
    cin >> ts[i] >> as[i];

  ll mt = 1, ma = 1;
  for (int i = 0; i < n; i++) {
    ll t = 0, a = 0;
    while (t < mt || a < ma)
      t += ts[i], a += as[i];
    mt = t, ma = a;
  }
  cout << mt + ma << endl;
  return 0;
}


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

constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3fLL;

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

  int n;
  cin >> n;
  vector<ll> ts(n), as(n);
  for (int i = 0; i < n; i++)
    cin >> ts[i] >> as[i];

  ll mt = 1, ma = 1;
  for (int i = 0; i < n; i++) {
    ll t = ((mt + ts[i] - 1) / ts[i]);
    ll a = ((ma + as[i] - 1) / as[i]);
    mt = ts[i] * max(t, a), ma = as[i] * max(t, a);
  }
  cout << mt + ma << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define pb push_back
#define mod 1000000007
using namespace std;
typedef long long int ll;

int main(void) {
  int n;
  ll t, a;
  cin >> n;
  cin >> t >> a;
  ll x, y;
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    if (a * x == t * y)
      continue;
    else {
      ll j = 1;
      while ((t > (x * j)) || (a > (y * j)))
        j++;
      t = x * j;
      a = y * j;
    }
  }
  cout << t + a << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define pb push_back
#define mod 1000000007
using namespace std;
typedef long long int ll;

int main(void) {
  int n;
  ll t, a;
  cin >> n;
  cin >> t >> a;
  ll x, y;
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    if (a * x == t * y)
      continue;
    else {
      ll tmp1, tmp2;
      tmp1 = (t + x - 1) / x;
      tmp2 = (a + y - 1) / y;
      if (tmp1 > tmp2) {
        ll ttt;
        ttt = tmp1;
        tmp1 = tmp2;
        tmp2 = ttt;
      }
      if (x * tmp1 >= t && y * tmp1 >= a) {
        t = tmp1 * x;
        a = tmp1 * y;
      } else {
        t = tmp2 * x;
        a = tmp2 * y;
      }
    }
  }
  cout << t + a << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

int main() {
  ll n;
  cin >> n;
  ll l, r;
  cin >> l >> r;
  rep(i, n - 1) {
    ll t, a;
    cin >> t >> a;
    ll mlt = 1;
    while (l > t * mlt || r > a * mlt)
      mlt++;
    l = t * mlt, r = a * mlt;
  }
  cout << l + r << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

int main() {
  ll n;
  cin >> n;
  ll l, r;
  cin >> l >> r;
  rep(i, n - 1) {
    ll t, a;
    cin >> t >> a;
    if (l <= t && r <= a)
      l = t, r = a;
    else {
      ll x, y;
      x = l / t, y = r / a;
      if (l % t != 0)
        x++;
      if (r % a != 0)
        y++;
      l = t * max(x, y), r = a * max(x, y);
    }
  }
  cout << l + r << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  long long left = 1, right = 1;
  for (int i = 0; i < N; ++i) {
    long long ll = left, lr = right;
    int T, A;
    long long t, a;
    cin >> T >> A;
    t = T, a = A;
    while (t < left || a < right) {
      t += T, a += A;
    }
    left = t, right = a;

    // cout << left << " " << right << endl;
  }
  cout << left + right << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  long long left = 1, right = 1;
  for (int i = 0; i < N; ++i) {
    long long ll = left, lr = right;
    int T, A;
    long long t, a;
    cin >> T >> A;
    long long mult = max(left / T, right / A);
    t = (mult)*T, a = (mult)*A;
    while (t < left || a < right) {
      t += T, a += A;
    }
    left = t, right = a;

    // cout << left << " " << right << endl;
  }
  cout << left + right << endl;

  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void dump(vector<T> v) {
  for (auto &x : v)
    cerr << x << " ";
  cerr << endl;
}
template <class T> inline void dump(vector<pair<T, T>> v) {
  for (auto &p : v)
    cerr << p.first << " " << p.second << endl;
}
template <class T> inline void dump(vector<vector<T>> vv) {
  for (auto &v : vv) {
    for (auto &x : v)
      cerr << x << " ";
    cerr << endl;
  }
}

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 60;
constexpr double eps = (1e-9);

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

  int n;
  cin >> n;

  ll ans_t = 1;
  ll ans_a = 1;

  rep(i, n) {
    ll t, a;
    cin >> t >> a;
    ll tmp_t = t;
    ll tmp_a = a;
    int count = max(ans_t / t, ans_a / a);
    while (tmp_t < ans_t || tmp_a < ans_a) {
      tmp_t = t * count;
      tmp_a = a * count;
      count++;
    }
    ans_t = tmp_t;
    ans_a = tmp_a;
  }

  cout << ans_t + ans_a << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void dump(vector<T> v) {
  for (auto &x : v)
    cerr << x << " ";
  cerr << endl;
}
template <class T> inline void dump(vector<pair<T, T>> v) {
  for (auto &p : v)
    cerr << p.first << " " << p.second << endl;
}
template <class T> inline void dump(vector<vector<T>> vv) {
  for (auto &v : vv) {
    for (auto &x : v)
      cerr << x << " ";
    cerr << endl;
  }
}

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 60;
constexpr double eps = (1e-9);

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

  int n;
  cin >> n;

  ll ans_t = 1;
  ll ans_a = 1;

  rep(i, n) {
    ll t, a;
    cin >> t >> a;
    ll count = max((ans_t + t - 1) / t, (ans_a + a - 1) / a);
    ans_t = t * count;
    ans_a = a * count;
  }

  cout << ans_t + ans_a << endl;

  return 0;
}
Problem p03966 (C++)

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

int main() {
  int n;
  long long t, a;
  cin >> n;

  long long ansA = 0, ansB = 0;

  n--;
  cin >> ansA >> ansB;

  while (n-- > 0) {
    cin >> t >> a;
    ansA = max(ansA, t);
    ansB = max(ansB, a);
    int i = max(ansA / t, ansB / a);
    while (((t * i) < ansA) || ((a * i) < ansB)) {
      i++;
    }
    ansA = t * i;
    ansB = a * i;
  }

  cout << ansA + ansB << endl;

  return 0;
}

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

int main() {
  int n;
  long long t, a;
  cin >> n;

  long long ansA = 0, ansB = 0;

  n--;
  cin >> ansA >> ansB;

  while (n-- > 0) {
    cin >> t >> a;
    ansA = max(ansA, t);
    ansB = max(ansB, a);
    long long i = max(ansA / t, ansB / a);
    while (((t * i) < ansA) || ((a * i) < ansB)) {
      i++;
    }
    ansA = t * i;
    ansB = a * i;
  }

  cout << ansA + ansB << endl;

  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

int main() {
  int n;
  cin >> n;
  vl t(n), a(n);
  rep(i, n) cin >> t[i] >> a[i];
  ll ans = t[0] + a[0];
  ll aa = a[0], tt = t[0];
  REP(i, 1, n) {
    ll k = 1LL;
    while (k * a[i] < aa || k * t[i] < tt)
      k++;
    aa = a[i] * k;
    tt = t[i] * k;
  }
  ans = aa + tt;
  cout << ans << endl;
}

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

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

int main() {
  int n;
  cin >> n;
  vl t(n), a(n);
  rep(i, n) cin >> t[i] >> a[i];
  ll ans = t[0] + a[0];
  ll aa = a[0], tt = t[0];
  REP(i, 1, n) {
    // ll k = 1LL;
    // while(k*a[i] < aa || k * t[i] < tt) k++;
    ll k = max((aa - 1) / a[i] + 1LL, (tt - 1) / t[i] + 1LL);
    aa = a[i] * k;
    tt = t[i] * k;
  }
  ans = aa + tt;
  cout << ans << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int n;
  cin >> n;
  ll ans = 0;
  ll a, b;
  cin >> a >> b;
  for (int i = 1; i < n; i++) {
    ll c, d;
    cin >> c >> d;
    ll e = 1;
    while (a > c * e || b > d * e) {
      e++;
      ;
    }
    a = c * e;
    b = d * e;
  }
  cout << a + b << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int n;
  cin >> n;
  ll ans = 0;
  ll a, b;
  cin >> a >> b;
  for (int i = 1; i < n; i++) {
    ll c, d;
    cin >> c >> d;
    ll e, f;
    if (a % c == 0)
      e = a / c;
    else
      e = a / c + 1;
    if (b % d == 0)
      f = b / d;
    else
      f = b / d + 1;
    a = c * max(e, f);
    b = d * max(e, f);
    // cout<<a<<" "<<b<<endl;
  }
  cout << a + b << endl;
  return 0;
}
Problem p03966 (C++)

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

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

typedef long long ll;
using namespace std;
int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  ll tk, ao;
  cin >> tk >> ao;
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    // a:bになる
    // tk+X : ao+Y = a:b
    // a(ao + Y) = b(tk+X)
    // Y = b(tk+X)/a - ao
    // aとbは互いに素なのでtk+Xはaの倍数
    // 同様にao+Yはbの倍数
    // よってtk+X = n*a, ao*Y =
    // n*bとなる(ここでnは高橋くんと青木くんの票数が減らない範囲でなるべく小さく取る)
    // つまりX >= 0 && Y >= 0
    ll n = max(ceil((double)tk / (double)a), ceil((double)ao / (double)b));
    assert(tk <= n * a && ao <= n * b);
    tk = n * a;
    ao = n * b;
    // cout << tk << " " << ao << endl;
  }
  cout << tk + ao << endl;
  return 0;
}

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

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

typedef long long ll;
using namespace std;
int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  ll tk, ao;
  cin >> tk >> ao;
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    // a:bになる
    // tk+X : ao+Y = a:b
    // a(ao + Y) = b(tk+X)
    // Y = b(tk+X)/a - ao
    // aとbは互いに素なのでtk+Xはaの倍数
    // 同様にao+Yはbの倍数
    // よってtk+X = n*a, ao*Y =
    // n*bとなる(ここでnは高橋くんと青木くんの票数が減らない範囲でなるべく小さく取る)
    // つまりX >= 0 && Y >= 0
    // ll n = max( ceil((double)tk/(double)a), ceil((double)ao/(double)b));
    ll n;
    ll n1, n2;
    if (tk % a == 0)
      n1 = tk / a;
    else
      n1 = tk / a + 1;
    if (ao % b == 0)
      n2 = ao / b;
    else
      n2 = ao / b + 1;
    n = max(n1, n2);
    assert(tk <= n * a && ao <= n * b);
    tk = n * a;
    ao = n * b;
    // cout << tk << " " << ao << endl;
  }
  cout << tk + ao << endl;
  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
// header {{{
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define ALPHABET 26
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define INF 1000000005

typedef long long ll;
typedef unsigned long long ull;
// }}}

int main() {
  ll n;
  cin >> n;
  ll t, a;
  cin >> t >> a;
  for (ll i = 1; i < n; i++) {
    ll tt, aa;
    cin >> tt >> aa;
    for (ll j = min(t / tt, a / aa);; j++) {
      if (tt * j >= t && aa * j >= a) {
        t = tt * j;
        a = aa * j;
        break;
      }
    }
  }
  printf("%lld\n", t + a);
  return 0;
}


Fixed submission:
// header {{{
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define ALPHABET 26
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define INF 1000000005

typedef long long ll;
typedef unsigned long long ull;
// }}}

int main() {
  ll n;
  cin >> n;
  ll t, a;
  cin >> t >> a;
  for (ll i = 1; i < n; i++) {
    ll tt, aa;
    cin >> tt >> aa;
    for (ll j = max(t / tt, a / aa);; j++) {
      if (tt * j >= t && aa * j >= a) {
        t = tt * j;
        a = aa * j;
        break;
      }
    }
  }
  printf("%lld\n", t + a);
  return 0;
}
Problem p03966 (C++)

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

int main() {
  int n;
  cin >> n;
  long long int t = 0, a = 0;
  for (int i = 0; i < n; ++i) {
    long long int ti, ai;
    cin >> ti >> ai;
    long long int m = 1;
    while (ti * m < t || ai * m < a)
      m++;
    t = ti * m;
    a = ai * m;
  }

  cout << t + a << endl;

  return 0;
}


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

int main() {
  int n;
  cin >> n;
  long long int t = 0, a = 0;
  for (int i = 0; i < n; ++i) {
    long long int ti, ai;
    cin >> ti >> ai;
    long long int m = max(1ll, max((t + ti - 1) / ti, (a + ai - 1) / ai));
    t = ti * m;
    a = ai * m;
  }

  cout << t + a << endl;

  return 0;
}