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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) long long((x).size())
#define OUT(x) cout << (x) << endl
typedef long long ll;
typedef vector<ll> V;
typedef vector<vector<ll>> VV;
typedef pair<ll, ll> P;
typedef map<string, ll> M;
typedef unordered_map<ll, ll> HM;
typedef set<ll> S;
typedef multiset<ll> MS;
typedef queue<ll> Q;

int main() {
  ll n;
  cin >> n;
  ll t[n], a[n];
  for (ll i = 0; i < n; i++)
    cin >> t[i] >> a[i];
  ll currT = t[0], currA = a[0];
  for (ll i = 1; i < n; i++) {
    ll x = t[i], y = a[i];
    while (x < currT || y < currA) {
      x += t[i];
      y += a[i];
    }
    currT = x;
    currA = y;
  }
  OUT(currT + currA);
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) long long((x).size())
#define OUT(x) cout << (x) << endl
typedef long long ll;
typedef vector<ll> V;
typedef vector<vector<ll>> VV;
typedef pair<ll, ll> P;
typedef map<string, ll> M;
typedef unordered_map<ll, ll> HM;
typedef set<ll> S;
typedef multiset<ll> MS;
typedef queue<ll> Q;

int main() {
  ll n;
  cin >> n;
  ll t[n], a[n];
  for (ll i = 0; i < n; i++)
    cin >> t[i] >> a[i];
  ll currT = t[0], currA = a[0];
  for (ll i = 1; i < n; i++) {
    ll x = t[i], y = a[i];
    if (x < currT || y < currA) {
      x *= max(currT / t[i], currA / a[i]);
      y *= max(currT / t[i], currA / a[i]);
    }
    if (x < currT || y < currA) {
      x += t[i];
      y += a[i];
    }
    currT = x;
    currA = y;
  }
  OUT(currT + currA);
}
Problem p03964 (C++)

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

int main() {
  long N;
  cin >> N;
  long t, a, tn, an;
  cin >> t >> a;
  tn = t;
  an = a;
  for (int i = 1; i < N; i++) {
    cin >> t >> a;
    if (tn % t)
      tn += t - tn % t;
    if (an % a)
      an += a - an % a;
    while (tn * a != an * t) {
      if (tn * a < an * t)
        tn += t;
      else
        an += a;
    }
  }
  cout << tn + an << endl;
}

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

int main() {
  long N;
  cin >> N;
  long t, a, tn, an;
  cin >> t >> a;
  tn = t;
  an = a;
  for (int i = 1; i < N; i++) {
    cin >> t >> a;
    long n = max(tn / t + (0 < tn % t), an / a + (0 < an % a));
    tn = t * n;
    an = a * n;
  }
  cout << tn + an << endl;
}
Problem p03964 (C++)

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

