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

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

#ifndef _DEBUG
#define main_ main
#endif
#define FOR(i, s, e) for (int i = int(s); i != int(e); i++)
#define INF (INT_MAX / 2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX / 2)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef vector<bool> vb;
typedef vector<string> vs;

template <typename T> using keyVal = pair<string, T>;
template <typename T>
bool val_greater(const keyVal<T> &left, const keyVal<T> &right) {
  return left.second > right.second;
}

void init_global() {}

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

  int N;
  cin >> N;
  vi T(N), A(N);
  FOR(i, 0, N) cin >> T[i] >> A[i];

  vl nt(N), na(N);
  nt[0] = T[0];
  na[0] = A[0];

  FOR(i, 1, N) {
    ll r = 1;
    while (true) {
      if (T[i] * r >= nt[i - 1] && A[i] * r >= na[i - 1])
        break;
      r++;
    }
    nt[i] = T[i] * r;
    na[i] = A[i] * r;
  }

  ll ans = nt[N - 1] + na[N - 1];
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

#ifndef _DEBUG
#define main_ main
#endif
#define FOR(i, s, e) for (int i = int(s); i != int(e); i++)
#define INF (INT_MAX / 2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX / 2)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef vector<bool> vb;
typedef vector<string> vs;

template <typename T> using keyVal = pair<string, T>;
template <typename T>
bool val_greater(const keyVal<T> &left, const keyVal<T> &right) {
  return left.second > right.second;
}

void init_global() {}

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

  int N;
  cin >> N;
  vi T(N), A(N);
  FOR(i, 0, N) cin >> T[i] >> A[i];

  vl nt(N), na(N);
  nt[0] = T[0];
  na[0] = A[0];

  FOR(i, 1, N) {
    /*		ll r = 1;
                    while (true) {
                            if (T[i] * r >= nt[i - 1] &&
                                    A[i] * r >= na[i - 1]) break;
                            r++;
                    }*/
    ll r = max((nt[i - 1] + T[i] - 1) / T[i], (na[i - 1] + A[i] - 1) / A[i]);

    nt[i] = T[i] * r;
    na[i] = A[i] * r;
  }

  ll ans = nt[N - 1] + na[N - 1];
  cout << ans << endl;
  return 0;
}
Problem p03964 (C++)

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

#define int long long

#define REP(i, n) for (int i = 0; i < n; 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 pb(x) push_back(x)
#define all(x) ((x).begin(), (x).end())
#define mp(x, y) make_pair(x, y)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 99999999999
#define MOD (int)(1e9) + 7

using namespace std;

int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};

int32_t main() {
  int n;
  cin >> n;
  vector<int> t(n), a(n);
  REP(i, n) cin >> t[i] >> a[i];

  FOR(i, 1, n) {
    int cnt = 1;
    while (t[i] * cnt < t[i - 1] || a[i] * cnt < a[i - 1]) {
      cnt++;
    }
    t[i] *= cnt;
    a[i] *= cnt;
  }

  cout << t[n - 1] + a[n - 1] << endl;
}

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

#define int long long

#define REP(i, n) for (int i = 0; i < n; 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 pb(x) push_back(x)
#define all(x) ((x).begin(), (x).end())
#define mp(x, y) make_pair(x, y)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 99999999999
#define MOD (int)(1e9) + 7

using namespace std;

int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};

