Language: C++

//include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;
//conversion//include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// chmax chmin
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

long long modinv(long long a, long long p){
    // a^{-1} = 1/a mod p (拡張Euclidの互除法)
    long long b = p, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= p;
    if (u < 0) u += p;
    return u;
}

void extEuclid(long long a, long long b, long long *x, long long *y){
    *x = modinv(a, b);
    *y = (a*(*x)-1) / b;
    if(*x < 0 || *y < 0){
        *x += b;
        *y += a;
    }
}

int main(void){

    int N;
    cin >> N;
    ll T[N], A[N];
    REP(i,N) cin >> T[i] >> A[i];

    ll sumT=T[0], sumA=A[0];
    
    FOR(i,1,N){
        ll mul = T[i]*sumA - A[i]*sumT;
        if(mul == 0) continue;
        ll x,y;
        if(mul > 0) extEuclid(A[i], T[i], &x, &y);
        else extEuclid(T[i], A[i], &y, &x);
        x *= abs(mul);
        y *= abs(mul);
        ll left = 0, right = 100000000000000000;
        ll n = (left+right)/2;
        REP(j,10000000){
            if(x-n*T[i] >= 0 && y-n*A[i] >= 0){
                if(x-(n+1)*T[i] < 0 || y-(n+1)*A[i] < 0) break;
                left = n;
            }
            else{
                right = n;
            }
            n = (left+right)/2;
        }
        x -= n*T[i];
        y -= n*A[i];
        sumT += x;
        sumA += y;
    }

    cout << sumT+sumA << endl;
    return 0;
}
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// chmax chmin
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

long long modinv(long long a, long long p){
    // a^{-1} = 1/a mod p (拡張Euclidの互除法)
    long long b = p, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= p;
    if (u < 0) u += p;
    return u;
}

void extEuclid(long long a, long long b, long long *x, long long *y){
    *x = modinv(a, b);
    *y = (a*(*x)-1) / b;
    if(*x < 0 || *y < 0){
        *x += b;
        *y += a;
    }
}

int main(void){

    int N;
    cin >> N;
    ll T[N], A[N];
    REP(i,N) cin >> T[i] >> A[i];

    ll sumT=T[0], sumA=A[0];
    
    FOR(i,1,N){
        ll mul = T[i]*sumA - A[i]*sumT;
        if(mul == 0) continue;
        ll x,y;
        if(mul > 0) extEuclid(A[i], T[i], &x, &y);
        else extEuclid(T[i], A[i], &y, &x);
        x *= abs(mul);
        y *= abs(mul);
        ll left = 0, right = 1000000000000000;
        ll n = (left+right)/2;
        REP(j,10000000){
            if(x-n*T[i] >= 0 && y-n*A[i] >= 0){
                if(x-(n+1)*T[i] < 0 || y-(n+1)*A[i] < 0) break;
                left = n;
            }
            else{
                right = n;
            }
            n = (left+right)/2;
        }
        x -= n*T[i];
        y -= n*A[i];
        sumT += x;
        sumA += y;
    }

    cout << sumT+sumA << endl;
    return 0;
}

Compiler output:
a.cc:111:12: error: redefinition of 'int toInt(std::string)'
  111 | inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
      |            ^~~~~
a.cc:11:12: note: 'int toInt(std::string)' previously defined here
   11 | inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
      |            ^~~~~
a.cc:112:33: error: redefinition of 'template<class T> std::string toString(T)'
  112 | template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
      |                                 ^~~~~~~~
a.cc:12:33: note: 'template<class T> std::string toString(T)' previously declared here
   12 | template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
      |                                 ^~~~~~~~
a.cc:115:28: error: redefinition of 'template<class T> T sqr(T)'
  115 | template<class T> inline T sqr(T x) {return x*x;}
      |                            ^~~
a.cc:15:28: note: 'template<class T> T sqr(T)' previously declared here
   15 | template<class T> inline T sqr(T x) {return x*x;}
      |                            ^~~
a.cc:139:14: error: redefinition of 'const double EPS'
  139 | const double EPS = 1e-10;
      |              ^~~
a.cc:39:14: note: 'const double EPS' previously defined here
   39 | const double EPS = 1e-10;
      |              ^~~
a.cc:140:14: error: redefinition of 'const double PI'
  140 | const double PI  = acos(-1.0);
      |              ^~
a.cc:40:14: note: 'const double PI' previously defined here
   40 | const double PI  = acos(-1.0);
      |              ^~
a.cc:147:31: error: redefinition of 'template<class T> bool chmax(T&, T)'
  147 | template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
      |                               ^~~~~
a.cc:47:31: note: 'template<class T> bool chmax(T&, T)' previously declared here
   47 | template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
      |                               ^~~~~
a.cc:148:31: error: redefinition of 'template<class T> bool chmin(T&, T)'
  148 | template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
      |                               ^~~~~
a.cc:48:31: note: 'template<class T> bool chmin(T&, T)' previously declared here
   48 | template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
      |                               ^~~~~
a.cc:150:11: error: redefinition of 'long long int modinv(long long int, long long int)'
  150 | long long modinv(long long a, long long p){
      |           ^~~~~~
a.cc:50:11: note: 'long long int modinv(long long int, long long int)' previously defined here
   50 | long long modinv(long long a, long long p){
      |           ^~~~~~
a.cc:163:6: error: redefinition of 'void extEuclid(long long int, long long int, long long int*, long long int*)'
  163 | void extEuclid(long long a, long long b, long long *x, long long *y){
      |      ^~~~~~~~~
a.cc:63:6: note: 'void extEuclid(long long int, long long int, long long int*, long long int*)' previously defined here
   63 | void extEuclid(long long a, long long b, long long *x, long long *y){
      |      ^~~~~~~~~
a.cc:172:5: error: redefinition of 'int main()'
  172 | int main(void){
      |     ^~~~
a.cc:72:5: note: 'int main()' previously defined here
   72 | int main(void){
      |     ^~~~