int main() {
  ll N;
  cin >> N;
  ll tmp_t, tmp_a;
  cin >> tmp_t >> tmp_a;
  ll ans = 0;
  rep(i, N - 1) {
    ll t, a;
    cin >> t >> a;
    if (tmp_t * a == tmp_a * t)
      continue;
    else {
      ll cnt = 1, base_t = t, base_a = a;
      while (t < tmp_t || a < tmp_a) {
        t = base_t * cnt;
        a = base_a * cnt;
        cnt++;
        // cout << t << a << cnt << endl;
      }
      tmp_t = t;
      tmp_a = a;
      // cout << "tmpt" << tmp_t << "tmpa" << tmp_a << endl;
    }
  }
  cout << tmp_t + tmp_a << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using vll = vector<ll>;
using vs = vector<string>;
#define all(v) v.begin(), v.end()

int main() {
  ll N;
  cin >> N;
  ll tmp_t, tmp_a;
  cin >> tmp_t >> tmp_a;
  ll ans = 0;
  rep(i, N - 1) {
    ll t, a;
    cin >> t >> a;
    if (tmp_t * a == tmp_a * t)
      continue;
    else {
      ll base_t = t, base_a = a;
      // ここ高速化
      ll prod = max(tmp_t / t, tmp_a / a);
      if (t * prod < tmp_t || a * prod < tmp_a)
        prod++;
      tmp_t = t * prod;
      tmp_a = a * prod;
      // cout << "tmpt" << tmp_t << "tmpa" << tmp_a << endl;
    }
  }
  cout << tmp_t + tmp_a << endl;
}
Problem p03964 (C++)

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

int main() {
  int N;
  cin >> N;
  long long int a, af, b, bf;
  af = 1;
  bf = 1;
  for (int i = 0; i < N; i++) {
    cin >> a >> b;
    int j = max(af / a, bf / b);
    while (a * j < af || b * j < bf) {
      j++;
    }
    af = a * j;
    bf = b * j;
  }
  cout << af + bf << endl;
}


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

int main() {
  int N;
  cin >> N;
  long long int a, af, b, bf;
  af = 1;
  bf = 1;
  for (int i = 0; i < N; i++) {
    cin >> a >> b;
    long long int j = max(af / a, bf / b);
    while (a * j < af || b * j < bf) {
      j++;
    }
    af = a * j;
    bf = b * j;
  }
  cout << af + bf << endl;
}
Problem p03964 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> T(N), A(N);
  rep(i, N) cin >> T[i] >> A[i];

  ll ansT, ansA;
  ansT = ansA = 1;
  rep(i, N) {
    ll t = (ansT + T[i] - 1) / T[i] * T[i];
    while (t / T[i] * A[i] < ansA) {
      t += T[i];
    }
    ansT = t;
    ansA = t / T[i] * A[i];
  }

  cout << ansT + ansA << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const double PI = acos(-1);
const ll MOD = 1000000007;
using Graph = vector<vector<int>>;

int main() {
  int N;
  cin >> N;
  vector<int> T(N), A(N);
  rep(i, N) cin >> T[i] >> A[i];

  ll ansT, ansA;
  ansT = ansA = 1;
  rep(i, N) {
    ll t = (ansT + T[i] - 1) / T[i];
    ll a = (ansA + A[i] - 1) / A[i];

    ansT = max(t, a) * T[i];
    ansA = max(t, a) * A[i];
  }

  cout << ansT + ansA << endl;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()

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;
}

typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  ll a, b;
  cin >> a >> b;
  rep(i, n - 1) {
    ll ta, tb;
    cin >> ta >> tb;
    ll tta, ttb;
    tta = ta;
    ttb = tb;
    while (!(tta >= a && ttb >= b)) {
      tta += ta;
      ttb += tb;
    }
    a = tta;
    b = ttb;
  }
  cout << a + b << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()

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;
}

typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  ll a, b;
  cin >> a >> b;
  rep(i, n - 1) {
    ll ta, tb;
    cin >> ta >> tb;
    ll bai = max((a - 1 + ta) / ta, (b - 1 + tb) / tb);
    a = bai * ta;
    b = bai * tb;
    // cout<<a<<" "<<b<<endl;
  }
  cout << a + b << endl;

  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n, a) for (int i = n; i >= a; i--)
#define FOR(i, n) for (int i = 0; i < n; i++)
#define all(s) s.begin(), s.end()
#define lla(s) s.rbegin(), s.rend()
#define pcnt(b) __builtin_popcount(b)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll INF = 1e18;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, a, b, A = 1, B = 1;
  cin >> n;
  rep(i, 0, n) {
    cin >> a >> b;
    rep(j, 1, 1 + max((A + a) / a, (B + b) / b)) {
      if (A <= j * a && B <= j * b) {
        A = j * a;
        B = j * b;
        break;
      }
    }
  }
  cout << (A + B) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n, a) for (int i = n; i >= a; i--)
#define FOR(i, n) for (int i = 0; i < n; i++)
#define all(s) s.begin(), s.end()
#define lla(s) s.rbegin(), s.rend()
#define pcnt(b) __builtin_popcount(b)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll INF = 1e18;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, a, b, A = 1, B = 1;
  cin >> n;
  rep(i, 0, n) {
    cin >> a >> b;
    A = (A + a - 1) / a * a;
    B = (B + b - 1) / b * b;
    int r = max(A / a, B / b);
    A = r * a;
    B = r * b;
  }
  cout << (A + B) << endl;
}
Problem p03964 (C++)

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

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

// ====================================================================

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


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

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

// ====================================================================

int main() {
  ll n;
  cin >> n;
  ll x, y, a = 1, b = 1;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    ll j = max((a + x - 1) / x, (b + y - 1) / y);
    a = x * j;
    b = y * j;
  }
  cout << a + b << endl;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma region template
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e18;
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W,
    x, y, z;
long long max_value = INF, max_index = -1;
long long min_value = MAX, min_index = -1;
typedef long long ll;

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

int main() {
  cin >> N;
  ll curr_a, curr_b;
  cin >> curr_a >> curr_b;
  rep(i, N - 1) {
    cin >> A >> B;
    int inc = 1;
    while (inc * A < curr_a || inc * B < curr_b)
      inc++;
    curr_a = inc * A;
    curr_b = inc * B;
  }
  cout << curr_a + curr_b;
}