int32_t main() {
  int n;
  cin >> n;
  vector<int> t(n), a(n);
  REP(i, n) cin >> t[i] >> a[i];

  FOR(i, 1, n) {
    int p = t[i - 1] / t[i];
    int q = a[i - 1] / a[i];
    if (t[i - 1] % t[i])
      p++;
    if (a[i - 1] % a[i])
      q++;
    int cnt = max(p, q);

    t[i] *= cnt;
    a[i] *= cnt;
  }

  cout << t[n - 1] + a[n - 1] << endl;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// #define pair pair<ll,ll>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

const ll mod = 1e9 + 7;

int main() {

  ll n;
  cin >> n;
  pair<ll, ll> p[n];
  for (ll i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }

  ll x = p[0].first, y = p[0].second;
  // cout<<x<<" "<<y<<endl;
  for (ll i = 1; i < n; i++) {
    ll a = p[i].first;
    ll b = p[i].second;
    ll c = a * y - b * x;
    for (ll l = 1;; l++) {
      ll X = l * a - x;
      ll Y = l * b - y;
      if (X >= 0 && Y >= 0) {
        x = a * l;
        y = b * l;
        // cout<<x<<" "<<y<<endl;
        break;
      }
    }
  }
  cout << x + y << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// #define pair pair<ll,ll>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

const ll mod = 1e9 + 7;

int main() {

  ll n;
  cin >> n;
  pair<ll, ll> p[n];
  for (ll i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }

  ll x = p[0].first, y = p[0].second;
  // cout<<x<<" "<<y<<endl;
  for (ll i = 1; i < n; i++) {
    ll a = p[i].first;
    ll b = p[i].second;
    ll c = a * y - b * x;
    for (ll l = max(x / a, y / b);; l++) {
      ll X = l * a - x;
      ll Y = l * b - y;
      if (X >= 0 && Y >= 0) {
        x = a * l;
        y = b * l;
        // cout<<x<<" "<<y<<endl;
        break;
      }
    }
  }
  cout << x + y << 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 t[n];
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  long long int tmpt, tmpa;
  for (int i = 1; i < n; i++) {
    tmpt = t[i];
    tmpa = a[i];
    for (int j = 1; j < 999999999; j++) {
      t[i] = tmpt * j;
      a[i] = tmpa * j;
      // cout << t[i] << " " << a[i] << endl;
      if (t[i] >= t[i - 1] && a[i] >= a[i - 1]) {
        break;
      }
    }
  }
  cout << t[n - 1] + a[n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int t[n];
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  long long int tmpt, tmpa;
  for (int i = 1; i < n; i++) {
    tmpt = t[i];
    tmpa = a[i];
    t[i] =
        t[i] * max((t[i - 1] + tmpt - 1) / tmpt, (a[i - 1] + tmpa - 1) / tmpa);
    a[i] =
        a[i] * max((t[i - 1] + tmpt - 1) / tmpt, (a[i - 1] + tmpa - 1) / tmpa);
    // cout << t[i] << " " << a[i] << endl;
  }
  cout << t[n - 1] + a[n - 1] << endl;
}
Problem p03964 (C++)

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
template <class T> using V = vector<T>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
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;
}

int main() {

  int n;
  cin >> n;

  ll takahashi = 1, aoki = 1;

  rep(i, n) {
    ll t, a;
    cin >> t >> a;
    ll roop = 1;
    while (t * roop < takahashi || a * roop < aoki)
      roop++;
    takahashi = t * roop, aoki = a * roop;
    // cout << takahashi << " " << aoki << endl;
  }

  ll ans = takahashi + aoki;
  cout << ans << endl;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
template <class T> using V = vector<T>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
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;
}

int main() {

  int n;
  cin >> n;

  ll takahashi = 1, aoki = 1;

  rep(i, n) {
    ll t, a;
    cin >> t >> a;
    ll roop = max(ceil(takahashi, t), ceil(aoki, a));
    takahashi = t * roop, aoki = a * roop;
    // cout << takahashi << " " << aoki << endl;
  }

  ll ans = takahashi + aoki;
  cout << ans << endl;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *    author:  FromDihPout
 *    created: 2020-06-19
 **/

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

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

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

Fixed submission:
/**
 *    author:  FromDihPout
 *    created: 2020-06-19
 **/

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define mod ((ull)1e9 + 7)
#define MAX ((ull)1e9)

int main() {
  int n;
  cin >> n;
  ull a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    if (i == 0)
      continue;
    ull n = (a[i - 1] + a[i] - 1) / a[i];
    a[i] *= n;
    b[i] *= n;
    n = (b[i - 1] + b[i] - 1) / b[i];
    a[i] *= n;
    b[i] *= n;
  }
  cout << a[n - 1] + b[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define mod ((ull)1e9 + 7)
#define MAX ((ull)1e9)

int main() {
  int n;
  cin >> n;
  ull a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    if (i == 0)
      continue;
    ull m = (a[i - 1] + a[i] - 1) / a[i], n = (b[i - 1] + b[i] - 1) / b[i];
    a[i] *= max(m, n);
    b[i] *= max(m, n);
  }
  cout << a[n - 1] + b[n - 1] << endl;
  return 0;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;

int main(void) {
  IOS int n;
  cin >> n;
  ll l = 1;
  ll l2 = 1;

  REP(i, n) {
    ll x, x2;
    cin >> x >> x2;
    bool flg = true;
    ll t = x;
    ll t2 = x2;
    while (flg) {
      if (t >= l && t2 >= l2) {
        l = t;
        l2 = t2;
        break;
      } else {
        t += x;
        t2 += x2;
      }
    }
  }
  cout << l + l2 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;

int main(void) {
  IOS int n;
  cin >> n;
  ll l = 1;
  ll l2 = 1;

  REP(i, n) {
    ll x, x2;
    cin >> x >> x2;
    ll m = (l - 1) / x + 1;
    ll m2 = (l2 - 1) / x2 + 1;
    ll mm = max(m, m2);

    l = x * mm;
    l2 = x2 * mm;
  }
  cout << l + l2 << endl;

  return 0;
}
Problem p03964 (C++)

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

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

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

  long long v_t = 0;
  long long v_a = 0;

  vector<int> t(N);
  vector<int> a(N);
  rep(i, N) {
    cin >> t[i];
    cin >> a[i];
  }

  rep(i, N) {
    if (v_t <= t[i] && v_a <= a[i]) {
      v_t = t[i];
      v_a = a[i];
    } else {
      int m = std::max(v_t / t[i], v_a / a[i]);
      while (true) {
        if (v_t <= m * t[i] && v_a <= m * a[i]) {
          v_t = m * t[i];
          v_a = m * a[i];
          break;
        }

        m++;
      }
    }
  }

  cout << v_t + v_a << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

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

  long long v_t = 0;
  long long v_a = 0;

  vector<int> t(N);
  vector<int> a(N);
  rep(i, N) {
    cin >> t[i];
    cin >> a[i];
  }

  rep(i, N) {
    if (v_t <= t[i] && v_a <= a[i]) {
      v_t = t[i];
      v_a = a[i];
    } else {
      long long m = std::max(v_t / t[i], v_a / a[i]);
      while (true) {
        if (v_t <= m * t[i] && v_a <= m * a[i]) {
          v_t = m * t[i];
          v_a = m * a[i];
          break;
        }

        m++;
      }
    }
  }

  cout << v_t + v_a << endl;

  return 0;
}
Problem p03964 (C++)

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

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
signed main() {
  int n;
  cin >> n;
  vector<int> t(n);
  vector<int> a(n);
  rep(i, n) cin >> t[i] >> a[i];
  int res_t = t[0];
  int res_a = a[0];
  int buf_t = 0;
  int buf_a = 0;
  rep1(i, n - 1) {
    if (t[i] >= res_t && a[i] >= res_a) {
      res_t = t[i];
      res_a = a[i];
    } else {
      int k = 1;
      while (t[i] * k < res_t || a[i] * k < res_a) {
        k++;
      }
      res_t = t[i] * k;
      res_a = a[i] * k;
    }
  }
  cout << res_t + res_a << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
signed main() {
  int n;
  cin >> n;
  vector<int> t(n);
  vector<int> a(n);
  rep(i, n) cin >> t[i] >> a[i];
  int res_t = t[0];
  int res_a = a[0];
  int buf_t = 0;
  int buf_a = 0;
  rep1(i, n - 1) {
    if (t[i] >= res_t && a[i] >= res_a) {
      res_t = t[i];
      res_a = a[i];
    } else {
      int k = max(res_t / t[i], res_a / a[i]) - 1;
      while (t[i] * k < res_t || a[i] * k < res_a) {
        k++;
      }
      res_t = t[i] * k;
      res_a = a[i] * k;
    }
  }
  cout << res_t + res_a << endl;
  return 0;
}
Problem p03964 (C++)

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

using namespace std;

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


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

using namespace std;

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define mod 1000000007

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

  ll st = T[0];
  ll sa = A[0];
  for (int i = 1; i < N; i++) {
    ll nt = T[i];
    ll na = A[i];
    while (nt < st || na < sa) {
      nt += T[i];
      na += A[i];
    }
    st = nt;
    sa = na;
  }

  cout << st + sa << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define mod 1000000007

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

  ll st = T[0];
  ll sa = A[0];
  for (int i = 1; i < N; i++) {
    ll k = (st + T[i] - 1) / T[i];
    ll l = (sa + A[i] - 1) / A[i];
    st = T[i] * max(k, l);
    sa = A[i] * max(k, l);
  }

  cout << st + sa << 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[i] >> A[i];
  }

  long long T1 = T[0];
  long long A1 = A[0];
  for (int i = 1; i < N; i++) {
    for (int j = 1;; j++) {
      if (T[i] * j >= T1 && A[i] * j >= A1) {
        T1 = T[i] * j;
        A1 = A[i] * j;
        break;
      } else {
        continue;
      }
    }
  }

  cout << T1 + A1 << 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[i] >> A[i];
  }

  long long T1 = T[0];
  long long A1 = A[0];
  for (int i = 1; i < N; i++) {
    long long ratio = max((T1 + T[i] - 1) / T[i], (A1 + A[i] - 1) / A[i]);
    T1 = T[i] * ratio;
    A1 = A[i] * ratio;
  }

  cout << T1 + A1 << endl;
}
Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#define rep(i, j, n) for (int i = j; i < n; i++)
using namespace std;
#define int long long
int mul[214514];
int divi[214514];
#define inf 1000000007
int bit[114514], n;
struct edge {
  int u, v, cost;
};
edge es[214514];
int zero = 0;
int dp[55][55][3000];
string alph("abcdefghijklmnopqrstuvwxyz");
signed main() {
  int n;
  cin >> n;
  pair<int, int> p[1005];
  rep(i, 0, n) { cin >> p[i].first >> p[i].second; }
  pair<int, int> count;
  count.first = 0, count.second = 0;
  rep(i, 1, n) {
    int h = p[i].first;
    int g = p[i].second;
    while (p[i].first < p[i - 1].first || p[i].second < p[i - 1].second) {
      p[i].first += h;
      p[i].second += g;
    }
  }
  int ans = p[n - 1].first + p[n - 1].second;
  cout << ans << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#define rep(i, j, n) for (int i = j; i < n; i++)
using namespace std;
#define int long long
int mul[214514];
int divi[214514];
#define inf 1000000007
int bit[114514], n;
struct edge {
  int u, v, cost;
};
edge es[214514];
int zero = 0;
int dp[55][55][3000];
string alph("abcdefghijklmnopqrstuvwxyz");
signed main() {
  int n;
  cin >> n;
  pair<int, int> p[1005];
  rep(i, 0, n) { cin >> p[i].first >> p[i].second; }
  pair<int, int> count;
  count.first = 0, count.second = 0;
  rep(i, 1, n) {
    int h = p[i - 1].first / p[i].first;
    int g = p[i - 1].second / p[i].second;
    if (p[i - 1].first % p[i].first != 0)
      h++;
    if (p[i - 1].second % p[i].second != 0)
      g++;
    int maxi = max(h, g);
    p[i].first *= maxi;
    p[i].second *= maxi;
  }
  int ans = p[n - 1].first + p[n - 1].second;
  cout << ans << endl;
  return 0;
}
Problem p03964 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
votes = [list(map(int, input().split())) for _ in range(n)]

curr_a, curr_b = 0, 0

for v1, v2 in votes:
    a, b = v1, v2
    i = min(curr_a // v1, curr_b // v2)

    while a < curr_a or b < curr_b:
        a = v1 * i
        b = v2 * i
        i += 1

    curr_a, curr_b = a, b

print(curr_a + curr_b)


Fixed submission:
n = int(input())
votes = [list(map(int, input().split())) for _ in range(n)]

curr_a, curr_b = 0, 0

for v1, v2 in votes:
    a, b = v1, v2
    i = max(curr_a // v1, curr_b // v2)

    while a < curr_a or b < curr_b:
        a = v1 * i
        b = v2 * i
        i += 1

    curr_a, curr_b = a, b

print(curr_a + curr_b)
Problem p03965 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1LL << 60;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<vector<ll>> dp(n, vector<ll>(2, -INF));
  if (s[0] == 'g')
    dp[0][0] = 0;
  else
    dp[0][0] = -1;
  for (int i = 1; i < n; i++) {
    if (i == 1) {
      if (s[i] == 'g') {
        dp[0][1] = dp[0][0];
        dp[1][1] = dp[0][0] + 1LL;
      } else {
        dp[0][1] = dp[0][0] - 1LL;
        dp[1][1] = dp[0][0];
      }
    } else {
      if (s[i] == 'g') {
        dp[0][i] = max(dp[0][i - 1], dp[1][i - 1]);
        dp[1][i] = dp[0][i - 1] + 1LL;
      } else {
        dp[0][i] = max(dp[0][i - 1] - 1LL, dp[1][i - 1] - 1LL);
        dp[1][i] = dp[0][i - 1];
      }
    }
  }
  // rep(i,n){
  //   cout << dp[0][i] << " ";
  // }
  // printf("\n");
  // rep(i,n){
  //   cout << dp[1][i] << " ";
  // }
  // printf("\n");
  cout << max(dp[0][n - 1], dp[1][n - 1]) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1LL << 60;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<vector<ll>> dp(2, vector<ll>(n, -INF));
  if (s[0] == 'g')
    dp[0][0] = 0;
  else
    dp[0][0] = -1;
  for (int i = 1; i < n; i++) {
    if (i == 1) {
      if (s[i] == 'g') {
        dp[0][1] = dp[0][0];
        dp[1][1] = dp[0][0] + 1LL;
      } else {
        dp[0][1] = dp[0][0] - 1LL;
        dp[1][1] = dp[0][0];
      }
    } else {
      if (s[i] == 'g') {
        dp[0][i] = max(dp[0][i - 1], dp[1][i - 1]);
        dp[1][i] = dp[0][i - 1] + 1LL;
      } else {
        dp[0][i] = max(dp[0][i - 1] - 1LL, dp[1][i - 1] - 1LL);
        dp[1][i] = dp[0][i - 1];
      }
    }
  }
  // rep(i,n){
  //   cout << dp[0][i] << " ";
  // }
  // printf("\n");
  // rep(i,n){
  //   cout << dp[1][i] << " ";
  // }
  // printf("\n");
  cout << max(dp[0][n - 1], dp[1][n - 1]) << endl;

  return 0;
}
Problem p03965 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
n = len(s)
dp = [0]

for i, c in enumerate(s):
    new = [0] * (i + 2)
    km = c == "g"
    for j in range(i % 2, i + 1, 2):
        new[j + 1] = dp[j] - (not km)
        if j:
            new[j - 1] = max(new[j - 1], dp[j] + km)
    dp = new

print(max(dp))


Fixed submission:
s = input()
print(len(s) // 2 - s.count("p"))
Problem p03965 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
  int n;
  char a[10001];
  int s;
  int x;
  scanf("%s", a);
  n = strlen(a);
  s = n / 2;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'p')
      x++;
  }
  printf("%d\n", s - x);
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
  int n;
  char a[100001];
  int s;
  int x;
  scanf("%s", a);
  n = strlen(a);
  s = n / 2;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'p')
      x++;
  }
  printf("%d\n", s - x);
  return 0;
}
Problem p03965 (C++)

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

