Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define ROF(i, j, k) for (int i = (int)(j); i >= (int)(k); --i)
#define FORLL(i, n, m) for (long long i = n; i < (long long)(m); i++)
#define SORT(v, n) sort(v, v + n)
#define REVERSE(v) reverse((v).begin(), (v).end())

using namespace std;
using ll = long long;
const ll MOD = 1000000007LL;
typedef pair<int, int> P;

ll ADD(ll x, ll y) { return (x + y) % MOD; }
ll SUB(ll x, ll y) { return (x - y + MOD) % MOD; }
ll MUL(ll x, ll y) { return x * y % MOD; }
ll POW(ll x, ll e) {
  ll v = 1;
  for (; e; x = MUL(x, x), e >>= 1)
    if (e & 1)
      v = MUL(v, x);
  return v;
}
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/
  return MUL(x, POW(y, MOD - 2));
}

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
int T[1010], A[1010];

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

  cin >> n;
  REP(i, n) cin >> T[i] >> A[i];

  int t = T[0], a = A[0];

  FOR(i, 1, n) {
    if (T[i] == A[i]) {
      t = max(t, a);
      a = max(t, a);
    } else {
      int tmp_a = A[i];
      int tmp_t = T[i];
      while (A[i] < a || T[i] < t) {
        A[i] += tmp_a;
        T[i] += tmp_t;
      }
      /*
      t = t * A[i];
      a = a * T[i];
      */
      t = T[i];
      a = A[i];
    }
    // cout << t << " : " << a << endl;
  }
  cout << t + a << endl;
  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define ROF(i, j, k) for (int i = (int)(j); i >= (int)(k); --i)
#define FORLL(i, n, m) for (long long i = n; i < (long long)(m); i++)
#define SORT(v, n) sort(v, v + n)
#define REVERSE(v) reverse((v).begin(), (v).end())

using namespace std;
using ll = long long;
const ll MOD = 1000000007LL;
typedef pair<int, int> P;

ll ADD(ll x, ll y) { return (x + y) % MOD; }
ll SUB(ll x, ll y) { return (x - y + MOD) % MOD; }
ll MUL(ll x, ll y) { return x * y % MOD; }
ll POW(ll x, ll e) {
  ll v = 1;
  for (; e; x = MUL(x, x), e >>= 1)
    if (e & 1)
      v = MUL(v, x);
  return v;
}
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/
  return MUL(x, POW(y, MOD - 2));
}

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
int T[1010], A[1010];

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

  cin >> n;
  REP(i, n) cin >> T[i] >> A[i];

  ll t = 1, a = 1;
  REP(i, n) {
    ll v = max(a / A[i] + (a % A[i] ? 1 : 0), t / T[i] + (t % T[i] ? 1 : 0));
    t = v * T[i];
    a = v * A[i];
  }
  cout << t + a << endl;
  return 0;
}