Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (long long int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (long long int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (long long int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
#define Abs(a, b) max(a, b) - min(a, b)
#define YES(condition)                                                         \
  if (condition) {                                                             \
    cout << "YES" << endl;                                                     \
  } else {                                                                     \
    cout << "NO" << endl;                                                      \
  }
#define Yes(condition)                                                         \
  if (condition) {                                                             \
    cout << "Yes" << endl;                                                     \
  } else {                                                                     \
    cout << "No" << endl;                                                      \
  }
#define Cout(x) cout << (x) << endl
#define POSSIBLE(condition)                                                    \
  if (condition) {                                                             \
    cout << "POSSIBLE" << endl;                                                \
  } else {                                                                     \
    cout << "IMPOSSIBLE" << endl;                                              \
  }
#define Possible(condition)                                                    \
  if (condition) {                                                             \
    cout << "Possible" << endl;                                                \
  } else {                                                                     \
    cout << "Impossible" << endl;                                              \
  }
#define possible(condition)                                                    \
  if (condition) {                                                             \
    cout << "possible" << endl;                                                \
  } else {                                                                     \
    cout << "impossible" << endl;                                              \
  }
#define Size(n) (n).size()

typedef long long ll;

using namespace std;

const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;

long long int n, cnt = 0, ans = 0, a[ohara], b[ohara], c, d, cmp, cmpp, m, h, w,
                 x, y, sum = 0, pos;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
bool fl = true;
struct edge {
  int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

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

  cin >> n;
  rep(i, n) cin >> a[i] >> b[i];
  ll nowa = a[0], nowb = b[0];
  rrep(i, 1, n) {
    cnt = 0;
    while (1) {
      if (nowa % a[i] == 0)
        break;
      nowa++;
    }
    while (1) {
      if (nowa / a[i] * b[i] >= nowb) {
        nowb = nowa / a[i] * b[i];
        break;
      }
      nowa += a[i];
    }
  }
  Cout(nowa + nowb);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (long long int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (long long int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (long long int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
#define Abs(a, b) max(a, b) - min(a, b)
#define YES(condition)                                                         \
  if (condition) {                                                             \
    cout << "YES" << endl;                                                     \
  } else {                                                                     \
    cout << "NO" << endl;                                                      \
  }
#define Yes(condition)                                                         \
  if (condition) {                                                             \
    cout << "Yes" << endl;                                                     \
  } else {                                                                     \
    cout << "No" << endl;                                                      \
  }
#define Cout(x) cout << (x) << endl
#define POSSIBLE(condition)                                                    \
  if (condition) {                                                             \
    cout << "POSSIBLE" << endl;                                                \
  } else {                                                                     \
    cout << "IMPOSSIBLE" << endl;                                              \
  }
#define Possible(condition)                                                    \
  if (condition) {                                                             \
    cout << "Possible" << endl;                                                \
  } else {                                                                     \
    cout << "Impossible" << endl;                                              \
  }
#define possible(condition)                                                    \
  if (condition) {                                                             \
    cout << "possible" << endl;                                                \
  } else {                                                                     \
    cout << "impossible" << endl;                                              \
  }
#define Size(n) (n).size()

typedef long long ll;

using namespace std;

const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;

long long int n, cnt = 0, ans = 0, a[ohara], b[ohara], c, d, cmp, cmpp, m, h, w,
                 x, y, sum = 0, pos;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
bool fl = true;
struct edge {
  int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

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

  cin >> n;
  rep(i, n) cin >> a[i] >> b[i];
  ll nowa = a[0], nowb = b[0];
  rrep(i, 1, n) {
    cmp = nowa / a[i];
    if (nowa % a[i] != 0)
      cmp++;
    cmpp = nowb / b[i];
    if (nowb % b[i] != 0)
      cmpp++;
    if (cmp > cmpp) {
      nowa = a[i] * cmp;
      nowb = b[i] * cmp;
    } else {
      nowb = b[i] * cmpp;
      nowa = a[i] * cmpp;
    }
  }
  Cout(nowa + nowb);

  return 0;
}