using namespace std;

char a[10010];

int main() {
  int b, i, l = 0, sum = 0, m = 0;
  scanf("%s", a);
  b = strlen(a);
  for (i = 0; i < b; i++) {
    if (a[i] == 'g' && l < m) {
      sum++;
      l++;
    } else if (a[i] == 'g' && l >= m) {
      m++;
    } else if (a[i] == 'p' && l >= m) {
      sum--;
      m++;
    } else if (a[i] == 'p' && l < m) {
      l++;
    }
  }
  printf("%d", sum);
  return 0;
}

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

using namespace std;

char a[1000010];

int main() {
  int b, i, l = 0, sum = 0, m = 0;
  scanf("%s", a);
  b = strlen(a);
  for (i = 0; i < b; i++) {
    if (a[i] == 'g' && l < m) {
      sum++;
      l++;
    } else if (a[i] == 'g' && l >= m) {
      m++;
    } else if (a[i] == 'p' && l >= m) {
      sum--;
      m++;
    } else if (a[i] == 'p' && l < m) {
      l++;
    }
  }
  printf("%d", sum);
  return 0;
}
Problem p03965 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s;
  cin >> s;
  ll N = s.size();
  vvll dp(N + 1, vll(N + 1, -INF));
  dp.at(1).at(0) = 0LL;
  for (int i = 2; i <= N; i++) {
    for (int j = 0; j <= i / 2; j++) {
      if (s.at(i - 1) == 'g') {
        if (j <= (i - 1) / 2)
          chmax(dp.at(i).at(j), dp.at(i - 1).at(j));
        if (j - 1 >= 0 && (j - 1) <= (i - 1) / 2)
          chmax(dp.at(i).at(j), dp.at(i - 1).at(j - 1) + 1LL);
      } else {
        if (j <= (i - 1) / 2)
          chmax(dp.at(i).at(j), dp.at(i - 1).at(j) - 1LL);
        if (j - 1 >= 0 && (j - 1) <= (i - 1) / 2)
          chmax(dp.at(i).at(j), dp.at(i - 1).at(j - 1));
      }
    }
  }
  ll ans = -INF;
  for (int i = 0; i <= N / 2; i++) {
    chmax(ans, dp.at(N).at(i));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s;
  cin >> s;
  ll N = s.size();
  ll ans = 0LL;
  for (int i = 0; i < N; i++) {
    if (i % 2 == 0 && s.at(i) == 'p')
      ans--;
    if (i % 2 != 0 && s.at(i) == 'g')
      ans++;
  }
  cout << ans << endl;
}
Problem p03965 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main(void) {
  int i = 0, count = 0, p;
  char str[10001] = {0};
  scanf("%s", str);
  while (str[i] == 'p' || str[i] == 'g') {
    i++;
    if (str[i] == 'p') {
      count++;
    }
  }
  p = i / 2;
  printf("%d\n", p - count);
  return 0;
}

