Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

class Counting {
  unsigned long long p, n;
  vector<long long> invMemo, facMemo, facInvMemo;

public:
  Counting(unsigned long long nn, unsigned long long np) {
    p = np;
    n = nn;
    invMemo.resize(n + 1);
    facMemo.resize(n + 1);
    facInvMemo.resize(n + 1);
    facMemo[0] = 1;
    facMemo[1] = 1;
    facInvMemo[0] = 1;
    facInvMemo[1] = 1;
    invMemo[0] = 0;
    invMemo[1] = 1;
    for (unsigned long long i = 2; i <= n; i++) {
      invMemo[i] = p - (p / i) * invMemo[p % i] % p;
      facMemo[i] = (facMemo[i - 1] * i) % p;
      facInvMemo[i] = (facInvMemo[i - 1] * invMemo[i]) % p;
    }
  }
  unsigned long long fac(unsigned long long k) { return facMemo[k]; }
  unsigned long long facInv(unsigned long long k) { return facInvMemo[k]; }
  unsigned long long choose(unsigned long long n, unsigned long long k) {
    if (!(0 <= k && k <= n))
      return 0;
    return (((facMemo[n] * facInvMemo[k]) % p) * facMemo[n - k]) % p;
  }
  unsigned long long divide(unsigned long long a, unsigned long long b) {
    return (a * invMemo[b]) % p;
  }
  unsigned long long mod(unsigned long long a) {
    a %= p;
    if (a < 0)
      a += p;
    return a;
  }
  unsigned long long power(unsigned long long a, unsigned long long e) {
    unsigned long long ans = 1;
    for (; e > 0; e /= 2) {
      if (e % 2 != 0)
        ans = ans * a % p;
      a = a * a % p;
    }
    return ans;
  }
};

int n;
long long T = 0, A = 0;
vector<long long> t, a;

int main() {
  cin >> n;
  t.resize(n);
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  T = t[0];
  A = a[0];
  for (int i = 1; i < n; i++) {
    for (int j = 1;; j++) {
      if (T <= t[i] * j && A <= a[i] * j) {
        T = t[i] * j;
        A = a[i] * j;
        break;
      }
    }
  }
  cout << T + A << endl;
  //	cin >> n;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

class Counting {
  unsigned long long p, n;
  vector<long long> invMemo, facMemo, facInvMemo;

public:
  Counting(unsigned long long nn, unsigned long long np) {
    p = np;
    n = nn;
    invMemo.resize(n + 1);
    facMemo.resize(n + 1);
    facInvMemo.resize(n + 1);
    facMemo[0] = 1;
    facMemo[1] = 1;
    facInvMemo[0] = 1;
    facInvMemo[1] = 1;
    invMemo[0] = 0;
    invMemo[1] = 1;
    for (unsigned long long i = 2; i <= n; i++) {
      invMemo[i] = p - (p / i) * invMemo[p % i] % p;
      facMemo[i] = (facMemo[i - 1] * i) % p;
      facInvMemo[i] = (facInvMemo[i - 1] * invMemo[i]) % p;
    }
  }
  unsigned long long fac(unsigned long long k) { return facMemo[k]; }
  unsigned long long facInv(unsigned long long k) { return facInvMemo[k]; }
  unsigned long long choose(unsigned long long n, unsigned long long k) {
    if (!(0 <= k && k <= n))
      return 0;
    return (((facMemo[n] * facInvMemo[k]) % p) * facMemo[n - k]) % p;
  }
  unsigned long long divide(unsigned long long a, unsigned long long b) {
    return (a * invMemo[b]) % p;
  }
  unsigned long long mod(unsigned long long a) {
    a %= p;
    if (a < 0)
      a += p;
    return a;
  }
  unsigned long long power(unsigned long long a, unsigned long long e) {
    unsigned long long ans = 1;
    for (; e > 0; e /= 2) {
      if (e % 2 != 0)
        ans = ans * a % p;
      a = a * a % p;
    }
    return ans;
  }
};

int n;
long long T = 0, A = 0;
vector<long long> t, a;

int main() {
  cin >> n;
  t.resize(n);
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  T = t[0];
  A = a[0];
  for (int i = 1; i < n; i++) {
    if (T <= t[i] && A <= a[i]) {
      T = t[i];
      A = a[i];
      continue;
    } else {
      long long var = max((T - 1) / t[i] + 1, (A - 1) / a[i] + 1);
      T = t[i] * var;
      A = a[i] * var;
    }
  }
  cout << T + A << endl;
  //	cin >> n;
  return 0;
}