Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// const ll INF = 1LL <<60;
const int INF = 10000;

// 最小公倍数
ll gcd(ll x, ll y) {
  ll tmp = 0;
  if (x < y) {
    tmp = x;
    x = y;
    y = tmp;
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}

// 最大公倍数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

// 階乗
ll kaijo(ll k) {
  ll sum = 1;
  for (ll i = 1; i <= k; ++i) {
    sum *= i;
    sum %= 1000000000 + 7;
  }
  return sum;
}

// for(int i = ; i < ; i++){}

ll lmax(ll s, ll t) {
  if (s > t) {
    return s;
  } else {
    return t;
  }
}

ll lmin(ll s, ll t) {
  if (s < t) {
    return s;
  } else {
    return t;
  }
}
// ここから開始

int main() {
  int n;
  cin >> n;
  int t[n], a[n];
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  ll tt = t[0];
  ll aa = a[0];
  for (int i = 1; i < n; i++) {
    if (tt <= t[i] && aa <= a[i]) {
      tt = t[i];
      aa = a[i];
      // cout<<"IF";
    } else {
      ll mod = lmax(tt / t[i], aa / a[i]);
      ll tmptt = t[i], tmpaa = a[i];
      while (1) {
        tmptt += t[i];
        tmpaa += a[i];
        // cout<<tmptt<<"*"<<tmpaa;
        if (tmptt >= tt && tmpaa >= aa)
          break;
      }
      tt = tmptt;
      aa = tmpaa;
      // cout<<"ELSE";
    }
    // cout<<tt<<" "<<aa<<endl;
  }
  cout << tt + aa << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// const ll INF = 1LL <<60;
const int INF = 10000;

// 最小公倍数
ll gcd(ll x, ll y) {
  ll tmp = 0;
  if (x < y) {
    tmp = x;
    x = y;
    y = tmp;
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}

// 最大公倍数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

// 階乗
ll kaijo(ll k) {
  ll sum = 1;
  for (ll i = 1; i <= k; ++i) {
    sum *= i;
    sum %= 1000000000 + 7;
  }
  return sum;
}

// for(int i = ; i < ; i++){}

ll lmax(ll s, ll t) {
  if (s > t) {
    return s;
  } else {
    return t;
  }
}

ll lmin(ll s, ll t) {
  if (s < t) {
    return s;
  } else {
    return t;
  }
}
// ここから開始

int main() {
  int n;
  cin >> n;
  int t[n], a[n];
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  ll tt = t[0];
  ll aa = a[0];
  for (int i = 1; i < n; i++) {
    if (tt <= t[i] && aa <= a[i]) {
      tt = t[i];
      aa = a[i];
      // cout<<"IF";
    } else {
      ll mod = lmax((tt - 1) / t[i] + 1, (aa - 1) / a[i] + 1);
      tt = t[i] * mod;
      aa = a[i] * mod;
      // cout<<"ELSE";
    }
    // cout<<tt<<" "<<aa<<endl;
  }
  cout << tt + aa << endl;
  return 0;
}