Fixed submission:
#include <stdio.h>

int main(void) {
  int i = 0, count = 0, p;
  char str[100001] = {0};
  scanf("%s", str);
  while (str[i] == 'p' || str[i] == 'g') {
    i++;
    if (str[i] == 'p') {
      count++;
    }
  }
  p = i / 2;
  printf("%d\n", p - count);
  return 0;
}
Problem p03966 (C++)

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

double g(int a, int b, int c, int d) {
  return (a - c) * (a - c) + (b - d) * (b - d);
}

double f(int a, int b, int c, int d) { return sqrt(g(a, b, c, d)); }
void ok() { puts("YES"); }
void ng() {
  puts("NO");
  exit(0);
}

int main() {
  int n;
  cin >> n;
  long long a = 1, b = 1;
  for (int i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    long long d = min(a / x, b / y);
    for (; x * d < a || y * d < b; d++)
      ;
    a = x * d;
    b = y * d;
  }
  cout << a + b << '\n';
  return 0;
}


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

double g(int a, int b, int c, int d) {
  return (a - c) * (a - c) + (b - d) * (b - d);
}

double f(int a, int b, int c, int d) { return sqrt(g(a, b, c, d)); }
void ok() { puts("YES"); }
void ng() {
  puts("NO");
  exit(0);
}