Fixed submission:
#pragma region template
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e18;
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W,
    x, y, z;
long long max_value = INF, max_index = -1;
long long min_value = MAX, min_index = -1;
typedef long long ll;

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

int main() {
  cin >> N;
  ll curr_a, curr_b;
  cin >> curr_a >> curr_b;
  rep(i, N - 1) {
    cin >> A >> B;
    ll inc = max((curr_a - 1) / A + 1, (curr_b - 1) / B + 1);
    curr_a = inc * A;
    curr_b = inc * B;
  }
  cout << curr_a + curr_b;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O0")
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
#define All(a) (a).begin(), (a).end()
#define Pi acos(-1)

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(15) << fixed;
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();

  ll n;
  cin >> n;
  ll x, y;
  cin >> x >> y;
  rep(i, n - 1) {
    ll t, a;
    cin >> t >> a;
    ll tt = t;
    ll ta = a;
    while (x > tt || y > ta) {
      tt += t;
      ta += a;
    }
    x = tt;
    y = ta;
  }
  cout << x + y << '\n';

  end = chrono::system_clock::now();
  auto elapsed =
      chrono::duration_cast<chrono::milliseconds>(end - start).count();
  cerr << elapsed << "ms" << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O0")
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
#define All(a) (a).begin(), (a).end()
#define Pi acos(-1)

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(15) << fixed;
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();

  ll n;
  cin >> n;
  ll x, y;
  cin >> x >> y;
  rep(i, n - 1) {
    ll t, a;
    cin >> t >> a;
    ll c = max((t + x - 1) / t, (a + y - 1) / a);
    t *= c;
    a *= c;
    x = t;
    y = a;
  }
  cout << x + y << '\n';

  end = chrono::system_clock::now();
  auto elapsed =
      chrono::duration_cast<chrono::milliseconds>(end - start).count();
  cerr << elapsed << "ms" << '\n';
}
Problem p03964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int t[1005], a[1005], n, c, d;
int main() {
  cin >> n;
  cin >> t[1] >> a[1];
  for (int i = 2; i <= n; i++) {
    cin >> t[i] >> a[i];

    if ((t[i] < t[i - 1]) && (a[i] < a[i - 1]))
      c = (t[i - 1] + t[i] - 1) / t[i], d = (a[i - 1] + a[i] - 1) / a[i],
      a[i] = max(c, d) * a[i], t[i] = max(c, d) * t[i];

    if (t[i] < t[i - 1])
      c = (t[i - 1] + t[i] - 1) / t[i], t[i] = c * t[i], a[i] = c * a[i];

    if (a[i] < a[i - 1])
      d = (a[i - 1] + a[i] - 1) / a[i], a[i] = d * a[i], t[i] = d * t[i];
  }
  cout << a[n] + t[n];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long t[1005], a[1005], n, c, d;
int main() {
  cin >> n;
  cin >> t[1] >> a[1];
  for (int i = 2; i <= n; i++) {
    cin >> t[i] >> a[i];

    if ((t[i] < t[i - 1]) && (a[i] < a[i - 1]))
      c = (t[i - 1] + t[i] - 1) / t[i], d = (a[i - 1] + a[i] - 1) / a[i],
      a[i] = max(c, d) * a[i], t[i] = max(c, d) * t[i];

    if (t[i] < t[i - 1])
      c = (t[i - 1] + t[i] - 1) / t[i], t[i] = c * t[i], a[i] = c * a[i];

    if (a[i] < a[i - 1])
      d = (a[i - 1] + a[i] - 1) / a[i], a[i] = d * a[i], t[i] = d * t[i];
  }
  cout << a[n] + t[n];
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;
int deco[1000];
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void Sequence() {
  int n;
  cin >> n;
  ll bt = 1;
  ll ba = 1;
  for (size_t i = 0; i < n; i++) {
    ll t, a;
    cin >> t >> a;
    ll g = gcd(t, a);
    ll ct = t / g;
    ll ca = a / g;
    ll rt = ct;
    ll ra = ca;
    while (rt < bt || ra < ba) {
      rt += ct;
      ra += ca;
    }
    bt = rt;
    ba = ra;
  }
  cout << (bt + ba) << endl;
}
int main() {
  Sequence();
  return 0;
}


Fixed submission:
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;
int deco[1000];
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void Sequence() {
  int n;
  cin >> n;
  ll bt = 1;
  ll ba = 1;
  for (size_t i = 0; i < n; i++) {
    ll t, a;
    cin >> t >> a;
    ll g = gcd(t, a);
    ll ct = t / g;
    ll ca = a / g;
    ll mul = max(bt / ct, ba / ca);
    ll rt = ct * mul;
    ll ra = ca * mul;

    while (rt < bt || ra < ba) {
      rt += ct;
      ra += ca;
    }
    bt = rt;
    ba = ra;
  }
  cout << (bt + ba) << endl;
}
int main() {
  Sequence();
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define int long long
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define itr(i, x) for (auto i = (x).begin(); i != (x).end(); ++i)
#define All(x) (x).begin(), (x).end()
#define gridif(a, x, b) if ((a) <= (x) && (x) < (b))
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;

int digitsum(int n, int b) {
  if (b < 2)
    return -1;
  if (n < b)
    return n;
  return digitsum(n / b, b) + n % b;
}
int mpow(int a, int x);
int m_inv(int n) { return mpow(n, mod - 2); }

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

signed main() {
  int N;
  cin >> N;
  int T = 1, A = 1;
  rep(i, N) {
    int t, a;
    cin >> t >> a;
    int j = 0;
    while (++j) {
      int tmpt, tmpa;
      tmpt = t * j;
      tmpa = a * j;
      if (tmpt >= T && tmpa >= A) {
        T = tmpt;
        A = tmpa;
        break;
      }
    }
  }
  cout << T + A << endl;
}

int mpow(int a, int x) {
  int ans = 1;
  while (x > 0) {
    if (x % 2 == 1)
      (ans *= a) %= mod;
    (a *= a) %= mod;
    x >>= 1;
  }
  return ans;
}

Fixed submission:
#include "bits/stdc++.h"
#define int long long
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define itr(i, x) for (auto i = (x).begin(); i != (x).end(); ++i)
#define All(x) (x).begin(), (x).end()
#define gridif(a, x, b) if ((a) <= (x) && (x) < (b))
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;

int digitsum(int n, int b) {
  if (b < 2)
    return -1;
  if (n < b)
    return n;
  return digitsum(n / b, b) + n % b;
}
int mpow(int a, int x);
int m_inv(int n) { return mpow(n, mod - 2); }

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

signed main() {
  int N;
  cin >> N;
  int T = 1, A = 1;
  rep(i, N) {
    int t, a;
    cin >> t >> a;
    int j = 0;
    int M = max((T - 1) / t, (A - 1) / a);
    M++;
    T = M * t;
    A = M * a;
  }
  cout << T + A << endl;
}

int mpow(int a, int x) {
  int ans = 1;
  while (x > 0) {
    if (x % 2 == 1)
      (ans *= a) %= mod;
    (a *= a) %= mod;
    x >>= 1;
  }
  return ans;
}
Problem p03964 (C++)

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

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<pair<ll, ll>> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i].first >> v[i].second;
  }
  ll t = v[0].first, a = v[0].second;
  for (int i = 1; i < n; i++) {
    ll tmp1 = v[i].first, tmp2 = v[i].second;
    while (true) {
      if (tmp1 >= t && tmp2 >= a) {
        t = tmp1;
        a = tmp2;
        break;
      }
      tmp1 += v[i].first;
      tmp2 += v[i].second;
    }
  }
  ll ans = t + a;
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<pair<ll, ll>> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i].first >> v[i].second;
  }
  ll t = v[0].first, a = v[0].second;
  for (int i = 1; i < n; i++) {
    ll n = (t + v[i].first - 1) / v[i].first;
    ll m = (a + v[i].second - 1) / v[i].second;
    ll tmp = max(n, m);
    t = tmp * v[i].first;
    a = tmp * v[i].second;
  }
  ll ans = t + a;
  cout << ans << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  long T, A;
  cin >> T >> A;
  long t, a, count;
  for (int i = 0; i < N - 1; i++) {
    cin >> t >> a;
    count = min(T / t, A / a);
    while (1) {
      if (T <= (t * count) && A <= (a * count)) {
        T = t * count, A = a * count;
        break;
      } else
        count++;
    }
  }
  cout << T + A << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  long T, A;
  cin >> T >> A;
  long t, a, count;
  for (int i = 0; i < N - 1; i++) {
    cin >> t >> a;
    count = max(T / t, A / a);
    while (1) {
      if (T <= (t * count) && A <= (a * count)) {
        T = t * count, A = a * count;
        break;
      } else
        count++;
    }
  }
  cout << T + A << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
