Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define INF (int)2e9
#define MOD (1000 * 1000 * 1000 + 7)
#define Ceil(x, n) (((((x)) + ((n)-1)) / n)) /* Nの倍数に切り上げ割り算 */
#define CeilN(x, n) (((((x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ */
#define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */
#define IsOdd(x) (((x)&0x01UL) == 0x01UL)
#define IsEven(x) (!IsOdd((x)))
#define ArrayLength(x) (sizeof(x) / sizeof(x[0]))
#define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF)
#define M_PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long int QWORD;
typedef pair<ll, ll> P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
int main() {
  ll N;
  cin >> N;
  vector<ll> T(N), A(N);
  REP(i, N) { cin >> T[i] >> A[i]; }

  ll tPrev = T[0];
  ll aPrev = A[0];
  REPN(i, 1, N) {
    ll rate = 1;
    while (1) {
      ll tNext = T[i] * rate;
      ll aNext = A[i] * rate;
      if ((tPrev <= tNext) && (aPrev <= aNext)) {
        tPrev = tNext;
        aPrev = aNext;
        break;
      }
      rate++;
    }
  }

  cout << tPrev + aPrev << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define INF (int)2e9
#define MOD (1000 * 1000 * 1000 + 7)
#define Ceil(x, n) (((((x)) + ((n)-1)) / n)) /* Nの倍数に切り上げ割り算 */
#define CeilN(x, n) (((((x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ */
#define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */
#define IsOdd(x) (((x)&0x01UL) == 0x01UL)
#define IsEven(x) (!IsOdd((x)))
#define ArrayLength(x) (sizeof(x) / sizeof(x[0]))
#define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF)
#define M_PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long int QWORD;
typedef pair<ll, ll> P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
int main() {
  ll N;
  cin >> N;
  vector<ll> T(N), A(N);
  REP(i, N) { cin >> T[i] >> A[i]; }

  ll tPrev = T[0];
  ll aPrev = A[0];
  REPN(i, 1, N) {
    ll rate = 1;
    rate = max(rate, tPrev / T[i]);
    rate = max(rate, aPrev / A[i]);
    while (1) {
      ll tNext = T[i] * rate;
      ll aNext = A[i] * rate;
      if ((tPrev <= tNext) && (aPrev <= aNext)) {
        tPrev = tNext;
        aPrev = aNext;
        break;
      }
      rate++;
    }
  }

  cout << tPrev + aPrev << endl;
  return 0;
}