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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, n, m) for (int i = n; i < (int)(m); ++i)
#define ALL(x) x.begin(), x.end()
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define in(i, n) (0 <= (i) && (i) < (n))
#define on(bit, i) (((bit >> i) & 1) == 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
const int INF = 1e9 + 7;

int H, W;
string grid[300];

int memo[305][305];
int cost[305][305];

int calc(int w, int h1, int h2) {
  if (h1 == H and h2 == H)
    return 0;
  if (memo[h1][h2] != -1)
    return memo[h1][h2];
  int res = INF;
  int cst = cost[h1][h2];
  if (h1 < H)
    chmin(res, calc(w, h1 + 1, h2) + cst);
  if (h2 < H)
    chmin(res, calc(w, h1, h2 + 1) + cst);
  return memo[h1][h2] = res;
}

int main(void) {
  cin >> H >> W;
  REP(i, H) cin >> grid[i];
  int res = 0;
  REP(w, W - 1) {
    REP(i, H + 1) REP(j, H + 1) memo[i][j] = -1;
    for (int di = 0; di <= H; di++) {
      for (int dj = 0; dj <= H; dj++) {
        int cnt = 0;
        for (int k = max(di, dj); k < H; k++) {
          cnt += (grid[k - di][w] == grid[k - dj][w + 1]);
        }
        //	cout << di << " " << dj << " " << cnt << endl;
        cost[di][dj] = cnt;
        for (int k = 0; k < H - max(di, dj); k++) {
          cnt -= (grid[H - di - 1 - k][w] == grid[H - dj - 1 - k][w + 1]);
          cost[di + k + 1][dj + k + 1] = cnt;
        }
      }
    }
    res += calc(w, 0, 0);
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, n, m) for (int i = n; i < (int)(m); ++i)
#define ALL(x) x.begin(), x.end()
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define in(i, n) (0 <= (i) && (i) < (n))
#define on(bit, i) (((bit >> i) & 1) == 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
const int INF = 1e9 + 7;

int H, W;
string grid[300];

int memo[305][305];
int cost[305][305];

int calc(int w, int h1, int h2) {
  if (h1 == H and h2 == H)
    return 0;
  if (memo[h1][h2] != -1)
    return memo[h1][h2];
  int res = INF;
  int cst = cost[h1][h2];
  if (h1 < H)
    chmin(res, calc(w, h1 + 1, h2) + cst);
  if (h2 < H)
    chmin(res, calc(w, h1, h2 + 1) + cst);
  return memo[h1][h2] = res;
}

int main(void) {
  cin >> H >> W;
  REP(i, H) cin >> grid[i];
  int res = 0;
  REP(w, W - 1) {
    REP(i, H + 1) REP(j, H + 1) memo[i][j] = -1;
    for (int d = -H; d <= H; d++) {
      int di = -1, dj = -1;
      if (d >= 0) {
        di = 0;
        dj = d;
      } else {
        di = -d;
        dj = 0;
      }
      int cnt = 0;
      for (int k = max(di, dj); k < H; k++) {
        cnt += (grid[k - di][w] == grid[k - dj][w + 1]);
      }
      cost[di][dj] = cnt;
      for (int k = 0; k < H - max(di, dj); k++) {
        cnt -= (grid[H - di - 1 - k][w] == grid[H - dj - 1 - k][w + 1]);
        cost[di + k + 1][dj + k + 1] = cnt;
      }
    }
    res += calc(w, 0, 0);
  }
  cout << res << endl;
  return 0;
}
Problem p03962 (Python)

Buggy submission (status: Runtime Error):
a, b, c = map(int, input().split())
print(len(set(a, b, c)))


Fixed submission:
a, b, c = map(int, input().split())
print(len(set([a, b, c])))
Problem p03962 (Python)

Buggy submission (status: Runtime Error):
a, b, c = map(int, (input()))

if a == b and a == c:
    print(1)
elif a == b or a == c or b == c:
    print(2)
else:
    print(3)


Fixed submission:
a, b, c = map(int, input().split())

if a == b and a == c:
    print(1)
elif a == b or a == c or b == c:
    print(2)
else:
    print(3)
Problem p03962 (Python)

Buggy submission (status: Runtime Error):
from sys import stdin

a = map((int(x) for x in stdin.readline().split()))

print(len(a))


Fixed submission:
from sys import stdin

a = set((int(x) for x in stdin.readline().split()))

print(len(a))
Problem p03962 (Python)

Buggy submission (status: Runtime Error):
print(len({input().split()}))


Fixed submission:
print(len(set(input().split())))
Problem p03962 (Python)

Buggy submission (status: Runtime Error):
n = map(int, input().split())
i = 3

if n[0] == n[1]:
    i -= 1

if n[0] == n[2]:
    i -= 1

elif n[1] == n[2]:
    i -= 1

print(i)


Fixed submission:
n = list(map(int, input().split()))
i = 3

if n[0] == n[1]:
    i -= 1

if n[0] == n[2]:
    i -= 1

elif n[1] == n[2]:
    i -= 1

print(i)
Problem p03962 (C++)

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

int main() {
  int x[300];
  int a, b, c, res = 0;

  for (int i = 0; i <= 1000; i++)
    x[i] = 0;

  for (int i = 0; i <= 1000; i++) {
    cin >> a;
    x[a] += 1;
  }

  for (int i = 0; i <= 1000; i++) {
    if (x[i] != 0)
      res += 1;
  }

  cout << res << endl;

  return 0;
}

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

int main() {
  int x[3000];
  int a, b, c, res = 0;

  for (int i = 0; i <= 1000; i++)
    x[i] = 0;

  for (int i = 0; i <= 1000; i++) {
    cin >> a;
    x[a] += 1;
  }

  for (int i = 0; i <= 1000; i++) {
    if (x[i] != 0)
      res += 1;
  }

  cout << res << endl;

  return 0;
}
Problem p03962 (C++)

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

void Main() {
  vector<int> vec;
  rep(i, 3) cin >> vec[i];
  // unique vec
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  cout << vec.size() << endl;
}

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


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

void Main() {
  vector<int> vec(3);
  rep(i, 3) cin >> vec[i];
  // unique vec
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  cout << vec.size() << endl;
}

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

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

int main() {
  vector<bool> n(100, false);
  int ans = 0;
  for (int i = 0; i < 3; i++) {
    int a;
    cin >> a;
    if (n.at(a - 1) == false) {
      n.at(a) = true;
      ans++;
    }
  }
  cout << ans;
}

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

int main() {
  vector<bool> n(100, false);
  int ans = 0;
  for (int i = 0; i < 3; i++) {
    int a;
    cin >> a;
    if (n.at(a - 1) == false) {
      n.at(a - 1) = true;
      ans++;
    }
  }
  cout << ans;
}
Problem p03962 (C++)

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

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

  vector<bool> paintCans(100, false);
  paintCans.at(a) = true;
  paintCans.at(b) = true;
  paintCans.at(c) = true;

  int kindsNum(0);
  for (int i(0); i < paintCans.size(); i += 1) {
    if (paintCans.at(i))
      kindsNum += 1;
    if (kindsNum == 3)
      break;
  }

  cout << kindsNum << endl;

  return 0;
}

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

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

  vector<bool> paintCans(100, false);
  paintCans.at(a - 1) = true;
  paintCans.at(b - 1) = true;
  paintCans.at(c - 1) = true;

  int kindsNum(0);
  for (int i(0); i < paintCans.size(); i += 1) {
    if (paintCans.at(i))
      kindsNum += 1;
    if (kindsNum == 3)
      break;
  }

  cout << kindsNum << endl;

  return 0;
}
Problem p03962 (C++)

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