typedef long long 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 l = t[0], r = a[0];
  for (ll i = 1; i < n; i++) {
    if (t[i] < l || a[i] < r) {
      ll j = 2;
      ll t_tmp = t[i];
      ll a_tmp = a[i];
      while (t_tmp < l || a_tmp < r) {
        t_tmp = t[i] * j;
        a_tmp = a[i] * j;
        j++;
      }
      l = t_tmp;
      r = a_tmp;
    } else {
      l = t[i];
      r = a[i];
    }
  }
  cout << l + r << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long 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 l = t[0], r = a[0];
  for (ll i = 1; i < n; i++) {
    if (t[i] < l || a[i] < r) {
      ll m = max((l - 1 + t[i]) / t[i], (r - 1 + a[i]) / a[i]);
      l = t[i] * m;
      r = a[i] * m;
    } else {
      l = t[i];
      r = a[i];
    }
  }
  cout << l + r << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
using ll = long long;
ll n, a[2000], b[2000], ans, x, y;
int i;
int main() {
  cin >> n;
  while (i < n)
    cin >> a[i] >> b[i], ++i;

  x = y = 1;
  i = 0;
  while (i < n) {
    ll r = max((x + a[i] - 1) / a[i], (y + b[i] - 1) / b[i]);
    x = a[i] * r, y = b[i] * r;
  }
  ans = x + y;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
using ll = long long;
ll n, a[2000], b[2000], ans, x, y;
int i;
int main() {
  cin >> n;
  while (i < n)
    cin >> a[i] >> b[i], ++i;

  x = y = 1;
  i = 0;
  while (i < n) {
    ll r = max((x + a[i] - 1) / a[i], (y + b[i] - 1) / b[i]);
    x = a[i] * r, y = b[i] * r;
    ++i;
  }
  ans = x + y;
  cout << ans << endl;
  return 0;
}
Problem p03964 (C++)

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

int main(void) {
  int N;

  cin >> N;

  long long int tn = 1, an = 1;
  for (int i = 0; i < N; ++i) {
    int T, A;
    cin >> T >> A;
    for (int p = max(max(tn / T, an / A), (long long int)1); p < 10e15; ++p) {
      if (T * p >= tn && A * p >= an) {
        tn = T * p;
        an = A * p;
        break;
      }
    }
  }

  cout << (long long int)tn + (long long int)an << endl;

  return 0;
}


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

int main(void) {
  int N;

  cin >> N;

  long long int tn = 1, an = 1;
  for (int i = 0; i < N; ++i) {
    int T, A;
    cin >> T >> A;
    long long int p = max(max(tn / T, an / A), (long long int)1);
    if (T * p >= tn && A * p >= an) {
      tn = T * p;
      an = A * p;
    } else {
      tn = T * (p + 1);
      an = A * (p + 1);
    }
  }

  cout << (long long int)tn + (long long int)an << endl;

  return 0;
}
Problem p03964 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<long long> T(N);
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> T.at(i) >> A.at(i);
  vector<long long> taka(N);
  vector<long long> aoki(N);
  taka.at(0) = T.at(0);
  aoki.at(0) = A.at(0);
  for (int i = 1; i < N; i++) {
    int j = 1;
    while (T.at(i) * j < taka.at(i - 1) || A.at(i) * j < aoki.at(i - 1))
      j++;
    taka.at(i) = T.at(i) * j;
    aoki.at(i) = A.at(i) * j;
  }
  cout << taka.at(N - 1) + aoki.at(N - 1) << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<long long> T(N);
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> T.at(i) >> A.at(i);
  vector<long long> taka(N);
  vector<long long> aoki(N);
  taka.at(0) = T.at(0);
  aoki.at(0) = A.at(0);
  for (int i = 1; i < N; i++) {
    long long j = 1;
    j = max((taka.at(i - 1) + T.at(i) - 1) / T.at(i),
            (aoki.at(i - 1) + A.at(i) - 1) / A.at(i));
    taka.at(i) = T.at(i) * j;
    aoki.at(i) = A.at(i) * j;
  }
  cout << taka.at(N - 1) + aoki.at(N - 1) << endl;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  ll T, A;
  cin >> T >> A;
  for (int i = 1; i < n; i++) {
    ll t, a;
    cin >> t >> a;
    ll x = t, y = a;
    while (t < T || a < A) {
      t += x;
      a += y;
    }
    T = t;
    A = a;
  }
  cout << A + T << "\n";

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  ll T, A;
  cin >> T >> A;
  for (int i = 1; i < n; i++) {
    ll t, a;
    cin >> t >> a;
    if (t < T && a >= A) {
      ll k = ceil((long double)T / t);
      t *= k;
      a *= k;
    } else if (t >= T && a < A) {
      ll k = ceil((long double)A / a);
      t *= k;
      a *= k;
    } else if (t < T && a < A) {
      ll k = max(ceil((long double)T / t), ceil((long double)A / a));
      t *= k;
      a *= k;
    }
    T = t;
    A = a;
  }
  cout << A + T << "\n";

  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n;
  int ans = 0;
  cin >> n;
  int a, b;
  cin >> a >> b;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (x == y) {
      a = max(a, b);
      b = max(a, b);
    } else {
      int &t1 = (x < y ? a : b);
      int &t2 = (x < y ? b : a);
      if (x > y)
        swap(x, y);
      while (t1 % x || y * (t1 / x) < t2)
        t1++;
      t2 = y * (t1 / x);
    }
  }
  cout << a + b << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n;
  int ans = 0;
  cin >> n;
  int a, b;
  cin >> a >> b;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (x == y) {
      a = max(a, b);
      b = max(a, b);
    } else {
      int &t1 = (x < y ? a : b);
      int &t2 = (x < y ? b : a);
      if (x > y)
        swap(x, y);
      int t = max((t1 + x - 1) / x, (t2 + y - 1) / y);
      t1 = t * x;
      t2 = t * y;
    }
  }
  cout << a + b << endl;
  return 0;
}
Problem p03964 (C++)

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

