Problem p03964 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
static const int INF = 1000010000;
static const int NIL = -1;
static const int MOD = 1000000007;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort(v.begin(), v.end());
#define pb push_back
#define mp make_pair
#define np next_permutation
#define pq priority_queue

int gcd(int a, int b) {
  int c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int lcm(int a, int b) {
  int c = gcd(a, b);
  a /= c;
  return a * b;
}
int nCr(int a, int b) {
  int i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}
int pow(int a, int b) {
  int i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long llpow(long long a, long long b) {
  long long i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long llgcd(long long a, long long b) {
  long long c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
long long lllcm(long long a, long long b) {
  long long c = llgcd(a, b);
  a /= c;
  return a * b;
}
long long llnCr(long long a, long long b) {
  long long i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}

// int dx4[4] = {0,1,0,-1}, dy4[4] = {-1,0,1,0};
// int dx5[5] = {-1,0,0,0,1}, dy5[5] = {0,-1,0,1,0};
// int dx8[8] = {-1,0,1,1,1,0,-1,-1}, dy8[8] = {1,1,1,0,-1,-1,-1,0};
// int dx9[9] = {-1,0,1,1,1,0,-1,-1,0}, dy9[9] = {1,1,1,0,-1,-1,-1,0,0};

int main() {
  int n;
  cin >> n;
  vector<ll> t(n), a(n);
  REP(i, n) { cin >> t[i] >> a[i]; }
  ll nt = t[0];
  ll na = a[0];
  REP(i, n) {
    ll tmp = lcm(nt, na);
    ll tt = tmp * t[i];
    ll ta = tmp * a[i];
    ll ngcd = gcd(tt, ta);
    tt /= ngcd;
    ta /= ngcd;
    ll kt = tt;
    ll ka = ta;
    if (tt < nt || ta < na) {
      ll tmpt = ((nt + tt - kt - 1) / kt);
      ll tmpa = ((na + ta - ka - 1) / ka);
      tt += kt * max(tmpt, tmpa);
      ta += ka * max(tmpt, tmpa);
    }
    nt = tt;
    na = ta;
  }
  cout << nt + na << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
static const int INF = 1000010000;
static const int NIL = -1;
static const int MOD = 1000000007;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort(v.begin(), v.end());
#define pb push_back
#define mp make_pair
#define np next_permutation
#define pq priority_queue

int gcd(int a, int b) {
  int c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int lcm(int a, int b) {
  int c = gcd(a, b);
  a /= c;
  return a * b;
}
int nCr(int a, int b) {
  int i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}
int pow(int a, int b) {
  int i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long llpow(long long a, long long b) {
  long long i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long llgcd(long long a, long long b) {
  long long c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
long long lllcm(long long a, long long b) {
  long long c = llgcd(a, b);
  a /= c;
  return a * b;
}
long long llnCr(long long a, long long b) {
  long long i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}

// int dx4[4] = {0,1,0,-1}, dy4[4] = {-1,0,1,0};
// int dx5[5] = {-1,0,0,0,1}, dy5[5] = {0,-1,0,1,0};
// int dx8[8] = {-1,0,1,1,1,0,-1,-1}, dy8[8] = {1,1,1,0,-1,-1,-1,0};
// int dx9[9] = {-1,0,1,1,1,0,-1,-1,0}, dy9[9] = {1,1,1,0,-1,-1,-1,0,0};

int main() {
  int n;
  cin >> n;
  vector<ll> t(n), a(n);
  REP(i, n) { cin >> t[i] >> a[i]; }
  ll nt = t[0];
  ll na = a[0];
  FOR(i, 1, n) {
    ll tmp = max((nt + t[i] - 1) / t[i], (na + a[i] - 1) / a[i]);
    nt = tmp * t[i];
    na = tmp * a[i];
  }
  cout << nt + na << endl;
}