Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// debug
#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl;
#define ddd(x, y, z)                                                           \
  cerr << "(" #x "," #y "," #z "):(" << x << "," << y << "," << z << ")"       \
       << endl;
#define dump(v)                                                                \
  cerr << #v ":[ ";                                                            \
  for (auto macro_vi : v) {                                                    \
    cerr << macro_vi << " ";                                                   \
  }                                                                            \
  cerr << "]" << endl;
#define ddump(v)                                                               \
  cerr << #v ":" << endl;                                                      \
  for (auto macro_row : v) {                                                   \
    cerr << "[";                                                               \
    for (auto macro__vi : macro_row) {                                         \
      cerr << macro__vi << " ";                                                \
    }                                                                          \
    cerr << "]" << endl;                                                       \
  }
#define damp(m)                                                                \
  for (auto macro_pair : m) {                                                  \
    cerr << macro_pair.first << ":" << macro_pair.second << endl;              \
  }
#define dpair(p)                                                               \
  cerr << #p ":"                                                               \
       << "(" << p.first << "," << p.second << ")" << endl;
// iterate
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) reprrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(v) v.begin(), v.end()
// etc
#define chmin(mi, val) mi = min(mi, val);
#define chmax(ma, val) ma = max(ma, val);
using ll = long long;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  ll T = 0ll, A = 0ll;
  rep(i, n) {
    ll ti, ai;
    cin >> ti >> ai;
    if (i == 0) {
      T = ti;
      A = ai;
      dd(T, A) continue;
    }

    ll p = 1;
    while (ti * p < T or ai * p < A) {
      p++;
    }
    ti *= p;
    ai *= p;
    T = ti;
    A = ai;

    dd(T, A)
  }

  cout << 0ll + T + A << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// debug
#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl;
#define ddd(x, y, z)                                                           \
  cerr << "(" #x "," #y "," #z "):(" << x << "," << y << "," << z << ")"       \
       << endl;
#define dump(v)                                                                \
  cerr << #v ":[ ";                                                            \
  for (auto macro_vi : v) {                                                    \
    cerr << macro_vi << " ";                                                   \
  }                                                                            \
  cerr << "]" << endl;
#define ddump(v)                                                               \
  cerr << #v ":" << endl;                                                      \
  for (auto macro_row : v) {                                                   \
    cerr << "[";                                                               \
    for (auto macro__vi : macro_row) {                                         \
      cerr << macro__vi << " ";                                                \
    }                                                                          \
    cerr << "]" << endl;                                                       \
  }
#define damp(m)                                                                \
  for (auto macro_pair : m) {                                                  \
    cerr << macro_pair.first << ":" << macro_pair.second << endl;              \
  }
#define dpair(p)                                                               \
  cerr << #p ":"                                                               \
       << "(" << p.first << "," << p.second << ")" << endl;
// iterate
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) reprrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(v) v.begin(), v.end()
// etc
#define chmin(mi, val) mi = min(mi, val);
#define chmax(ma, val) ma = max(ma, val);
using ll = long long;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  ll T = 0ll, A = 0ll;
  rep(i, n) {
    ll ti, ai;
    cin >> ti >> ai;
    if (i == 0) {
      T = ti;
      A = ai;
      dd(T, A) continue;
    }
    dd(ti, ai) ll p = 1;
    p = max((T + ti - 1) / ti, (A + ai - 1) / ai);
    d(p) ti *= p;
    ai *= p;
    T = ti;
    A = ai;

    dd(T, A)
  }

  cout << 0ll + T + A << endl;
  return 0;
}