int main(void) {
  int N;
  cin >> N;
  long long A, B;
  cin >> A >> B;
  for (int i = 0; i < (N - 1); i++) {
    long long x, y;
    cin >> x >> y;
    int k = 1;
    while (k * x < A or k * y < B) {
      k++;
    }
    A = k * x;
    B = k * y;
  }
  cout << A + B << endl;
  return 0;
}

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

int main(void) {
  int N;
  cin >> N;
  long long A, B;
  cin >> A >> B;
  for (int i = 0; i < (N - 1); i++) {
    long long x, y;
    cin >> x >> y;
    long long k = max(A / x, B / y);
    while (k * x < A or k * y < B) {
      k++;
    }
    A = k * x;
    B = k * y;
  }
  cout << A + B << endl;
  return 0;
}
Problem p03964 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
ta = [tuple(int(x) for x in input().split()) for _ in range(n)]

a = 0
b = 0
for x, y in ta:
    if x >= a and y >= b:
        a = x
        b = y
    else:
        temp = 2
        while x * temp < a or y * temp < b:
            temp += 1
        a = x * temp
        b = y * temp

print(a + b)


Fixed submission:
n = int(input())
ta = [tuple(int(x) for x in input().split()) for _ in range(n)]

a = 0
b = 0
for x, y in ta:
    if x >= a and y >= b:
        a = x
        b = y
    else:
        temp = max(a // x, b // y)
        while x * temp < a or y * temp < b:
            temp += 1
        a = x * temp
        b = y * temp

print(a + b)
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
long long int n, a[1010][2];
long long int c, d, min, e, f, k, t;

int main() {
  int i;
  scanf("%lld", &n);
  scanf("%lld %lld", &c, &d);
  for (i = 1; i < n; i++) {
    scanf("%lld %lld", &e, &f);
    t = e;
    k = f;
    while (t < c || k < d) {
      t = t + e;
      k = k + f;
    }
    c = t;
    d = k;
  }
  printf("%lld\n", c + d);
}

Fixed submission:
#include <stdio.h>
long long int n, a[1010][2];
long long int c, d, min, e, f, k, t;

int main() {
  int i;
  scanf("%lld", &n);
  scanf("%lld %lld", &c, &d);
  for (i = 1; i < n; i++) {
    scanf("%lld %lld", &e, &f);
    if ((c / e) > (d / f)) {
      t = e * (c / e);
      k = f * (c / e);
    } else {
      t = e * (d / f);
      k = f * (d / f);
    }
    while (t < c || k < d) {
      t = t + e;
      k = k + f;
    }
    c = t;
    d = k;
  }
  printf("%lld\n", c + d);
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef vector<llong> V;

#define _                                                                      \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false);
llong n;
llong t, a, tt, aa, x;
llong ans = 0;
int main() {
  _;
  cin >> n;
  REP(i, n) {
    cin >> t >> a;
    x = 1;
    if (i == 0) {
      tt = t;
      aa = a;
      continue;
    }

    while (x * t - tt < 0 || x * a - aa < 0)
      ++x;
    tt = x * t;
    aa = x * a;
  }
  cout << x * (t + a) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef vector<llong> V;

#define _                                                                      \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false);
llong n;
llong t, a, tt, aa, x;
llong ans = 0;
int main() {
  _;
  cin >> n;
  REP(i, n) {
    cin >> t >> a;
    x = 1;
    if (i == 0) {
      tt = t;
      aa = a;
      continue;
    }

    x = max((tt + t - 1) / t, (aa + a - 1) / a);
    tt = x * t;
    aa = x * a;
  }
  cout << x * (t + a) << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Runtime Error):
#pragma target("avx")
#pragma optimize("O3")
#include <bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll, ll>;
using PP = std::pair<ll, P>;
using V = std::vector<ll>;
template <typename T> using pq = std::priority_queue<T>;
template <typename T>
using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

#define REP(i, b, e) for (ll i = b; i < e; i++)
#define ALL(vec) vec.begin(), vec.end()
#define PRINT(vec)                                                             \
  printf("[ ");                                                                \
  for (auto &i : vec)                                                          \
    printf("%ld ", i);                                                         \
  puts("]");
#define SCAN(vec)                                                              \
  for (auto &i : vec)                                                          \
  scanf("%ld", &i)
#define fi first
#define se second

const int MOD = 1e9 + 7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main() {
  int n;
  scanf("%d", &n);
  ll a, b;
  scanf("%ld %ld", &a, &b);
  while (n--) {
    ll na, nb;
    scanf("%ld %ld", &na, &nb);
    ll x = (a + na - 1) / na, y = (b + nb - 1) / nb;
    if (x > y)
      std::swap(x, y);
    if (a <= x * na and b <= x * nb)
      a = na * x, b = nb * x;
    else
      a = na * y, b = nb * y;
  }
  printf("%ld\n", a + b);
  return 0;
}


Fixed submission:
#pragma target("avx")
#pragma optimize("O3")
#include <bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll, ll>;
using PP = std::pair<ll, P>;
using V = std::vector<ll>;
template <typename T> using pq = std::priority_queue<T>;
template <typename T>
using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

#define REP(i, b, e) for (ll i = b; i < e; i++)
#define ALL(vec) vec.begin(), vec.end()
#define PRINT(vec)                                                             \
  printf("[ ");                                                                \
  for (auto &i : vec)                                                          \
    printf("%ld ", i);                                                         \
  puts("]");
#define SCAN(vec)                                                              \
  for (auto &i : vec)                                                          \
  scanf("%ld", &i)
#define fi first
#define se second

const int MOD = 1e9 + 7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main() {
  int n;
  scanf("%d", &n);
  ll a, b;
  scanf("%ld %ld", &a, &b);
  while (--n) {
    ll na, nb;
    scanf("%ld %ld", &na, &nb);
    ll x = (a + na - 1) / na, y = (b + nb - 1) / nb;
    if (x > y)
      std::swap(x, y);
    if (a <= x * na and b <= x * nb)
      a = na * x, b = nb * x;
    else
      a = na * y, b = nb * y;
  }
  printf("%ld\n", a + b);
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

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

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define PB push

#define MOD 1000000007LL
#define EPS 1e-10

int main() {
  int N;
  cin >> N;
  LL T, A;
  cin >> T >> A;
  REP(i, N - 1) {
    LL t, a;
    cin >> t >> a;
    LL tt, aa;
    tt = t;
    aa = a;
    while (tt < T || aa < A) {
      tt += t;
      aa += a;
    }
    T = tt;
    A = aa;
  }
  cout << T + A << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

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

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define PB push

#define MOD 1000000007LL
#define EPS 1e-10

int main() {
  int N;
  cin >> N;
  LL T, A;
  cin >> T >> A;
  REP(i, N - 1) {
    LL t, a;
    cin >> t >> a;
    LL nt = t * max((T + (t - 1)) / t, (A + (a - 1)) / a);
    LL na = a * max((T + (t - 1)) / t, (A + (a - 1)) / a);
    T = nt;
    A = na;
    // printf( "%lld : %lld = %lld : %lld\n", t, a, T, A );
  }
  cout << T + A << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  ll n, t, a;
  ll ans = 1, mnt = 1, mna = 1;
  cin >> n;
  rep(i, n) {
    cin >> t >> a;
    int sumt = t, suma = a;
    while (suma < mna || sumt < mnt) {
      suma += a;
      sumt += t;
    }
    mnt = sumt;
    mna = suma;
  }
  cout << mnt + mna << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  ll n, t, a;
  ll ans = 1, mnt = 1, mna = 1;
  cin >> n;
  rep(i, n) {
    cin >> t >> a;
    ll m =
        max(mna / a + (mna % a == 0 ? 0 : 1), mnt / t + (mnt % t == 0 ? 0 : 1));
    mnt = t * m;
    mna = a * m;
  }
  cout << mnt + mna << endl;
  return 0;
}
Problem p03964 (C++)

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

void solve() {
  int n;
  cin >> n;
  vector<int> t(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  unsigned long long ts = t[0], as = a[0];
  for (int i = 1; i < n; i++) {
    unsigned long long int x = ts / t[i];
    if (ts % t[i] != 0)
      x++;
    for (unsigned long long int j = x;; j++) {
      if (t[i] * j >= ts && a[i] * j >= as) {
        ts = t[i] * j;
        as = a[i] * j;
        break;
      }
    }
    // cout << ts << " " << as << endl;
  }
  cout << ts + as << endl;
  return;
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<int> t(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  unsigned long long ts = t[0], as = a[0];
  for (int i = 1; i < n; i++) {
    unsigned long long int x = ts / t[i];
    if (ts % t[i] != 0)
      x++;
    unsigned long long int y = as / a[i];
    if (as % a[i] != 0)
      y++;
    for (unsigned long long int j = max(x, y);; j++) {
      if (t[i] * j >= ts && a[i] * j >= as) {
        ts = t[i] * j;
        as = a[i] * j;
        break;
      }
    }
    // cout << ts << " " << as << endl;
  }
  cout << ts + as << endl;
  return;
}

int main() {
  solve();
  return 0;
}
Problem p03964 (C++)

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

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
const int inf = 1e9;
const ll linf = 1LL << 50;

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  ll l = 1, r = 1;
  for (int i = 0; i < n; i++) {
    int t, a;
    cin >> t >> a;
    ll tt = t, aa = a;
    while (a < l || t < r) {
      a += aa;
      t += tt;
    }
    l = a;
    r = t;
  }
  cout << l + r << endl;
  return 0;
}


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

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
const int inf = 1e9;
const ll linf = 1LL << 50;

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  ll l = 1, r = 1;
  for (int i = 0; i < n; i++) {
    int t, a;
    cin >> t >> a;
    ll pro = max((l + a - 1) / a, (r + t - 1) / t);
    l = a * pro;
    r = t * pro;
  }
  cout << l + r << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define POWER9 1000000000
#define MOD POWER9 + 7
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN (LL) - 9223372036854775807
#define LL_MAX (LL)9223372036854775807
#define PI 3.14159265359

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(9);

  int N;
  cin >> N;
  LL a, b;
  cin >> a >> b;
  for (int i = 1; i < N; i++) {
    LL x, y;
    cin >> x >> y;
    for (LL j = min(x / a, y / b); j <= INT_MAX; j++) {
      if (x * j >= a && y * j >= b) {
        a = x * j;
        b = y * j;
        break;
      }
    }
  }
  cout << a + b << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define POWER9 1000000000
#define MOD POWER9 + 7
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN (LL) - 9223372036854775807
#define LL_MAX (LL)9223372036854775807
#define PI 3.14159265359

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(9);

  int N;
  cin >> N;
  LL a, b;
  cin >> a >> b;
  for (int i = 1; i < N; i++) {
    LL x, y;
    cin >> x >> y;
    for (LL j = max(a / x, b / y) - 5; j <= LL_MAX; j++) {
      if (x * j >= a && y * j >= b) {
        a = x * j;
        b = y * j;
        break;
      }
    }
  }
  cout << a + b << endl;

  return 0;
}
Problem p03964 (C++)

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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  ll x = 0, y = 0;
  while (n--) {
    ll a, b;
    cin >> a >> b;
    for (ll i = 1;; i++) {
      if ((a * i >= x) && (b * i >= y)) {
        x = a * i;
        y = b * i;
        break;
      }
    }
  }
  cout << x + y << endl;
}


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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  ll x = 0, y = 0;
  while (n--) {
    ll a, b;
    cin >> a >> b;
    ll k = 1;
    if (x != 0)
      k = max(x / a, y / b);
    for (ll i = k;; i++) {
      if ((a * i >= x) && (b * i >= y)) {
        x = a * i;
        y = b * i;
        break;
      }
    }
  }
  cout << x + y << endl;
}