int main() {
  vector<int> x(100, 0);
  vector<int> color(3);
  for (int i = 0; i < 3; i++)
    cin >> color.at(i);
  for (int i = 0; i < 3; i++)
    x.at(color.at(i)) = 1;
  cout << accumulate(x.begin(), x.end(), 0) << endl;
}


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

int main() {
  vector<int> x(101, 0);
  vector<int> color(3);
  for (int i = 0; i < 3; i++)
    cin >> color.at(i);
  for (int i = 0; i < 3; i++)
    x.at(color.at(i)) = 1;
  cout << accumulate(x.begin(), x.end(), 0) << endl;
}
Problem p03962 (Python)

Buggy submission (status: Runtime Error):
print(len(set([input().split()])))


Fixed submission:
print(len(set(list(input().split()))))
Problem p03963 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
bool bo[100];
#define ll long long
ll dp[1001][1001];
ll f(ll n, ll r) {
  if (r == 1)
    return n;
  if (n == r)
    return 1;
  if (dp[n][r] != -1) {
    return dp[n][r];
  }
  dp[n][r] = f(n - 1, r) + f(n - 1, r - 1);
  return dp[n][r];
}

ll ff(ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return 1;
  return n * ff(n - 1);
}

int main() {
  for (int i = 0; i < 1001; i++) {
    for (int j = 0; j < 1001; j++) {
      dp[i][j] = -1;
    }
  }

  ll n, r;
  cin >> n >> r;
  // cout<<ff(4);
  cout << f(r, n) * ff(n) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
bool bo[100];
#define ll long long
ll dp[1001][1001];
ll f(ll n, ll r) {
  if (r == 1)
    return n;
  if (n == r)
    return 1;
  if (dp[n][r] != -1) {
    return dp[n][r];
  }
  dp[n][r] = f(n - 1, r) + f(n - 1, r - 1);
  return dp[n][r];
}

ll ff(ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return 1;
  return n * ff(n - 1);
}

int main() {
  for (int i = 0; i < 1001; i++) {
    for (int j = 0; j < 1001; j++) {
      dp[i][j] = -1;
    }
  }

  ll n, r;
  cin >> n >> r;
  if (r == 1 && n == 1) {
    cout << "0" << endl;
    return 0;
  } else if (r == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll res;
  res = r * (ll)pow(r - 1, n - 1);
  cout << res << "\n";
}
Problem p03963 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;

  long long ans = k;
  for (int i = 1; 1 < n; i++) {
    ans *= k - 1;
  }
  cout << ans << endl;

  return 0;
}


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

int main() {
  int n, k;
  cin >> n >> k;

  long long ans = k;
  for (int i = 1; i < n; i++) {
    ans *= k - 1;
  }
  cout << ans << endl;

  return 0;
}
Problem p03963 (C++)

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

long long pow(int b, int p) {
  if (p == 0)
    return 1;
  if (p % 2 == 1)
    return b * pow(b, p - 1);
  else {
    long long t = pow(b, p);
    return t * t;
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  cout << k * pow(k - 1, n - 1);
}


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

long long pow(int b, int p) {
  if (p == 0)
    return 1;
  if (p % 2 == 1)
    return b * pow(b, p - 1);
  else {
    long long t = pow(b, p / 2);
    return t * t;
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  cout << k * pow(k - 1, n - 1);
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1000000;
const int mod = 998244353;
const ll INF = 1LL << 60;
#define rep(i, n) for (ll i = (ll)0; i < (ll)n; ++i)
#define ALL(x) x.begin(), x.end()
#define pp pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define fix(n) fixed << setprecision(n)
#define itr vector<ll>::iterator
ll n, t[1010], a[1010], nowa, nowb;
int main() {
  cin >> n;
  rep(i, n) {
    cin >> t[i] >> a[i];
    if (i == 0)
      nowa = t[i], nowb = a[i];
    else {
      ll ct = 1;
      while ((t[i] * ct < nowa || a[i] * ct < nowb))
        ++ct;
      nowa = ct * t[i], nowb = ct * a[i];
    }
  }
  cout << nowa + nowb << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1000000;
const int mod = 998244353;
const ll INF = 1LL << 60;
#define rep(i, n) for (ll i = (ll)0; i < (ll)n; ++i)
#define ALL(x) x.begin(), x.end()
#define pp pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define fix(n) fixed << setprecision(n)
#define itr vector<ll>::iterator
ll n, t[1010], a[1010], nowa, nowb;
int main() {
  cin >> n;
  rep(i, n) {
    cin >> t[i] >> a[i];
    if (i == 0)
      nowa = t[i], nowb = a[i];
    else {
      ll ct = max(nowa / t[i] + 1 + (nowa % t[i] == 0 ? -1 : 0),
                  nowb / a[i] + 1 + (nowb % a[i] == 0 ? -1 : 0));
      nowa = ct * t[i], nowb = ct * a[i];
    }
  }
  cout << nowa + nowb << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
// RAM RAM

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define vi vector<int>
#define mp make_pair
#define arrayin(a, n)                                                          \
  for (int i = 0; i < n; i++) {                                                \
    cin >> a[i];                                                               \
  }
#define N 55

using namespace std;

pair<int, int> ratioo(int a, int b) {
  if (__gcd(a, b) == 1) {
    return mp(a, b);
  }
  int temp = __gcd(a, b);
  a = a / temp;
  b = b / temp;
  return mp(a, b);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int t;
  cin >> t;
  int a, b;
  cin >> a >> b;
  t--;
  while (t--) {
    int x, y;
    cin >> x >> y;
    int s = 1;
    if (x < y) {
      s = a / x;
    } else {
      s = b / y;
    }
    for (int i = s;; i++) {
      if (x * i >= a && y * i >= b) {
        a = x * i;
        b = y * i;
        break;
      }
    }
  }
  cout << a + b << endl;
  return 0;
}

Fixed submission:
// RAM RAM

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define vi vector<int>
#define mp make_pair
#define arrayin(a, n)                                                          \
  for (int i = 0; i < n; i++) {                                                \
    cin >> a[i];                                                               \
  }
#define N 55

using namespace std;

pair<int, int> ratioo(int a, int b) {
  if (__gcd(a, b) == 1) {
    return mp(a, b);
  }
  int temp = __gcd(a, b);
  a = a / temp;
  b = b / temp;
  return mp(a, b);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int t;
  cin >> t;
  int a, b;
  cin >> a >> b;
  t--;
  while (t--) {
    int x, y;
    cin >> x >> y;
    int s = 1;
    s = max(a / x, b / y);
    for (int i = s;; i++) {
      if (x * i >= a && y * i >= b) {
        a = x * i;
        b = y * i;
        break;
      }
    }
  }
  cout << a + b << endl;
  return 0;
}
Problem p03964 (C++)

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

using namespace std;

typedef long long ll;

#define MOD 1000000007

int main() {
  int n;
  cin >> n;
  vector<int> t(n), a(n);
  for (int i = 0; i < n; ++i)
    cin >> t[i] >> a[i];
  ll tk = 1, ao = 1;
  for (int i = 0; i < n; ++i) {
    while (tk * a[i] != ao * t[i]) {
      if (tk * a[i] < ao * t[i]) {
        tk++;
      } else {
        ao++;
      }
    }
  }
  cout << tk + ao << endl;
  return 0;
}

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

using namespace std;

typedef long long ll;

#define MOD 1000000007

int main() {
  int n;
  cin >> n;
  vector<int> t(n), a(n);
  for (int i = 0; i < n; ++i)
    cin >> t[i] >> a[i];
  ll tk = 1, ao = 1;
  for (int i = 0; i < n; ++i) {
    ll res1 = (tk + a[i] - 1) / a[i];
    ll res2 = (ao + t[i] - 1) / t[i];
    tk = max(res1, res2) * a[i];
    ao = max(res1, res2) * t[i];
  }
  cout << tk + ao << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

int main() {
  int n;
  cin >> n;
  ll ta = 1, ao = 1;
  for (int i = 0; i < n; i++) {
    ll t, a;
    cin >> t >> a;
    ll tt = t, aa = a;
    while (ta > tt || ao > aa) {
      tt += t;
      aa += a;
    }
    ta = tt;
    ao = aa;
  }
  cout << ta + ao << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

int main() {
  int n;
  cin >> n;
  ll ta = 1, ao = 1;
  for (int i = 0; i < n; i++) {
    ll t, a;
    cin >> t >> a;
    ll z = max((ta + t - 1) / t, (ao + a - 1) / a);
    ta = z * t;
    ao = z * a;
  }
  cout << ta + ao << endl;
}
Problem p03964 (C++)

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

using ll = int64_t;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)

#define rng(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

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

  int n;
  std::cin >> n;
  ll t, a;
  std::cin >> t >> a;

  rep(i, n) {
    ll ti, ai;
    std::cin >> ti >> ai;
    ll div = max((t + ti - 1) / ti, (a + ai - 1) / ai);
    a = div * ai, t = div * ti;
  }
  std::cout << t + a << "\n";
}


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

using ll = int64_t;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)

#define rng(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

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

  int n;
  std::cin >> n;
  ll t, a;
  std::cin >> t >> a;

  rep(i, n - 1) {
    ll ti, ai;
    std::cin >> ti >> ai;
    ll div = max((t + ti - 1) / ti, (a + ai - 1) / ai);
    a = div * ai, t = div * ti;
  }
  std::cout << t + a << "\n";
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 50;

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

void solve() {
  int n;
  cin >> n;
  ll t, a;
  cin >> t >> a;
  ll x = t, y = a;
  rep(i, n - 1) {
    cin >> t >> a;
    ll cnt = 1;
    while (cnt * t < x || cnt * a < y) {
      ++cnt;
    }
    x = cnt * t;
    y = cnt * a;
  }
  cout << x + y << "\n";
}

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


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 50;

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

void solve() {
  int n;
  cin >> n;
  ll t, a;
  cin >> t >> a;
  ll x = t, y = a;
  rep(i, n - 1) {
    cin >> t >> a;
    ll maxv = max((x + t - 1) / t, (y + a - 1) / a);
    x = maxv * t;
    y = maxv * a;
  }
  cout << x + y << "\n";
}

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

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

#define int long long

using namespace std;
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007, INF = 1e18;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};

int n;

signed main() {
  cin >> n;
  int x = 1, y = 1;
  rep(i, 0, n) {
    int a, b;
    cin >> a >> b;
    while (x * b != y * a) {
      if (x * b > y * a)
        y++;
      else
        x++;
    }
  }
  cout << x + y << endl;

  return 0;
}

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

#define int long long

using namespace std;
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007, INF = 1e18;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};

int n;

signed main() {
  cin >> n;
  int x = 1, y = 1;
  rep(i, 0, n) {
    int a, b;
    cin >> a >> b;
    int z = max((a + x - 1) / a, (b + y - 1) / b);
    x = a * z;
    y = b * z;
  }
  cout << x + y << endl;

  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

  int n;
  cin >> n;
  ll x = 1, y = 1;
  cin >> x >> y;
  rep(i, 0, n) {
    ll t, a;
    cin >> t >> a;
    ll max_val = max((x + t - 1) / t, (y + a - 1) / a);
    x = max_val * t;
    y = max_val * a;
  }
  cout << x + y << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> ts(n), as(n);
  rep(i, n) cin >> ts.at(i) >> as.at(i);

  ll t = 1, a = 1;
  rep(i, n) {
    ll nextt = ts.at(i), nexta = as.at(i);
    while (nextt < t || nexta < a) {
      nextt += ts.at(i);
      nexta += as.at(i);
    }
    t = nextt;
    a = nexta;
  }

  cout << t + a << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> ts(n), as(n);
  rep(i, n) cin >> ts.at(i) >> as.at(i);

  ll t = 1, a = 1;
  rep(i, n) {
    ll x = (t + ts.at(i) - 1) / ts.at(i);
    ll y = (a + as.at(i) - 1) / as.at(i);
    t = max(x, y) * ts.at(i);
    a = max(x, y) * as.at(i);
  }

  cout << t + a << endl;
}
Problem p03964 (C++)

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

const int INF = 1e7;
pair<long long, long long> calculate(long long a, long long b, long long x,
                                     long long y) {
  for (int i = 1; i <= max(x / a, y / b) + 1; ++i) {
    if (i * a >= x && i * b >= y) {
      return make_pair(i * a, i * b);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  long long x, y;
  cin >> x >> y;
  for (int i = 0; i < (n - 1); ++i) {
    long long a, b;
    cin >> a >> b;
    if (a >= x && b >= y) {
      x = a;
      y = b;
      continue;
    } else {
      pair<long long, long long> tmp = calculate(a, b, x, y);
      x = tmp.first;
      y = tmp.second;
    }
  }
  cout << x + y << endl;

  return 0;
}


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

const int INF = 1e7;
pair<long long, long long> calculate(long long a, long long b, long long x,
                                     long long y) {
  long long a1 = x / a;
  a1 += x % a == 0 ? 0 : 1;
  long long b1 = y / b;
  b1 += y % b == 0 ? 0 : 1;
  long long mn = max(a1, b1);
  return make_pair(a * mn, b * mn);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  long long x, y;
  cin >> x >> y;
  for (int i = 0; i < (n - 1); ++i) {
    long long a, b;
    cin >> a >> b;
    if (a >= x && b >= y) {
      x = a;
      y = b;
      continue;
    } else {
      pair<long long, long long> tmp = calculate(a, b, x, y);
      x = tmp.first;
      y = tmp.second;
    }
  }
  cout << x + y << endl;

  return 0;
}
Problem p03964 (C++)

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

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

int main() {
  int N;
  cin >> N;
  vector<int> T(N), A(N);
  rep(i, N) cin >> T[i] >> A[i];
  ll t = T[0], a = A[0];
  for (int i = 1; i < N; i++) {
    for (ll j = 1;; j++) {
      if (T[i] * j >= t && A[i] * j >= a) {
        t = T[i] * j;
        a = A[i] * j;
        break;
      }
    }
  }
  cout << t + a << endl;
  return 0;
}

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

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

int main() {
  int N;
  cin >> N;
  vector<int> T(N), A(N);
  rep(i, N) cin >> T[i] >> A[i];
  vector<ll> d(N);
  d[0] = 1;
  for (int i = 1; i < N; i++)
    d[i] = max((d[i - 1] * T[i - 1] + T[i] - 1) / T[i],
               (d[i - 1] * A[i - 1] + A[i] - 1) / A[i]);
  cout << T[N - 1] * d[N - 1] + A[N - 1] * d[N - 1] << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long n, t, a, tt, aa;
  cin >> n >> tt >> aa;
  for (int i = 1; i < n; i++) {
    cin >> t >> a;
    for (int j = max(tt / t, aa / a) - 1;; j++) {
      if (t * j >= tt && a * j >= aa) {
        tt = t * j;
        aa = a * j;
        break;
      }
    }
  }
  cout << tt + aa << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long n, t, a, tt, aa;
  cin >> n >> tt >> aa;
  for (int i = 1; i < n; i++) {
    cin >> t >> a;
    for (long j = max(tt / t, aa / a) - 1;; j++) {
      if (t * j >= tt && a * j >= aa) {
        tt = t * j;
        aa = a * j;
        break;
      }
    }
  }
  cout << tt + aa << endl;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  typedef long long ll;
  ll T, A;
  cin >> T >> A;
  ll t = T, a = A;
  for (int i = 1; i < N; ++i) {
    cin >> T >> A;
    ll j = 1;
    for (; (j * T < t) || (j * A < a); ++j)
      ;
    t = j * T;
    a = j * A;
  }
  cout << t + a;
  return 0;
}


Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  typedef long long ll;
  ll T, A;
  cin >> T >> A;
  ll t = T, a = A;
  for (int i = 1; i < N; ++i) {
    cin >> T >> A;
    ll m = t / T + ((0 == t % T) ? 0 : 1);
    ll n = a / A + ((0 == a % A) ? 0 : 1);
    ll j = ((m < n) ? n : m);
    t = j * T;
    a = j * A;
  }
  cout << t + a;
  return 0;
}
Problem p03964 (C++)

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

ll N;
vector<ll> t, a;
vector<ll> thyou, ahyou;

int main() {
  cin >> N;
  t.resize(N);
  a.resize(N);
  thyou.resize(N);
  ahyou.resize(N);
  for (int i = 0; i < N; i++)
    scanf("%Ld %Ld", &t[i], &a[i]);

  thyou[0] = t[0];
  ahyou[0] = a[0];
  for (int i = 1; i < N; i++) {
    for (int j = max(thyou[i - 1] / t[i], ahyou[i - 1] / a[i]); 1; j++) {
      if (thyou[i - 1] <= j * t[i] && ahyou[i - 1] <= j * a[i]) {
        thyou[i] = j * t[i];
        ahyou[i] = j * a[i];
        break;
      }
    }
  }
  cout << thyou[N - 1] + ahyou[N - 1] << endl;
  return 0;
}

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

ll N;
vector<ll> t, a;
vector<ll> thyou, ahyou;

int main() {
  cin >> N;
  t.resize(N);
  a.resize(N);
  thyou.resize(N);
  ahyou.resize(N);
  for (int i = 0; i < N; i++)
    scanf("%Ld %Ld", &t[i], &a[i]);

  thyou[0] = t[0];
  ahyou[0] = a[0];
  for (int i = 1; i < N; i++) {
    for (ll j = max(thyou[i - 1] / t[i], ahyou[i - 1] / a[i]); 1; j++) {
      if (thyou[i - 1] <= j * t[i] && ahyou[i - 1] <= j * a[i]) {
        thyou[i] = j * t[i];
        ahyou[i] = j * a[i];
        break;
      }
    }
  }
  cout << thyou[N - 1] + ahyou[N - 1] << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007
#define irep(i, n) for (int i = 0; i < n; i++)
#define irep2(i, a, n) for (int i = a; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define lrep2(i, a, n) for (long long i = a; i < n; i++)

typedef long long ll;
typedef std::vector<int> v_int;
typedef std::vector<std::vector<int>> v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<std::vector<ll>> v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<std::vector<std::string>> v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<std::vector<bool>> v2_bool;
typedef std::pair<ll, ll> pll;

using namespace std;

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

  irep(i, n) { cin >> a[i] >> b[i]; }

  ll ans = 2;

  ll s = a[0];
  ll t = b[0];

  irep2(i, 1, n) {
    if (s * b[i] != t * a[i]) {
      ll j = min(s / a[i], t / b[i]);
      while (1) {
        if (s <= a[i] * j && t <= b[i] * j) {
          s = a[i] * j;
          t = b[i] * j;
          break;
        } else {
          j++;
        }
      }
    }
  }
  cout << s + t << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007
#define irep(i, n) for (int i = 0; i < n; i++)
#define irep2(i, a, n) for (int i = a; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define lrep2(i, a, n) for (long long i = a; i < n; i++)

typedef long long ll;
typedef std::vector<int> v_int;
typedef std::vector<std::vector<int>> v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<std::vector<ll>> v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<std::vector<std::string>> v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<std::vector<bool>> v2_bool;
typedef std::pair<ll, ll> pll;

using namespace std;

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

  irep(i, n) { cin >> a[i] >> b[i]; }

  ll ans = 2;

  ll s = a[0];
  ll t = b[0];

  irep2(i, 1, n) {
    if (s * b[i] != t * a[i]) {
      ll j = max(s / a[i], t / b[i]);
      while (1) {
        if (s <= a[i] * j && t <= b[i] * j) {
          s = a[i] * j;
          t = b[i] * j;
          break;
        } else {
          j++;
        }
      }
    }
  }
  cout << s + t << endl;
  return 0;
}
Problem p03964 (Python)

Buggy submission (status: Time Limit Exceeded):
import math

N = int(input())

t = 0
a = 0
for i in range(N):
    T, A = map(int, input().split())
    if i == 0:
        t += T
        a += A
        continue
    if T >= t and A >= a:
        t = T
        a = A
        continue

    max_ta = max(t, a)
    g = math.gcd(t, a)
    tg, ag = t // g, a // g
    if (T, A) != (tg, ag):
        sum_t, sum_a = T * max_ta, A * max_ta
        # print(sum_t, sum_a)
        while True:
            sum_t -= T
            sum_a -= A
            if sum_t < t or sum_a < a:
                sum_t += T
                sum_a += A
                break
        t = sum_t
        a = sum_a
        continue

print(t + a)


Fixed submission:
import math

N = int(input())

t = 0
a = 0
for i in range(N):
    T, A = map(int, input().split())
    if i == 0:
        t += T
        a += A
        continue
    if T >= t and A >= a:
        t = T
        a = A
        continue

    max_ta = max(t, a)
    g = math.gcd(t, a)
    tg, ag = t // g, a // g
    if (T, A) != (tg, ag):
        sum_t, sum_a = T * max_ta, A * max_ta
        # print(sum_t, sum_a)

        cout_sn = min((sum_t - t) // T, (sum_a - a) // A)
        # while True:
        sum_t -= T * cout_sn
        sum_a -= A * cout_sn
        # if sum_t < t or sum_a < a:
        #     sum_t += T
        #     sum_a += A
        #     break
        t = sum_t
        a = sum_a
        continue

print(t + a)
Problem p03964 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())

t, a = map(int, input().split())

for i in range(N - 1):
    nt, na = map(int, input().split())
    pt, pa = nt, na
    while pt < t or pa < a:
        pt += nt
        pa += na
    t = pt
    a = pa
print(t + a)


Fixed submission:
N = int(input())

t, a = map(int, input().split())

for i in range(N - 1):
    nt, na = map(int, input().split())
    p = max(t // nt, a // na)
    pt = nt * p
    pa = na * p
    while pt < t or pa < a:
        pt += nt
        pa += na
    t = pt
    a = pa
print(t + a)
Problem p03964 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    TA = [[int(x) for x in input().split()] for _ in range(N)]

    now = TA[0]
    for t, a in TA[1:]:
        i = 1
        while now[0] > t * i or now[1] > a * i:
            i += 1
        now[0] = t * i
        now[1] = a * i

    print(now[0] + now[1])


if __name__ == "__main__":
    main()


Fixed submission:
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    TA = [[int(x) for x in input().split()] for _ in range(N)]

    now = TA[0]
    for t, a in TA[1:]:
        i = max(now[0] // t, now[1] // a)
        while now[0] > t * i or now[1] > a * i:
            i += 1
        now[0] = t * i
        now[1] = a * i

    print(now[0] + now[1])


if __name__ == "__main__":
    main()
Problem p03964 (C++)

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

#define PI 3.1415926535897932
#define MOD 1000000007
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;

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

ll lcm(int a, int b) { return a / gcd(a, b) * b; }

void solve() {
  ll N;
  cin >> N;
  ll ans = 0;
  ll T0, A0;
  cin >> T0 >> A0;
  ans = T0 + A0;
  REP1(i, N - 1) {
    ll T, A;
    cin >> T >> A;
    ll P = T, Q = A;
    while (P < T0 || Q < A0) {
      P += T;
      Q += A;
    }
    ans = P + Q;
    T0 = P;
    A0 = Q;
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}

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

#define PI 3.1415926535897932
#define MOD 1000000007
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;

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

ll lcm(int a, int b) { return a / gcd(a, b) * b; }

void solve() {
  ll N;
  cin >> N;
  ll ans = 0;
  ll T0, A0;
  cin >> T0 >> A0;
  ans = T0 + A0;
  REP1(i, N - 1) {
    ll T, A;
    cin >> T >> A;
    ll S = max((T0 + T - 1) / T, (A0 + A - 1) / A);
    T *= S;
    A *= S;
    T0 = T;
    A0 = A;
    ans = T0 + A0;
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const ll inf = 1e9 + 7;

int main() {
  fast;

  ll n;
  cin >> n;
  ll a = 0, b = 0;
  for (ll i = 0, x, y; i < n; i++) {
    cin >> x >> y;

    if (i == 0) {
      a = x, b = y;
      continue;
    }

    for (ll j = 1, x1, y1;; j++) {
      x1 = x * j;
      y1 = y * j;
      if (x1 >= a && y1 >= b) {
        a = x1, b = y1;
        break;
      }
    }
  }

  cout << a + b;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const ll inf = 1e9 + 7;

int main() {
  fast;

  ll n;
  cin >> n;
  ll a = 0, b = 0;
  for (ll i = 0, x, y; i < n; i++) {
    cin >> x >> y;

    if (i == 0) {
      a = x, b = y;
      continue;
    }

    ll x1, y1;
    x1 = ceil((long double)a / (long double)x);
    y1 = ceil((long double)b / (long double)y);

    ll curl = max(x1, y1);

    // cout<<curl<<"\n";
    a = x * curl;
    b = y * curl;
  }

  cout << a + b;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e16;
int main() {
  int n;
  cin >> n;
  ll x = 1, y = 1;
  ll a, b;
  ll r, l;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    l = 1, r = INF;
    while (l < r) {
      ll mid = (l + r) / 2;
      if (mid * a >= x && mid * b >= y)
        r = mid;
      else
        l = mid + 1;
    }
    while (l * a < x || l * b < y)
      l++;
    x = l * a;
    y = l * b;
  }
  cout << x + y << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e16;
int main() {
  int n;
  cin >> n;
  ll x = 1, y = 1;
  ll a, b;
  ll r, l;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    l = 1, r = INF;
    while (l < r) {
      ll mid = (l + r) / 2;
      if (mid * a >= x && mid * b >= y)
        r = mid;
      else
        l = mid + 1;
    }
    if (l * a < x || l * b < y) {
      l = l + max((x - l * a) / a, (y - b * l) / b);
    }
    while (l * a < x || l * b < y)
      l++;
    x = l * a;
    y = l * b;
  }
  cout << x + y << endl;
  return 0;
}
Problem p03964 (C++)

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

int main() {
  int n;
  cin >> n;
  int t1, t2;
  int a = 0, b = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &t1, &t2);
    while (1) {
      if ((double)t1 / t2 == (double)a / b)
        break;
      if ((double)t1 / t2 > (double)a / b)
        a++;
      else
        b++;
    }
  }
  cout << a + b;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;

int main() {
  int t;
  cin >> t;
  ll a = 1, b = 1, t1, t2, n;
  while (t--) {
    cin >> t1 >> t2;
    n = max(a / t1, b / t2);
    if (n * t1 < a || n * t2 < b)
      n++;
    a = n * t1, b = n * t2;
  }
  cout << a + b;
}
Problem p03964 (C++)

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

using namespace std;

int main() {
  int n, i;
  long long a[1000]{0}, b[1000]{0}, aa[1000]{0}, bb[1000]{0}, c, d;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }

  aa[0] = a[0];
  bb[0] = b[0];

  for (i = 1; i < n; i++) {
    c = aa[i - 1] / a[i];
    aa[i] = a[i] * c;
    bb[i] = b[i] * c;
    while (-1) {
      if (aa[i] < aa[i - 1] || bb[i] < bb[i - 1]) {
        aa[i] += a[i];
        bb[i] += b[i];
      } else
        break;
    }
  }

  cout << aa[n - 1] + bb[n - 1] << endl;

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n, i;
  long long a[1000]{0}, b[1000]{0}, aa[1000]{0}, bb[1000]{0}, c, d;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }

  aa[0] = a[0];
  bb[0] = b[0];

  for (i = 1; i < n; i++) {
    c = aa[i - 1] / a[i];
    d = bb[i - 1] / b[i];
    if (c < d)
      c = d;
    aa[i] = a[i] * c;
    bb[i] = b[i] * c;
    while (-1) {
      if (aa[i] < aa[i - 1] || bb[i] < bb[i - 1]) {
        aa[i] += a[i];
        bb[i] += b[i];
      } else
        break;
    }
  }

  cout << aa[n - 1] + bb[n - 1] << endl;

  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t, a;
  vector<pair<int, int>> v;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &t);
    v.push_back(make_pair(a, t));
  }
  long long an = v[0].first, tn = v[0].second;
  for (int i = 1; i < n; i++) {
    if ((v[i].first >= an) && (v[i].second >= tn))
      an = v[i].first, tn = v[i].second;
    else {
      int j = 2;
      while (1) {
        if ((v[i].first * j >= an) && (v[i].second * j >= tn)) {
          an = v[i].first * j;
          tn = v[i].second * j;
          break;
        }
        j++;
      }
    }
  }
  cout << an + tn;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t, a;
  vector<pair<int, int>> v;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &t);
    v.push_back(make_pair(a, t));
  }
  long long an = v[0].first, tn = v[0].second;
  for (int i = 1; i < n; i++) {
    if ((v[i].first >= an) && (v[i].second >= tn))
      an = v[i].first, tn = v[i].second;
    else {
      long long j = max((an + v[i].first - 1) / v[i].first,
                        (tn + v[i].second - 1) / v[i].second);
      an = j * v[i].first;
      tn = j * v[i].second;
    }
  }
  cout << an + tn;
  return 0;
}