int main() {
  int n;
  cin >> n;
  long long a = 1, b = 1;
  for (int i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    long long d = max(a / x, b / y);
    for (; x * d < a || y * d < b; d++)
      ;
    a = x * d;
    b = y * d;
  }
  cout << a + b << '\n';
  return 0;
}
Problem p03966 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<ll> T(N), A(N), t_num(N), a_num(N);
  for (int i = 0; i < N; ++i) {
    ll t, a;
    cin >> t >> a;
    T[i] = t;
    A[i] = a;
    if (i == 0) {
      t_num[i] = t;
      a_num[i] = a;
    }
  }

  for (int i = 1; i < N; ++i) {
    ll last_t = t_num[i - 1];
    ll last_a = a_num[i - 1];
    ll t_rate = T[i];
    ll a_rate = A[i];
    ll start = (last_t + (t_rate - 1)) / t_rate;
    while (true) {
      ll mul = start * a_rate;
      if (last_a <= mul) {
        break;
      } else {
        ++start;
      }
    }
    t_num[i] = t_rate * start;
    a_num[i] = a_rate * start;
  }
  cout << t_num[N - 1] + a_num[N - 1] << endl;
}


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

int main() {
  int N;
  cin >> N;
  vector<ll> T(N), A(N), t_num(N), a_num(N);
  for (int i = 0; i < N; ++i) {
    ll t, a;
    cin >> t >> a;
    T[i] = t;
    A[i] = a;
    if (i == 0) {
      t_num[i] = t;
      a_num[i] = a;
    }
  }

  for (int i = 1; i < N; ++i) {
    ll last_t = t_num[i - 1];
    ll last_a = a_num[i - 1];
    ll t_rate = T[i];
    ll a_rate = A[i];
    ll div_t = (last_t + (t_rate - 1)) / t_rate;
    ll div_a = (last_a + (a_rate - 1)) / a_rate;
    ll div = max(div_t, div_a);
    t_num[i] = t_rate * div;
    a_num[i] = a_rate * div;
  }
  cout << t_num[N - 1] + a_num[N - 1] << endl;
}
Problem p03966 (C++)

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

