Problem:
 Score : 300 points    Problem Statement AtCoDeer the deer is seeing a quick report of election results on TV.
Two candidates are standing for the election: Takahashi and Aoki.
The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.
AtCoDeer has checked the report N times, and when he checked it for the i -th (1≦i≦N) time, the ratio was T_i:A_i .
It is known that each candidate had at least one vote when he checked the report for the first time.  Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N -th time.
It can be assumed that the number of votes obtained by each candidate never decreases.      Constraints  1≦N≦1000  1≦T_i,A_i≦1000 (1≦i≦N)  T_i and A_i  (1≦i≦N) are coprime.  It is guaranteed that the correct answer is at most 10^{18} .         Input The input is given from Standard Input in the following format:  N  T_1  A_1  T_2  A_2  :  T_N  A_N       Output Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N -th time.        Sample Input 1 3
2 3
1 1
3 2      Sample Output 1 10  When the numbers of votes obtained by the two candidates change as 2,3 → 3,3 → 6,4 , the total number of votes at the end is 10 , which is the minimum possible number.       Sample Input 2 4
1 1
1 1
1 5
1 100      Sample Output 2 101  It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.       Sample Input 3 5
3 10
48 17
31 199
231 23
3 2      Sample Output 3 6930

Solution (Python):
N = int(input())

