Problem p03966 (C++)

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

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    REP(WW, n) cerr << v[WW] << ' ';                                           \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, WW, HH)                                                      \
  {                                                                            \
    REP(W_, WW) {                                                              \
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define fastcin()                                                              \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)

typedef long long ll;
typedef pair<ll, ll> P;

int main() {

  int n;
  cin >> n;
  ll a = 0;
  ll b = 0;
  REP(i, n) {
    ll aa = LLINF;
    ll bb = LLINF;
    ll x, y;
    cin >> x >> y;
    ll tmp = max(1LL, a / x);
    while (a > tmp * x || b > tmp * y)
      tmp++;
    a = tmp * x;
    b = tmp * y;
  }

  cout << a + b << endl;

  return 0;
}


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

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    REP(WW, n) cerr << v[WW] << ' ';                                           \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, WW, HH)                                                      \
  {                                                                            \
    REP(W_, WW) {                                                              \
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define fastcin()                                                              \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)

typedef long long ll;
typedef pair<ll, ll> P;

int main() {

  int n;
  cin >> n;
  ll a = 0;
  ll b = 0;
  REP(i, n) {
    ll aa = LLINF;
    ll bb = LLINF;
    ll x, y;
    cin >> x >> y;
    ll tmp = max(1LL, max(a / x, b / y));
    while (a > tmp * x || b > tmp * y)
      tmp++;
    a = tmp * x;
    b = tmp * y;
  }

  cout << a + b << endl;

  return 0;
}