Problem p03964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define i64 long long
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define INF I64_MAX
#define MOD 1000000007
#define MEM_SIZE = 1e5 + 5;

template <typename T> void DEBUG(T e) { std::cout << e << std::endl; }
template <class T> void DEBUG(string str, T e) {
  std::cout << str << ">>" << e << std::endl;
}
template <typename T> void DEBUG(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    DEBUG(v);
  }
}
template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    exit(0);
  }
}

//-----CODE------//

void solve(void) {
  int N;
  cin >> N;
  int A, B;
  cin >> A >> B;
  for (int i = 0; i < N - 1; i++) {
    int X, Y;
    cin >> X >> Y;
    int M = 2;
    while (X < A || Y < B) {
      X = X * M / (M - 1);
      Y = Y * M / (M - 1);
      M++;
    }
    A = X;
    B = Y;
  }
  cout << A + B << endl;

  return;
}

int32_t main(int32_t argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(9);
  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define i64 long long
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define INF I64_MAX
#define MOD 1000000007
#define MEM_SIZE = 1e5 + 5;

template <typename T> void DEBUG(T e) { std::cout << e << std::endl; }
template <class T> void DEBUG(string str, T e) {
  std::cout << str << ">>" << e << std::endl;
}
template <typename T> void DEBUG(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    DEBUG(v);
  }
}
template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    exit(0);
  }
}

//-----CODE------//

void solve(void) {
  int N;
  cin >> N;
  int A, B;
  cin >> A >> B;
  for (int i = 0; i < N - 1; i++) {
    int X, Y;
    cin >> X >> Y;

    if (X < A || Y < B) {
      int M = max(A % X ? A / X + 1 : A / X, B % Y ? B / Y + 1 : B / Y);
      X = X * M;
      Y = Y * M;
    }
    A = X;
    B = Y;
  }
  cout << A + B << endl;

  return;
}

int32_t main(int32_t argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(9);
  solve();

  return 0;
}