int main() {
  int N;
  long long int A, B, t, a;
  cin >> N >> A >> B;
  for (int i = 1; i < N; i++) {
    cin >> t >> a;
    for (int j = 1;; j++) {
      if (t * j >= A && a * j >= B) {
        A = t * j;
        B = a * j;
        break;
      }
    }
  }
  cout << A + B << endl;
}

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

int main() {
  int N;
  long long int A, B, t, a;
  cin >> N >> A >> B;
  for (int i = 1; i < N; i++) {
    cin >> t >> a;
    for (long long int j = max(A / t, B / a);; j++) {
      if (t * j >= A && a * j >= B) {
        A = t * j;
        B = a * j;
        break;
      }
    }
  }
  cout << A + B << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  vector<int> T(N), A(N);
  long long t, a;
  REP(i, N) { scanf("%d%d", &T[i], &A[i]); }
  t = 0;
  a = 0;
  int k;
  for (int i = 0; i < N; i++) {
    k = 1;
    while (1) {
      if (T[i] * k >= t && A[i] * k >= a) {
        break;
      } else {
        k++;
      }
    }
    t = T[i] * k;
    a = A[i] * k;
    // printf("i = %dのあとt = %lld, a = %lld\n", i, t, a);
  }
  printf("%lld\n", t + a);
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  vector<int> T(N), A(N);
  long long t, a;
  REP(i, N) { scanf("%d%d", &T[i], &A[i]); }
  t = T[0];
  a = A[0];
  long long int k, l;
  for (int i = 1; i < N; i++) {
    k = t / T[i];
    l = a / A[i];
    if (t % T[i] != 0)
      k++;
    if (a % A[i] != 0)
      l++;
    k = max(k, l);
    t = T[i] * k;
    a = A[i] * k;
    // printf("i = %dのあとt = %lld, a = %lld\n", i, t, a);
  }
  printf("%lld\n", t + a);
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(c) (c).begin(), (c).end()
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
using namespace std;

int main() {
  long long n;
  cin >> n;

  long long cT, cA;
  cin >> cT >> cA;

  for (long long i = 1; i < n; i++) {
    long long t, a;
    cin >> t >> a;

    long long loop;

    if (cT > cA)
      loop = cT / t;
    else
      loop = cA / a;
    while (1) {
      long long tmpT = loop * t, tmpA = loop * a;
      if (cT <= tmpT && cA <= tmpA) {
        cT = tmpT;
        cA = tmpA;
        break;
      }
      loop++;
    }
  }
  cout << cT + cA << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(c) (c).begin(), (c).end()
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
using namespace std;

int main() {
  long long n;
  cin >> n;

  long long cT, cA;
  cin >> cT >> cA;

  for (long long i = 1; i < n; i++) {
    long long t, a;
    cin >> t >> a;

    long long loop;

    if (cT / t > cA / a)
      loop = cT / t;
    else
      loop = cA / a;
    while (1) {
      long long tmpT = loop * t, tmpA = loop * a;
      if (cT <= tmpT && cA <= tmpA) {
        cT = tmpT;
        cA = tmpA;
        break;
      }
      loop++;
    }
  }
  cout << cT + cA << endl;
}
Problem p03966 (C++)

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

using namespace std;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (long long i = (a), i##Len_ = (b); i < i##Len_; i++)
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;

// abc062_c
// TとAの数比
int main() {
  std::ifstream in("resource/input.txt");
  std::cin.rdbuf(in.rdbuf());

  ll N;
  cin >> N;

  ll t[N];
  ll a[N];

  ll res;

  REP(i, N) { cin >> t[i] >> a[i]; }

  // 票数を1とする
  ll T = 1;
  ll A = 1;

  REP(i, N) {
    // bai,T : i-1番目までに投票した票数
    ll bai = max((T + t[i] - 1) / t[i], (A + a[i] - 1) / a[i]);

    // T : i番目までに投票した票数
    T = bai * t[i];
    A = bai * a[i];
  }

  cout << T + A << endl;

  return 0;
}


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

using namespace std;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (long long i = (a), i##Len_ = (b); i < i##Len_; i++)
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;

// abc062_c
// TとAの数比
int main() {
  ll N;
  cin >> N;

  ll t[N];
  ll a[N];

  ll res;

  REP(i, N) { cin >> t[i] >> a[i]; }

  // 票数を1とする
  ll T = 1;
  ll A = 1;

  REP(i, N) {
    // bai,T : i-1番目までに投票した票数
    ll bai = max((T + t[i] - 1) / t[i], (A + a[i] - 1) / a[i]);

    // T : i番目までに投票した票数
    T = bai * t[i];
    A = bai * a[i];
  }

  cout << T + A << endl;

  return 0;
}
Problem p03966 (C++)

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

int main() {
  int n;
  cin >> n;
  long t, a;
  cin >> t >> a;
  while (--n) {
    long x, y;
    cin >> x >> y;
    if (t <= x && a <= y)
      t = x, a = y;
    else {
      long d = 1;
      for (; t > x * d || a > y * d; ++d)
        ;
      t = x * d, a = y * d;
    }
  }
  cout << a + t << endl;
}


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

int main() {
  int n;
  cin >> n;
  long t, a;
  cin >> t >> a;
  while (--n) {
    long x, y;
    cin >> x >> y;
    if (t <= x && a <= y)
      t = x, a = y;
    else {
      long d = max((t + x - 1) / x, (a + y - 1) / y);
      t = x * d, a = y * d;
    }
  }
  cout << a + t << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 29)
using namespace std;

int main() {
  int n;
  vector<pair<long long, long long>> v;
  cin >> n;
  rep(i, n) {
    long long a, b;
    cin >> a >> b;
    v.emplace_back(make_pair(a, b));
  }
  range(i, 1, n) {
    long long f = v[i].first;
    long long s = v[i].second;
    while (v[i].first < v[i - 1].first) {
      v[i].first += f;
      v[i].second += s;
    }
    while (v[i].second < v[i - 1].second) {
      v[i].first += f;
      v[i].second += s;
    }
  }
  cout << v[n - 1].first + v[n - 1].second << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 29)
using namespace std;

int main() {
  int n;
  vector<pair<long long, long long>> v;
  cin >> n;
  rep(i, n) {
    long long a, b;
    cin >> a >> b;
    v.emplace_back(make_pair(a, b));
  }
  range(i, 1, n) {
    long long f = v[i].first;
    long long s = v[i].second;
    long long kf, ks;
    if (v[i].first < v[i - 1].first || v[i].second < v[i - 1].second) {
      kf = abs(v[i - 1].first - v[i].first) / f;
      ks = abs(v[i - 1].second - v[i].second) / s;
      long long k = max(kf, ks);
      v[i].first = v[i].first + f * k;
      v[i].second = v[i].second + s * k;
      if (v[i].first < v[i - 1].first) {
        v[i].first += f;
        v[i].second += s;
      }
      if (v[i].second < v[i - 1].second) {
        v[i].first += f;
        v[i].second += s;
      }
    }
  }
  cout << v[n - 1].first + v[n - 1].second << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
const int Nmax = 10000;
int N, T[Nmax], A[Nmax];

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

  long t = T[0], a = A[0];
  for (int i = 1; i < N; i++) {
    int k = 1;
    while (t > k * T[i] || a > k * A[i])
      k++;
    t = k * T[i];
    a = k * A[i];
  }

  cout << t + a << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
const int Nmax = 10000;
int N, T[Nmax], A[Nmax];

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

  long t = T[0], a = A[0];
  for (int i = 1; i < N; i++) {
    long kt = t % T[i] > 0 ? t / T[i] + 1 : t / T[i];
    long ka = a % A[i] > 0 ? a / A[i] + 1 : a / A[i];
    long k = max(kt, ka);
    t = k * T[i];
    a = k * A[i];
  }

  cout << t + a << endl;
  return 0;
}
Problem p03966 (C++)

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

int main() {
  int n;
  cin >> n;
  long long t, a;
  long long pt, pa;
  cin >> pt >> pa;
  for (int i = 1; i < n; i++) {
    cin >> t >> a;
    if (pt <= t && pa <= a) {
      pt = t;
      pa = a;
    } else {
      long long x = 2;
      while (pt > t || pa > a) {
        if (pt <= t * x && pa <= a * x) {
          pt = t * x;
          pa = a * x;
          break;
        }
        x++;
      }
    }
  }

  cout << pt + pa << endl;
}


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

int main() {
  int n;
  cin >> n;
  long long t, a;
  long long pt, pa;
  cin >> pt >> pa;
  for (int i = 1; i < n; i++) {
    cin >> t >> a;
    if (pt <= t && pa <= a) {
      pt = t;
      pa = a;
    } else {
      long long x = max((pt + t - 1) / t, (pa + a - 1) / a);
      pt = t * x;
      pa = a * x;
    }
  }

  cout << pt + pa << endl;
}
Problem p03966 (C++)

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

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define RFOR(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define LL long long
#define INF INT_MAX

const double EPS = 1e-14;
const double PI = acos(-1.0);

LL get(LL x, int t) {
  if (x % t == 0)
    return x;
  else {
    return ((x / t) + 1) * t;
  }
}

void calc(LL &low, LL &high, int a, int b) {
  if (low * b / a >= high)
    high = low * b / a;
  else {
    high = get(high, b);
    calc(high, low, b, a);
  }
  return;
}

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

  int t[n];
  int a[n];
  REP(i, n) { cin >> t[i] >> a[i]; }

  LL now_t = 1;
  LL now_a = 1;
  REP(i, n) {
    if (t[i] == a[i]) {
      now_t = max(now_t, now_a);
      now_a = max(now_t, now_a);
    } else if (t[i] < a[i]) {
      now_t = get(now_t, t[i]);
      calc(now_t, now_a, t[i], a[i]);
    } else {
      now_a = get(now_a, a[i]);
      calc(now_a, now_t, a[i], t[i]);
    }
  }

  cout << now_t + now_a << endl;
}


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

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define RFOR(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define LL long long
#define INF INT_MAX

const double EPS = 1e-14;
const double PI = acos(-1.0);

LL get(LL x, int t) {
  if (x % t == 0)
    return x;
  else {
    return ((x / t) + 1) * t;
  }
}

void calc(LL &low, LL &high, int a, int b) {
  if (low / a * b >= high)
    high = low / a * b;
  else {
    high = get(high, b);
    calc(high, low, b, a);
  }
  return;
}

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

  int t[n];
  int a[n];
  REP(i, n) { cin >> t[i] >> a[i]; }

  LL now_t = 1;
  LL now_a = 1;
  REP(i, n) {
    if (t[i] == a[i]) {
      now_t = max(now_t, now_a);
      now_a = max(now_t, now_a);
    } else if (t[i] < a[i]) {
      now_t = get(now_t, t[i]);
      calc(now_t, now_a, t[i], a[i]);
    } else {
      now_a = get(now_a, a[i]);
      calc(now_a, now_t, a[i], t[i]);
    }
  }

  cout << now_t + now_a << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\n";
}
ld PI = 3.14159265358979323846;

int main() {
  ll N;
  cin >> N;
  vector<ll> T(N), A(N);
  rep(i, N) { cin >> T[i] >> A[i]; }
  ll ct = 0, ca = 0;
  rep(i, N) {
    ll c = __gcd(T[i], A[i]);
    ll mt = T[i] / c;
    ll ma = A[i] / c;
    ll nt = mt;
    ll na = ma;
    while (ct > nt || ca > na) {
      nt += mt;
      na += ma;
    }
    ct = nt;
    ca = na;
  }
  cout << ct + ca << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\n";
}
ld PI = 3.14159265358979323846;

int main() {
  ll N;
  cin >> N;
  vector<ll> T(N), A(N);
  rep(i, N) { cin >> T[i] >> A[i]; }
  ll ct = 0, ca = 0;
  rep(i, N) {
    ll c = __gcd(T[i], A[i]);
    ll mt = T[i] / c;
    ll ma = A[i] / c;
    ll nt = mt;
    ll na = ma;
    if (ct > nt || ca > na) {
      ll q = max(((ct - nt + mt - 1) / mt), ((ca - na + ma - 1) / ma));
      nt = q * mt + mt;
      na = q * ma + ma;
    }
    ct = nt;
    ca = na;
  }
  cout << ct + ca << endl;
}