def check(t, a):
    T, A = map(int, input().split())
    t = T * max((t + (T-1))//T, (a + (A-1))//A)
    a = A * max((t + (T-1))//T, (a + (A-1))//A)
    return t, a

t = 1
a = 1
for _ in range(N):
    t, a = check(t, a)

print (t + a)

Solution (Python):
N = int(input())

def check(t, a):
    T, A = map(int, input().split())
    if T >= t and A >= a:
        t = T
        a = A
        return t, a
    if T >= t and A < a:
        t = T * ((a + (A-1))//A)
        a = A * ((a + (A-1))//A)
        return t, a
    if T < t and A >= a:
        t = T * ((t + (T-1))//T)
        a = A * ((t + (T-1))//T)
        return t, a
    if T < t and A < a:
        t = T * max((t + (T-1))//T, (a + (A-1))//A)
        a = A * max((t + (T-1))//T, (a + (A-1))//A)
        return t, a
t = 0
a = 0
for _ in range(N):
    t, a = check(t, a)

print (t + a)

Solution (Python):
N = int(input())

T = [0] * N
A = [0] * N
for i in range(N):
    A[i], T[i] = map(int, input().split())

tmp_T = T[0]
tmp_A = A[0]

for i in range(1,N):
    ma = tmp_A // A[i] + (1 if tmp_A % A[i] > 0 else 0)
    mt = tmp_T // T[i] + (1 if tmp_T % T[i] > 0 else 0)
    n = max(mt,ma)
    tmp_A = n * A[i]
    tmp_T = n * T[i]

print(tmp_T + tmp_A)

Solution (Python):
import math
import sys
from decimal import Decimal


N = int(sys.stdin.readline())
TA = []
for i in range(N):
    TA.append(list(map(int, sys.stdin.readline().split())))

current_T = 1
current_A = 1
for i in range(N):
    count = max(math.ceil(Decimal(current_T)/Decimal(TA[i][0])), math.ceil(Decimal(current_A)/Decimal(TA[i][1])))
    current_T = TA[i][0] * count
    current_A = TA[i][1] * count

print(current_T + current_A)

Solution (Python):
import math
import sys
from decimal import Decimal


N = int(sys.stdin.readline())
TA = []
for i in range(N):
    TA.append(list(map(int, sys.stdin.readline().split())))

current_T = 1
current_A = 1
for i in range(N):
    count = max(math.ceil(Decimal(current_T)/Decimal(TA[i][0])), math.ceil(Decimal(current_A)/Decimal(TA[i][1])))
    current_T = TA[i][0] * count
    current_A = TA[i][1] * count

print(current_T + current_A)

Solution (Python):
N = int(input())

lst_S = []
for i in range(N):
    lst_S.append(input().split())
    lst_S[i][0] = int(lst_S[i][0])
    lst_S[i][1] = int(lst_S[i][1])

bote_A = lst_S[0][0]
bote_B = lst_S[0][1]

for i in range(1, N):
    bai_A = (bote_A - 1) // lst_S[i][0]
    bai_B = (bote_B - 1) // lst_S[i][1]

    bai = max(bai_A, bai_B) + 1

    bote_A = lst_S[i][0] * bai
    bote_B = lst_S[i][1] * bai

ans = bote_A + bote_B

print(ans)

Solution (Python):
N = int(input())
TA = [list(map(int,input().split())) for i in range(N)]

vote_T = TA[0][0]
vote_A = TA[0][1]
for i in range(1,N):
  var_T = (vote_T + (TA[i][0]-1)) // TA[i][0]
  var_A = (vote_A + (TA[i][1]-1)) // TA[i][1]
  multiple = max(var_T, var_A)
  vote_T = TA[i][0] * multiple
  vote_A = TA[i][1] * multiple
  
print(vote_T + vote_A)

Solution (Python):
n = int(input())
ta = []
for i in range(n):
    ta.append(input().split())

l_sum = int(ta[0][0])
r_sum = int(ta[0][1])

for i in range(n):
    if int(ta[i][0]) > l_sum and int(ta[i][1]) > r_sum:
        l_sum = int(ta[i][0])
        r_sum = int(ta[i][1])
    elif int(ta[i][0]) <= l_sum and int(ta[i][1]) > r_sum:
        times_num = -(- l_sum // int(ta[i][0]))
        l_sum = int(ta[i][0]) * times_num
        r_sum = int(ta[i][1]) * times_num
    elif int(ta[i][0]) > l_sum and int(ta[i][1]) <= r_sum:
        times_num = -(- r_sum // int(ta[i][1]))
        l_sum = int(ta[i][0]) * times_num
        r_sum = int(ta[i][1]) * times_num
    elif int(ta[i][0]) <= l_sum and int(ta[i][1]) <= r_sum:
        r_times_num = -(- r_sum // int(ta[i][1]))
        l_times_num = -(- l_sum // int(ta[i][0]))
        times_num = max(r_times_num, l_times_num)
        l_sum = int(ta[i][0]) * times_num
        r_sum = int(ta[i][1]) * times_num
print(l_sum + r_sum)

Solution (Python):
import math
n = int(input())
a,b = 1,1
for i in range(n):
    x,y = map(int, input().split(' '))
    tmp = max(-(-a//x), -(-b//y))
    a = x * tmp
    b = y * tmp
print(a+b)

Solution (Python):
import math
n = int(input())
ta_li = [list(map(int, input().split())) for _ in range(n)]

def current_result(t, a, x, y):
  
  
  tx, ay = 0, 0
  if t % x == 0:
    tx = t // x 
  else:
    tx = t // x + 1
  if a % y == 0:
    ay = a // y
  else:
    ay = a // y + 1
  n = max(tx, ay)
  return n*x, n*y

if __name__ == "__main__":
  t, a = 1, 1
  for i in range(n):
    x, y = ta_li[i]
    t, a = current_result(t, a, x, y)
  print(t+a)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
from decimal import Decimal

mod = 10**9+7
sys.setrecursionlimit(mod) 

N = int(input())
TA = [[] for i in range(N)]
for i in range(N):
    TA[i] = [int(i) for i in input().split()]

min_T,min_A = 1,1
for i in range(N):
    t,a = TA[i]
    
    multi = max(math.ceil(Decimal(min_T) / Decimal(t)),math.ceil(Decimal(min_A)/Decimal(a)),1)
    min_T,min_A = t*multi, a*multi

print(min_A+min_T)

Solution (Python):
import itertools
from math import ceil
from decimal import Decimal

N = input()
N = int(N)
lasta,lastb = 1,1
temp = []
for _ in range(N):
    temp.append(list(map(int,input().split(" "))))


for hoge in temp:
    a,b = hoge
    diffa = ceil(Decimal(lasta)/Decimal(a))
    diffb = ceil(Decimal(lastb)/Decimal(b))
    diff = max(diffa,diffb)
    lasta,lastb = a*diff,b*diff
print(lasta+lastb)

Solution (Python):

n = int(input())
ta = [list(map(int,input().split())) for i in range(n)]

cur_t = 1
cur_a = 1


def ceil(a, b):
    return a // b + int(a % b != 0)

for t,a in ta:
    if cur_t > t or cur_a > a:
        rate = max([ceil(cur_t,t),ceil(cur_a,a)])
        cur_t = t*rate
        cur_a = a*rate
    else:
        cur_t=t
        cur_a=a
print(cur_t+cur_a)

Solution (Python):
N = int(input())
a,b = 1,1
for i in range(N):
    aa,bb = map(int,input().split())
    k = max((a+aa-1)//aa,(b+bb-1)//bb)
    aa *= k
    bb *= k
    a,b = aa,bb

print(a+b)

Solution (Python):
n = int(input())

a = 0
b = 0

for _ in range(n):
    x, y = map(int, input().split())
    if not a:
        a, b = x, y
        continue
    z = max(-(-a // x), -(-b // y))
    a, b = z * x, z * y

print(a + b)

Solution (Python):


import math
import decimal


N = int(input())
tn,an = [1,1]
for _ in range(N):
  t,a = map(int, input().split())
  x = max(math.ceil(decimal.Decimal(tn)/decimal.Decimal(t)),math.ceil(decimal.Decimal(an)/decimal.Decimal(a)))
  tn,an = [t*x,a*x]
print(tn+an)

Solution (Python):

N = int(input())
TA = []
for i in range(N):
    TA.append(list(map(int, input().split())))

t = TA[0][0]
a = TA[0][1]
flag = True
for i in range(1,  N+1):
    tmp_t = 0
    tmp_a = 0
    if i == N:
        flag = False
        break
    k = max(t // TA[i][0], a // TA[i][1])   
    while flag:   
        tmp_t = k * TA[i][0]
        tmp_a = k * TA[i][1]
        if tmp_t >= t and tmp_a >= a:
            t = tmp_t
            a = tmp_a
            break
        k += 1
    
print(t+a)

Solution (Python):
from math import ceil
from decimal import Decimal

N = int(input())
TA = [list(map(int, input().split())) for _ in range(N)]

a = 1
b = 1
for x, y in TA:
    n = max(ceil(Decimal(a) / Decimal(x)), ceil(Decimal(b) / Decimal(y)))
    a = n * x
    b = n * y
print(a + b)

Solution (Python):
from math import ceil

N = int(input())
TA = [list(map(int, input().split())) for _ in range(N)]

def ceil(a, b):
    return a // b + int(a % b != 0)

a = 1
b = 1
for x, y in TA:
    n = max(ceil(a, x), ceil(b, y))
    a = n * x
    b = n * y
print(a + b)

Solution (Python):
from math import ceil

N = int(input())
TA = [list(map(int, input().split())) for _ in range(N)]

a = 1
b = 1
for x, y in TA:
    n = max(-(-a // x), -(-b // y))
    a = n * x
    b = n * y
print(a + b)

Solution (Python):
N = int(input())

t = 0
a = 0
for _ in range(N):
  rt,ra = map(int,input().split())
  c = max((t-1)//rt+1, (a-1)//ra+1)
  t = rt*c
  a = ra*c
  if a+t == 0:
    t += rt
    a += ra

    
answer = t+a
print(answer)

Solution (Python):
n = int(input())
T = 1
A = 1
for _ in range(n):
    i = 1
    t, a = map(int, input().split())
    i = max((T + t - 1) // t, (A + a -1) // a)
    T = i * t
    A = i * a
print(T + A)

Solution (Python):
from copy import deepcopy as copy
n = int(input())
T = [1]
A = [1]
for i in range(n):
    t, a = map(int, input().split())
    tc = copy(t)
    ac = copy(a)
    if T[-1] > t or A[-1] > a:
        multi = max(T[-1] // t, A[-1] // a) + 1
        t *= multi
        a *= multi
        while t - tc >= T[-1] and a - ac >= A[-1]:
            t -= tc
            a -= ac
    T.append(t)
    A.append(a)
print(T[-1] + A[-1])

Solution (Python):
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]


    mint, mina =  f()
    for i in range(n-1):
        t,a = f()

        m = max((mint+t-1)//t, (mina+a-1)//a)
        
        mint = m * t
        mina = m * a

    print(mint + mina)


solve()

Solution (Python):
N = int(input())
T, A = map(int, input().split())
for i in range(N - 1):
    Ti, Ai = map(int, input().split())
    mt = T // Ti + (1 if T % Ti > 0 else 0)
    ma = A // Ai + (1 if A % Ai > 0 else 0)
    m = max(mt, ma)
    Ti *= m
    Ai *= m
    T, A = Ti, Ai
print(T + A)

Solution (Python):
def ceil_div(x, y):
    return (x + y - 1) // y

n = int(input())
ans = 1
tt = 1
aa = 1
for i in range(n):
    t, a = map(int, input().split())
    x = t + a
    p = max(ceil_div(ans, x), ceil_div(tt, t), ceil_div(aa, a))
    ans = x * p
    aa = a * p
    tt = t * p
print(ans)

Solution (Python):
n = int(input())
t, a = list(map(int, input().split()))
for _ in range(n-1):
    ti, ai = list(map(int, input().split()))
    mt = t // ti if t % ti == 0 else t // ti + 1
    ma = a // ai if a % ai == 0 else a // ai + 1
    m = max(mt, ma)

    t = m * ti
    a = m * ai

print(t + a)

Solution (Python):
N = int(input())

t_pre = 1
a_pre = 1
for i in range(N):
    t, a = map(int, input().split())
    if t_pre<=t and a_pre<=a:
        t_pre = t
        a_pre = a
    else:
        n = max((t_pre+t-1)//t,(a_pre+a-1)//a)        
        t_pre = n*t
        a_pre = n*a
print(t_pre+a_pre)

Solution (Python):
n = int(input())
t_, a_ = 1, 1
for _ in range(n):
    x, y = map(int, input().split())
    l = max(((t_ - 1) // x), ((a_ - 1) // y)) + 1
    t_ = l * x
    a_ = l * y
print(t_ + a_)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
L = [list(map(int, input().split())) for _ in range(N)]

def gcd(a, b):
    if b > a:
        a, b = b, a
    if a % b == 0:
        return b
    return gcd(b, a % b)

l, r = 1, 1

for x, y in L:
    c = max(l // x, r // y)
    while True:
        xx = x * c
        yy = y * c
            
        if l <= xx and r <= yy:
            l, r = xx, yy
            break
        else:
            c += 1

print(l + r)

Solution (Python):
n=int(input())
a,b=map(int,input().split())
for i in range(n-1):
  ta,tb=map(int,input().split())
  if ta>=a:
    a=ta
  else:
    if a%ta!=0:
      a=(a//ta+1)*ta
  if tb>=b:
    b=tb
  else:
    if b%tb!=0:
      b=(b//tb+1)*tb
  qa,qb=a//ta,b//tb
  if qa!=qb:
    if qa>qb:
      b=b*qa//qb
    elif qb>qa:
      a=a*qb//qa
print(a+b)

Solution (Python):
from decimal import Decimal, ROUND_CEILING

n = int(input())
ta_array = [[int(x) for x in input().split()] for y in range(n)]
res = 1
prev_t, prev_a = ta_array.pop(0)
for t, a in ta_array:
    res_a = Decimal(prev_a*res/a).quantize(Decimal(0.0), rounding=ROUND_CEILING)
    res_t = Decimal(prev_t*res/t).quantize(Decimal(0.0), rounding=ROUND_CEILING)
    res = max(res_a, res_t)
    prev_t = t
    prev_a = a
print(prev_t*res+prev_a*res)

Solution (Python):
from math import ceil
from decimal import *

N = int(input())
T, A = Decimal(1), Decimal(1)
for t, a in [map(int, input().split()) for _ in range(N)]:
    n = int(max(ceil(T / Decimal(t)), ceil(A / Decimal(a))))
    T, A = n * t, n * a
print(T + A)

Solution (Python):
from math import ceil

N = int(input())
T, A = 1, 1
for t, a in [map(int, input().split()) for _ in range(N)]:
    n = int(max(T // t + (0 if T%t==0 else 1), A//a + (0 if A%a==0 else 1) ))
    T, A = n * t, n * a
print(T + A)

Solution (Python):
N = int(input())
T, A = 1, 1
for t, a in [map(int, input().split()) for _ in range(N)]:
    n = max(-(-T // t), -(-A // a))
    T, A = n * t, n * a
print(T + A)

Solution (Python):
N = int(input())
T, A = 1, 1
for t, a in [map(int, input().split()) for _ in range(N)]:
    n = max(-(-T // t), -(-A // a))
    T, A = n * t, n * a
print(T + A)

Solution (Python):
from math import floor

N = int(input())
T, A = 1, 1
for t, a in [map(int, input().split()) for _ in range(N)]:
    n = max(-(-T // t), -(-A // a))
    T, A = n * t, n * a
print(T + A)

Solution (Python):
N = int(input())
TA = [map(int, input().split()) for _ in range(N)]

T, A = 1, 1
for t, a in TA:
    n = max(-(- T // t), -(- A // a))
    T, A = n * t, n * a
print(T + A)

Solution (Python):
N=int(input())
T,A=1,1
for _ in range(N):
    t,a = map(int,input().split())
    kt=(T-1)//t + 1
    ka=(A-1)//a + 1
    if ka*t<T:
        k=kt
    elif kt*a<A:
        k=ka
    elif t<a:
        k=kt
    else:
        k=ka
    T=k*t
    A=k*a
print(A+T)

Solution (Python):
N = int(input())

L = []
for i in range(N):
    t,a = map(int, input().split())
    L.append((t,a))


import math
from decimal import Decimal

check1 = (1, 1)
for i in range(N):
    check2 = L[i]
    
    x1 = check1[0]
    y1 = check1[1]
    
    x2 = check2[0]
    y2 = check2[1]
    check2 = L[i]
    


    
    a1 = Decimal(math.ceil(x1 / x2))
    a2 = Decimal(math.ceil(y1 / y2))
    

        
    a = max(a1, a2)
        
    check1 = (x2*a, y2*a) 
    





print(sum(check1))

Solution (Python):
import math

n = int(input())

t_, a_ = 1, 1
for i in range(n):
    t, a =[int(n) for n in input().split()]

    if t_ % t == 0:
        pt = 0
    else:
        pt = 1
    
    if a_ % a == 0:
        pa = 0
    else:
        pa = 1

    m = max((t_//t + pt), (a_//a + pa))
    t_, a_ = t * m, a * m
    
ans = t_ + a_
print(ans)

Solution (Python):
N = int(input())
R = [list(map(int, input().split())) for _ in range(N)]
T = R[0][0]
A = R[0][1]
for r in R[1:]:
    i = T // r[0]
    if T % r[0] != 0:
        i += 1
    j = A // r[1]
    if A % r[1] != 0:
        j += 1
    T = r[0] * max(i, j)
    A = r[1] * max(i, j)
print(T+A)

Solution (Python):
def main():
	N = int(input())
	T = [0]*N
	A = [0]*N
	for i in range(N):
		T[i], A[i] = map(int, input().split())

	total_t = T[0]
	total_a = A[0]
	for i in range(N):
		if total_t == T[i] and total_a == A[i]:
			continue
		current_t = total_t//T[i] if total_t%T[i]==0 else (total_t//T[i])+1
		current_a = total_a//A[i] if total_a%A[i]==0 else (total_a//A[i])+1
		n = max(current_t, current_a)
		total_t = n*T[i]
		total_a = n*A[i]

	print(total_a+total_t)


if __name__ == '__main__':
	main()

Solution (Python):
import math


def read_input():
    n = int(input())

    ratios = []
    for i in range(n):
        t, a = map(int, input().split())
        ratios.append((t, a))

    return n, ratios


def get_votes(ex_votes, ratio):
    def check_votes(a, b):
        return a[0] <= b[0] and a[1] <= b[1]

    def div_ceil(a, b):
        if a % b == 0:
            return a //b
        else:
            return a // b + 1

    base = div_ceil(sum(ex_votes), (ratio[0] + ratio[1]))
    base = int(base)
    while True:
        test_votes = (ratio[0] * base, ratio[1] * base)
        if check_votes(ex_votes, test_votes):
            return test_votes
        else:
            diff_votes = (test_votes[0] - ex_votes[0], test_votes[1] - ex_votes[1])
            if diff_votes[0]/ratio[0] < diff_votes[1]/ratio[1]:
                base += div_ceil(-diff_votes[0], ratio[0])
            else:
                base += div_ceil(-diff_votes[1], ratio[1])
            base = int(base)


def submit():
    n, ratios = read_input()

    votes = ratios[0]
    for ratio in ratios[1:]:
        votes = get_votes(votes, ratio)

    print(sum(votes))


if __name__ == '__main__':
    submit()

Solution (Python):
n = int(input())
x = y = 1
for i in range(n):
    t, a = map(int, input().split())
    bx = 1 if x % t else 0
    by = 1 if y % a else 0
    k = max(x//t + bx, y//a + by)
    x, y = t*k, a*k
print(x+y)

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    T, A = 1, 1
    for _ in range(N):
        t, a = map(int, input().split())

        n = max((T+t-1)//t, (A+a-1)//a)
        T = t * n
        A = a * n

    return T + A


if __name__ == '__main__':
    print(main())

Solution (Python):
n = int(input())
ta = [[int(item) for item in input().split()] for _ in range(n)]

p, q = ta[0]
for t, a in ta[1:]:
    rate1 = p//t
    rate2 = q//a
    if p % t != 0:
        rate1 += 1
    if q % a != 0:
        rate2 += 1
    rate = max(rate1, rate2)
    p = t*rate 
    q = a*rate 
print(p + q)

Solution (Python):
N = int(input())
tans = 1
aans = 1
for i in range(N):
    t, a = map(int, input().split())
    n = max(-(-tans // t), -(-aans // a))
    tans = n * t
    aans = n * a
print(aans + tans)

Solution (Python):
def c_atcodeer_and_election_report(N, R):
    vote_takahashi, vote_aoki = 1, 1
    for x, y in R:
        
        n = max(-(-vote_takahashi // x), -(-vote_aoki // y))
        vote_takahashi = n * x
        vote_aoki = n * y
    return vote_takahashi + vote_aoki

N = int(input())
R = [[int(i) for i in input().split()] for j in range(N)]
print(c_atcodeer_and_election_report(N, R))

Solution (Python):
import math


def ceil(a: int, b: int) -> int:
    '''a / b の切り上げを求める。
    '''
    n = a // b
    return n if n * b == a else n + 1


def atcodeer_and_election_report(N: int, ratios: list) -> int:
    t, a = ratios[0]

    for nt, na in ratios[1:]:
        
        
        k = max(ceil(t, nt), ceil(a, na))
        t, a = nt * k, na * k

    return t + a


if __name__ == "__main__":
    N = int(input())
    ratios = [tuple(int(s) for s in input().split()) for _ in range(N)]
    ans = atcodeer_and_election_report(N, ratios)
    print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
TA = [LIST() for _ in range(N)]
vote_T = 1
vote_A = 1
for x, y in TA:
	
	
	n = max((vote_T//x + (vote_T%x!=0)),vote_A//y + (vote_A%y!=0))
	vote_T = x * n
	vote_A = y * n
print(vote_A + vote_T)

Solution (Python):
n = int(input())

li_hi = list()
a = 1
b = 1
for i in range(n):
    li_hi.append(list(map(int, input().split())))

for l in li_hi:
    w = max(-(-a // l[0]), -(-b // l[1]))
    a = w * l[0]
    b = w * l[1]

print(a + b)

Solution (Python):
import math
from fractions import Fraction
n = int(input())

pt = [0] * n
pa = [0] * n

for i in range(n):
  t, a = map(int, input().split())
  if i == 0:
    pt[0] = t
    pa[0] = a
  else:
    tmp = math.ceil(max(Fraction(pt[i-1], t), Fraction(pa[i-1], a)))
    pt[i]=tmp*t
    pa[i]=tmp*a

print(int(pt[-1] + pa[-1]))

Solution (Python):
from functools import reduce

def main():
    
    

    
    
    
    
    
    
    
    

    
    
    
    
    N = int(input().rstrip())
    T=[]
    A=[]
    x=[]
    ans = 0
    for i in range(N):
        t, a = [int(_) for _ in input().rstrip().split(' ')]
        T.append(t)
        A.append(a)
    x.append(1)
    for i in range(1,N):
        x.append(max(-1*(-1*(T[i-1] *  x[i-1])//T[i]),-1*(-1*(A[i-1] *  x[i-1])//A[i])))
        t = x[i] * T[i]
        a = x[i] * A[i]
    print(t+a)
if __name__ == '__main__':
    main()

Solution (Python):
import math
from fractions import Fraction
N = int(input())
ta = [list(map(int,input().split())) for _ in range(N)]
x = 1
y = 1
for i in range(N):
    if i == 0:
        x , y = ta[i][0] , ta[i][1]
    else:
        temp = int(math.ceil(max( Fraction(x ,  ta[i][0])  , Fraction(y , ta[i][1]))))
        x = temp * ta[i][0]
        y = temp * ta[i][1]
        
print(x + y)

Solution (Python):
import sys
n=int(input())
l=[[int(i) for i in l.split()] for l in sys.stdin]
for i in range(n-1):
  if l[i]==l[i+1]:
    continue
  x=max((l[i][0]+l[i+1][0]-1)//l[i+1][0],(l[i][1]+l[i+1][1]-1)//l[i+1][1])
  l[i+1][0]*=x
  l[i+1][1]*=x
print(sum(l[-1]))

Solution (Python):
import math
from fractions import Fraction
N = int(input())
tmin = 1
amin = 1
for i in range(N):
    t, a = map(int, input().split())
    multiple = math.ceil(max(Fraction(tmin, t), Fraction(amin, a)))
    tmin = t * multiple
    amin = a * multiple
print(tmin + amin)

Solution (Python):
import math
N = int(input())
tmin = 1
amin = 1
for i in range(N):
    t, a = map(int, input().split())
    multiple = max((tmin -  1) // t, (amin - 1) // a) + 1
    tmin = t * multiple
    amin = a * multiple
print(tmin + amin)

Solution (Python):


















import math
import copy
from collections import defaultdict, Counter

from itertools import product

from itertools import permutations

from itertools import combinations
from bisect import bisect_left, bisect_right


def s_inpl(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
INF = float("inf")

N = int(input())
T, A = [], []
for _ in range(N):
    t, a = s_inpl()
    T.append(t)
    A.append(a)

ans_t, ans_a = 1, 1
for ti, ai in zip(T, A):
    n = max(ans_t//ti, ans_a//ai)
    if ans_t>ti*n or ans_a>ai*n:
        n += 1
    ans_t = ti*n
    ans_a = ai*n

print(ans_t+ans_a)

Solution (Python):
n=int(input());a,b=map(int,input().split())
for i in range(n-1):
  c,d=map(int,input().split())
  m=max((a-1)//c,(b-1)//d)+1
  a,b=c*m,d*m
print(a+b)

Solution (Python):
import sys
import math
import bisect
import heapq
import copy
from fractions import gcd
from itertools import permutations, combinations,combinations_with_replacement,product
from collections import defaultdict, Counter, deque

N = int(input())
T, A = [0]*N, [0]*N
for i in range(N):
    T[i], A[i] = map(int, input().split())
a, b = T[0], A[0]
if N == 1:
    print(a+b)
else:
    for x, y in zip(T[1:], A[1:]):
        c = max((a+x-1)//x, (b+y-1)//y)
        a, b = x*c, y*c
    print(a+b)

Solution (Python):
import sys
import math
import bisect
import heapq
import copy
from fractions import gcd
from itertools import permutations, combinations,combinations_with_replacement,product
from collections import defaultdict, Counter, deque

N = int(input())
T, A = [0]*N, [0]*N
for i in range(N):
    T[i], A[i] = map(int, input().split())
a, b = T[0], A[0]
if N == 1:
    print(a+b)
else:
    for x, y in zip(T[1:], A[1:]):
        c = max((a-1)//x+1, (b-1)//y+1)
        a, b = x*c, y*c
    print(a+b)

Solution (Python):
from math import ceil
from fractions import Fraction
n = int(input())
res1,res2 = 1,1
for i in range(n):
    t,a = map(int,input().split())
    r = ceil(max(Fraction(res1,t),Fraction(res2,a)))
    res1 = t*r
    res2 = a*r
print(res1+res2)

Solution (Python):
from fractions import gcd
import math

N=int(input())

def divceil(a,b):
  if a%b==0:
    return a//b
  else:
    return a//b + 1

T=1
A=1
for _ in range(N):
  t,a=list(map(int, input().split()))
  m = max(divceil(T,t), divceil(A,a))
  T = t*m
  A = a*m
  
print(T+A)

Solution (Python):
N = int(input())
min_T, min_A = 1,1

for _ in range(N):
    T, A = map(int,input().split())
    i =max(-(-min_A // A) , -(-min_T // T))
    min_T = T * i
    min_A = A * i
print(min_T+min_A)

Solution (Python):
n  =int(input())
ta=[]
for i in range(n):
    ta.append(list(map(int,input().split())))

import math
for i in range(n-1):
    bai = max(math.ceil((ta[i][0]-1)//ta[i+1][0]+1),math.ceil((ta[i][1]-1)//ta[i+1][1]+1))
    ta[i+1][0]=ta[i+1][0]*bai
    ta[i+1][1]=ta[i+1][1]*bai
print(ta[n-1][0]+ta[n-1][1])

Solution (Python):
n = int(input())
ta = [[int(i) for i in input().split()] for i in range(n)]

ct = ta[0][0]
ca = ta[0][1]

for i in range(1, n):
    t, a = ta[i]

    if ct / t > ca / a:
        if ct%t == 0:
            rate = ct//t
        else:
            rate = ct//t+1
    else:
        if ca%a == 0:
            rate = ca//a
        else:
            rate = ca//a+1
    ct = rate * t
    ca = rate * a
print(ct+ca)

Solution (Python):
import math

n = int(input())
a,b = 1,1
for _ in range(n):
  x,y = [int(_) for _ in input().split(" ")]
  l = max(-(-a//x), -(-b//y))
  a,b = l*x,l*y
print(a+b)

Solution (Python):
def main():
    N = int(input())
    T = 1
    A = 1
    for _ in range(N):
        t, a = map(int, input().split())
        m1 = -(-T//t)
        m2 = -(-A//a)
        T1, T2 = min(m1, m2) * t, max(m1, m2) * t
        A1, A2 = min(m1, m2) * a, max(m1, m2) * a
        if T <= T1 and A <= A1:
            T = T1
            A = A1
        else:
            T = T2
            A = A2
    print(T+A)


if __name__ == "__main__":
    main()

Solution (Python):
import math
N=int(input())
T=[list(map(int,input().split())) for i in range(N)]
cur=[1,1]
for i in T:
    K=max(cur[0]//i[0]+(cur[0]%i[0]!=0),cur[1]//i[1]+(cur[1]%i[1]!=0))
    cur=[j*K for j in i]
print(sum(cur))

Solution (Python):
N = int(input())
ratio = [tuple(map(int, input().split())) for _ in range(N)]
t,a = 1,1

for nt,na in ratio:
    if nt<t or na<a:
        r = max(-(-t//nt), -(-a//na))
        nt *= r
        na *= r
    t,a = nt,na
    
print(t+a)

Solution (Python):
N = int(input())
ratio = [tuple(map(int, input().split())) for _ in range(N)]
t,a = 1,1

for nt,na in ratio:
    if nt<t or na<a:
        r = max((t-1)//nt+1, (a-1)//na+1)
        nt *= r
        na *= r
    t,a = nt,na
    
print(t+a)

Solution (Python):
n = int(input())
ta = [tuple(int(x) for x in input().split()) for _ in range(n)]

a = 0
b = 0
for x, y in ta:
    if x >= a and y >= b:
        a = x
        b = y
    else:
        temp = max(a // x, b // y)
        while x * temp < a or y * temp < b:
            temp += 1
        a = x * temp
        b = y * temp

print(a + b)

Solution (Python):
import math


def main():
    N = int(input())
    TA = [
        list(map(int, input().split()))
        for _ in range(N)
    ]

    
    ans = editorial(N, TA)
    print(ans)


def editorial(N, TA):
    A, B = 1, 1
    for x, y in TA:
        
        n = max(A // x + (A % x != 0),
                B // y + (B % y != 0))
        A, B = n * x, n * y

    ans = A + B
    return ans


if __name__ == '__main__':
    main()

Solution (Python):

N=int(input())
A=[[]*N]
a,b=1,1
for n in range(N):
    A,B=map(int,input().split())
    if a%A!=0:
        ka=a//A+1
    else:
        ka=a//A
    if b%B!=0:
        kb=b//B+1
    else:
        kb=b//B
    k=max(ka,kb)
    a,b=A*k,B*k
print(a+b)

Solution (Python):
from math import ceil
n = int(input())

T=1
A=1

for i in range(n):
    a,b = map(int,input().split())
    num = max(ceil((T)//a)+(T%a!=0),ceil((A)//b)+(A%b!=0))
    T = a*(num)
    A = b*(num)
    
print(T+A)

Solution (Python):
def solve(l: list):
    x, y = 1, 1

    for t, a in l:
        nx = x // t + 1 - (x%t==0)
        ny = y // a + 1 - (y%a==0)
        n = max(nx, ny)
        x = n * t
        y = n * a
    return x + y


def main():
    N = int(input())
    l = []
    for i in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    print(solve(l))


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
l=1000
r=1000
tmp=1
for i in range(n):
    if (a[i][0]>=l and a[i][1]>=r) or i == 0:
        tmp=sum(a[i])
        seki=1
    else:
        if l/a[i][0] > r/a[i][1]:
            seki=-1*(-1*l//a[i][0])
            tmp=sum(a[i])*seki
        else:
            seki=-1*(-1*r//a[i][1])
            tmp=sum(a[i])*seki
    l,r=[a[i][0]*seki,a[i][1]*seki]
print(tmp)

Solution (Python):
n = int(input())
info = [list(map(int,input().split())) for i in range(n)]

cnt_l = info[0][0]
cnt_r = info[0][1]

for i in range(1,n):
  tmpcnt_l = info[i][0]
  tmpcnt_r = info[i][1]
  
  if cnt_l > tmpcnt_l and cnt_l/tmpcnt_l >= cnt_r/tmpcnt_r:
    cnt_l = (cnt_l//tmpcnt_l+int(bool(cnt_l%tmpcnt_l))) * tmpcnt_l
    cnt_r = (cnt_l//tmpcnt_l+int(bool(cnt_l%tmpcnt_l))) * tmpcnt_r
  elif cnt_r > tmpcnt_r and cnt_l/tmpcnt_l <= cnt_r/tmpcnt_r:
    cnt_l = (cnt_r//tmpcnt_r+int(bool(cnt_r%tmpcnt_r))) * tmpcnt_l
    cnt_r = (cnt_r//tmpcnt_r+int(bool(cnt_r%tmpcnt_r))) * tmpcnt_r
  else:
    cnt_l = tmpcnt_l
    cnt_r = tmpcnt_r

print(cnt_l+cnt_r)

Solution (Python):

import sys

def main():
	n = int(input())
	
	ins = []
	for _ in range(n):
		t, a = map(int, input().split(" "))
		ins.append((t, a))
		
	takahashi = 0
	aoki = 0
	takahashi, aoki = ins[0]
	
	for i, val in enumerate(ins):
		if i == 0: continue
		t, a = val
		m = max(-(-takahashi // t), -(-aoki // a))
		takahashi, aoki = t * m, a * m
		
	print(takahashi + aoki)

if __name__ == "__main__":
	main()

Solution (Python):

import sys

def main():
	n = int(input())
	
	ins = []
	for _ in range(n):
		t, a = map(int, input().split(" "))
		ins.append((t, a))
		
	takahashi = 0
	aoki = 0
	takahashi, aoki = ins[0]
	
	for i, val in enumerate(ins):
		if i == 0: continue
		t, a = val
		m = max(-(-takahashi // t), -(-aoki // a))
		takahashi, aoki = t * m, a * m
		
	print(takahashi + aoki)

if __name__ == "__main__":
	main()

Solution (Python):
n = int(input())
A = [[int(i) for i in input().split()] for i in range(n)]

x, y = A[0]
for a, b in A[1:]:
    m = max(-(-x // a), -(-y // b))
    x = a * m
    y = b * m
    
        
print(x + y)

Solution (Python):
n = int(input())
A = [[int(i) for i in input().split()] for i in range(n)]

x, y = 1, 1
for i in range(n):
    a, b = A[i]
    m = max(-(-x // a), -(-y // b))
    x, y = a * m, b * m
    
        
print(x + y)