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



Sample input:
3
2 3
1 1
3 2


Sample output:
10

Submission status: Accepted

Solution code:
from decimal import *

def calc_vote(T, A, TN_prev, AN_prev):
    '''
    input:
        T, A: rate of vote
        TN_prev, AN_prev: previous minimum number of vote
    output:
        TN, AN: minimum number of vote 
    '''
    k = max((TN_prev/T).quantize(Decimal('0'), rounding=ROUND_CEILING), (AN_prev/A).quantize(Decimal('0'), rounding=ROUND_CEILING))
    TN = k * T
    AN = k * A
    
    return TN, AN
  
# input N
N = int(input())

TN, AN = map(Decimal, input().split())
for _ in range(2, N + 1):
    T, A = map(Decimal, input().split())
    TN, AN = calc_vote(T, A, TN, AN)

print(TN + AN)

Submission status: Accepted

Solution code:
n = int(input())

c,d = 0,0
for i in range(n):
    a,b = map(int,input().split())
    if c<=a and d<=b:
        c,d=a,b
        continue
    else:
        f = max(c//a + (c%a>0),d//b+(d%b>0))
        c,d = a*f,b*f
print(c+d)

Submission status: Accepted

Solution code:
N = int(input())
T,A = [],[]
for i in range(N):
    t,a = map(int,input().split())
    T.append(t); A.append(a)
x,y = T[0],A[0]
for i in range(1,N):
    p = x//T[i] if x%T[i]==0 else x//T[i]+1
    q = y//A[i] if y%A[i]==0 else y//A[i]+1
    p = max(p,q)
    x = p*T[i]
    y = p*A[i]
print(x+y)

Submission status: Accepted

Solution code:
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=m*c,m*d

print(a+b)

Submission status: Accepted

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

t = 1
a = 1

for i in range(N):
  k = max((t+ta[i][0]-1)//ta[i][0], (a+ta[i][1]-1)//ta[i][1])
  t = ta[i][0]*k
  a = ta[i][1]*k
  
print(t+a)

Submission status: Accepted

Solution code:
N = int(input())
a = list(int(i) for i in input().split())
x =a[0]
y =a[1]
for j in range(N-1):
    a = list(int(i) for i in input().split())
    if a[0]>x:
        if a[1]>y:
            x =a[0]
            y =a[1]
        else:
            q=(y-1)//a[1]+1
            x =a[0]*q
            y =a[1]*q
    else:
        if a[1]>y:
            p=(x-1)//a[0]+1
            x = a[0]*p
            y = a[1]*p
        else:
            q=(y-1)//a[1]+1
            p=(x-1)//a[0]+1
            if p > q:
                x =a[0]*p
                y =a[1]*p
            else:
                x = a[0]*q
                y = a[1]*q
print(x+y)

Submission status: Accepted

Solution code:
n = int(input())
t,a = map(int,input().split())
for i in range(n-1):
    ti,ai = map(int,input().split())
    if t*ai == a*ti:
        continue
    else:
            b = max(-(-t//ti),-(-a//ai))
            t = ti*b
            a = ai*b
print(t+a)

Submission status: Accepted

Solution code:
n = int(input())
sa,sb=[int(it) for it in input().split()]
for i in range(n-1):
  a,b=[int(it) for it in input().split()]
  r =  max( sa//a + (1 if sa%a!=0 else 0),sb//b+(1 if sb%b!=0 else 0) )
  sa = r*a
  sb = r*b
print (sa+sb)

Submission status: Accepted

Solution code:
import math
N = int(input())
T = [None] * N
A = [None] * N
for i in range(N):
  T[i], A[i] = map(int, input().split())

t, a = T[0], A[0]

for i in range(1, N):
  if (t < T[i] and a < A[i]):
    t, a = T[i], A[i]
  else:
    n = max((t - 1) // T[i] + 1, (a - 1) // A[i] + 1)
    t, a = n * T[i], n * A[i]

print(t + a)

Submission status: Accepted

Solution code:
N = int(input())
T = []
A = []
for i in range(N):
  a, b = map(int, input().split())
  T.append(a)
  A.append(b)

t = T[0]
a = A[0]

for i in range(1, N):
  if t < T[i] and a < A[i]:
    t = T[i]
    a = A[i]
  else:
    x = (t + T[i] - 1) // T[i]
    y = (a + A[i] - 1) // A[i]
    t = T[i] * max(x, y)
    a = A[i] * max(x, y)

print(t + a)

Submission status: Accepted

Solution code:
n=int(input())
t1,a1=map(int,input().split())
for i in range(1,n):
  t,a=map(int,input().split())
  if t1%t==0:
    kt=t1//t
  else:
    kt=t1//t+1
  if a1%a==0:
    ka=a1//a
  else:
    ka=a1//a+1
  k=max(kt,ka)
  t1=k*t
  a1=k*a
print(t1+a1)

Submission status: Accepted

Solution code:
n = int(input())
t = [list(map(int,input().split())) for _ in range(n)]
ans = [1,1]
for t_ in t:
    k = max(ans[0]//t_[0]+min(ans[0]%t_[0],1),ans[1]//t_[1]+min(ans[1]%t_[1],1))
    ans[0] = t_[0]*k
    ans[1] = t_[1]*k
print(sum(ans))

Submission status: Accepted

Solution code:
n=int(input())
p,q=1,1
for i in range(n):
    t,a=map(int,input().split())
    n=max((p-1)//t+1,(q-1)//a+1)
    p=t*n
    q=a*n
print(p+q)

Submission status: Accepted

Solution code:
n=int(input())
a0,b0=1,1
for _ in range(n):
  a,b=map(int,input().split())
  k=max(a0//a+(a0%a>0),b0//b+(b0%b>0))
  a0,b0=a*k,b*k
print(a0+b0)

Submission status: Accepted

Solution code:
N = int(input())
TA = []
AO = []
for i in range(N):
    T, A = map(int, input().split())
    TA += [T]
    AO += [A]

for i in range(N-1):
    D = ((TA[i]-1)//TA[i+1])+1
    E = ((AO[i]-1)//AO[i+1])+1
    F = max(D, E)
    TA[i+1] *= F
    AO[i+1] *= F
ans = int(TA[N-1] + AO[N-1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

for i in range(N):
 A,T = map(int,input().split())
 if(i == 0):
  now_A,now_T = A,T
 else:
  if((now_A<=A)and(now_T<=T)):
    now_A,now_T = A,T
  else: #上のはこっちにまとめられる
    if(now_A%A == 0): tmp1 = now_A//A
    else: tmp1 = now_A//A + 1
    if(now_T%T == 0): tmp2 = now_T//T
    else: tmp2 = now_T//T + 1
    tmp = max(tmp1,tmp2)
    now_A,now_T = tmp*A,tmp*T
ans = now_A + now_T
print(ans)

Submission status: Accepted

Solution code:
import math

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

T, A = 1, 1
for i in range(N):
  if T >= A:
    if TA[i][0] / TA[i][1] >= T / A:
      a = -1 * ((-1 * A) // TA[i][1])
    else:
      a = -1 * ((-1 * T) // TA[i][0])
  else:
    if TA[i][1] / TA[i][0] >= A / T:
      a = -1 * ((-1 * T) // TA[i][0])
    else:
      a = -1 * ((-1 * A) // TA[i][1])
  T = TA[i][0] * a
  A = TA[i][1] * a
  #print("{0} {1}".format(T, A))

print(T + A)

Submission status: Accepted

Solution code:
import sys
import math
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
m = 0
t0, a0 = 1, 1
for i in range(n):
    t, a = nm()
    m = max((a0 + a - 1) // a, (t0 + t - 1) // t)
    a0 = a * m
    t0 = t * m
print(a0 + t0)

Submission status: Accepted

Solution code:
n=int(input())
th,ah=1,1
for i in range(n):
    t,a=map(int,input().split())
    n=max((th+t-1)//t,(ah+a-1)//a)
    th,ah=n*t,n*a
print(th+ah)

Submission status: Accepted

Solution code:
N = int(input())
T, A = 1, 1

for _ in range(N):
    ti, ai = map(int, input().split())
    ratio = max(-(-T // ti), -(-A // ai))
    T = ratio * ti
    A = ratio * ai
print(T + A)

Submission status: Accepted

Solution code:
import math
from decimal import *

n=int(input())

t_prev,a_prev=1,1
for i in range(n):
  t,a=map(int,input().split())
  n_koho1=math.ceil(Decimal(t_prev)/Decimal(t))
  n_koho2=math.ceil(Decimal(a_prev)/Decimal(a))

  #これはダメ
  #n_koho1=math.ceil(t_prev/t)
  #n_koho2=math.ceil(a_prev/a)

  #これはOK
  #n_koho1 = t_prev // t
  #n_koho2 = a_prev // a
  #if t_prev % t != 0:
  #    n_koho1 += 1
  #if a_prev % a != 0:
  #    n_koho2 += 1
        
  n=max(n_koho1,n_koho2)
  t=n*t
  a=n*a
    
  t_prev=t
  a_prev=a

print(t_prev+a_prev)

"""
t=10**15
print(t)
print(t+1)
print((t+1)/t)
print(math.ceil((t+1)/t))

t=10**16
print(t)
print(t+1)
print((t+1)/t)
print(math.ceil((t+1)/t))

print(Decimal((t+1)/t))
print(Decimal(t+1)/Decimal(t))
print(math.ceil(Decimal(t+1)/Decimal(t)))
"""

Submission status: Accepted

Solution code:
import math
from decimal import *

n=int(input())

t_prev,a_prev=1,1
for i in range(n):
  t,a=map(int,input().split())
  n_koho1=math.ceil(Decimal(t_prev)/Decimal(t))
  n_koho2=math.ceil(Decimal(a_prev)/Decimal(a))

  #これはダメ
  #n_koho1=math.ceil(t_prev/t)
  #n_koho2=math.ceil(a_prev/a)

  #これはOK
  #n_koho1 = t_prev // t
  #n_koho2 = a_prev // a
  #if t_prev % t != 0:
  #    n_koho1 += 1
  #if a_prev % a != 0:
  #    n_koho2 += 1
        
  n=max(n_koho1,n_koho2)
  t=n*t
  a=n*a
    
  t_prev=t
  a_prev=a

print(t_prev+a_prev)

Submission status: Accepted

Solution code:
import math

n=int(input())

t_prev,a_prev=1,1
for i in range(n):
  t,a=map(int,input().split())
  #n_koho1=math.ceil(t_prev/t)
  #n_koho2=math.ceil(a_prev/a)

  n_koho1 = t_prev // t
  n_koho2 = a_prev // a
  if t_prev % t != 0:
      n_koho1 += 1
  if a_prev % a != 0:
      n_koho2 += 1
        
  n=max(n_koho1,n_koho2)
  t=n*t
  a=n*a
    
  t_prev=t
  a_prev=a

print(t_prev+a_prev)

Submission status: Accepted

Solution code:
N=int(input())
P=[list(map(int,input().split(' '))) for i in range(N)]
T=[P[i][0] for i in range(N)]
A=[P[i][1] for i in range(N)]
t=T[0]
a=A[0]
k=1
for i in range(1,N):
	k1 = int((T[i-1]*k-1)//T[i]+1)
	k2 = int((A[i-1]*k-1)//A[i]+1)
	k = max(k1,k2)
print(k*(T[-1]+A[-1]))

Submission status: Accepted

Solution code:
import math

N = int(input())
pt = 1
pa = 1
for i in range(N):
    t, a = map(int, input().split())

    t_ratio = pt // t
    a_ratio = pa // a
    if pt % t != 0:
        t_ratio += 1
    if pa % a != 0:
        a_ratio += 1
    min_ratio = max(t_ratio, a_ratio)
    pt = t * min_ratio
    pa = a * min_ratio

print(pt+pa)

Submission status: Accepted

Solution code:
N = int(input())            
T,A = 0,0

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

Submission status: Accepted

Solution code:
import math
from decimal import *

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

total_T = 1
total_A = 1

for t,a in _input:
    #n = max(math.ceil(total_T/t),math.ceil(total_A/a))
    n = max(-(-total_T//t), -(-total_A//a))
    total_T = int(t*n)
    total_A = int(a*n)

print(total_A+total_T)

Submission status: Accepted

Solution code:
N = int(input())
L = []
for i in range(N):
  L.append(list(map(int,input().split())))
A = 1
B = 1
for i in range(N):
  k = max(-(-A//L[i][0]),-(-B//L[i][1]))
  A = L[i][0]*k
  B = L[i][1]*k
print(A+B)

Submission status: Accepted

Solution code:
n = int(input())
ans_t, ans_a = map(int,input().split())
for i in range(n-1):
    t, a = map(int,input().split())

    if ans_t % t == 0:
        p = ans_t//t
    else:
        p = ans_t//t + 1
    if ans_a % a == 0:
        q = ans_a // a
    else:
        q = ans_a// a + 1

    r = max(p, q)
    ans_a = a * r
    ans_t = t * r


print(ans_a + ans_t)

Submission status: Accepted

Solution code:
n = int(input())
t2, a2 = map(int, input().split())
for _ in range(n-1):
  t, a = map(int, input().split())
  m = max((t2-1)//t, (a2-1)//a)+1
  t2 = m*t
  a2 = m*a
print(t2+a2)

Submission status: Accepted

Solution code:
N = int(input())
TA = []
for _ in range(N):
    t, a = map(int, input().split())
    TA.append([t, a])

t0, a0 = 1, 1
for t, a in TA:
    t0 = -(-t0 // t) * t
    a0 = -(-a0 // a) * a
    if t0 / a0 < t / a:
        t0 = t * a0 // a
    else:
        a0 = a * t0 // t

ans = t0 + a0
print(ans)

Submission status: Accepted

Solution code:
def inpl():
    return list(map(int, input().split()))


N = int(input())
TA = [inpl() for i in range(N)]

a, b = TA[0][0], TA[0][1]
for ta, ao in TA[1:]:
    if ta >= a and ao >= b:
        a = ta
        b = ao
    else:
        # elif ta < a and ao < b:
        tmp_a = -(-1 * a // ta)
        tmp_b = -(-1 * b // ao)
        tmp = max(tmp_a, tmp_b)
        a = ta * tmp
        b = ao * tmp
    # elif ta < a and ao >= b:

print(a + b)

Submission status: Accepted

Solution code:
n = int(input())
mt, ma = 1, 1
for t, a in (map(int, input().split()) for _ in range(n)):
    l = max((mt-1)//t, (ma-1)//a)  + 1
    mt, ma = l*t, l*a
print(mt+ma)
#copy code

Submission status: Accepted

Solution code:
N=int(input())
T,A=map(int,input().split())

for i in range(N-1):
    t,a=map(int,input().split())
    if t>=T and a>=A:
        T=t
        A=a
    elif T/t < A/a:
        if A%a==0:
            T=A//a*t
        else:
            A=(A//a+1)*a
            T=A//a*t
    else:
        if T%t==0:
            A=T//t*a
        else:
            T=(T//t+1)*t
            A=T//t*a
print(T+A)

Submission status: Accepted

Solution code:
n=int(input())
a,b=map(int,input().split())
for i in range(n-1):
    e,f=map(int,input().split())
    if (a//e+(1 if a%e!=0 else 0))*f>=b:
        b=f*(a//e+(1 if a%e!=0 else 0))
        a=e*(a//e+(1 if a%e!=0 else 0))
    else:
        a=e*(b//f+(1 if b%f!=0 else 0))
        b=f*(b//f+(1 if b%f!=0 else 0))
print(a+b)

Submission status: Accepted

Solution code:
N = int(input())
v_T = 1
v_A = 1
for _ in range(N):
    t, a = list(map(int, input().split()))
    d = max(((-v_T)//t)*(-1), ((-v_A)//a)*(-1))
    v_T = d*t
    v_A = d*a


print(v_T+v_A)

Submission status: Accepted

Solution code:
def floor(a, b):
    return ((-a)//b)*-1


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

v_T = ratio[0][0]
v_A = ratio[0][1]

for t, a in ratio[1:]:
    diff_t = t - v_T
    diff_a = a - v_A

    d = max(floor(v_T, t), floor(v_A, a))
    v_T = d*t
    v_A = d*a


print(v_T+v_A)

Submission status: Accepted

Solution code:
def floor(a, b):
    return ((-a)//b)*-1


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

v_T = ratio[0][0]
v_A = ratio[0][1]

for t, a in ratio[1:]:
    diff_t = t - v_T
    diff_a = a - v_A

    if diff_t < 0 or diff_a < 0:
        d = max(floor(v_T, t), floor(v_A, a))
        v_T = d*t
        v_A = d*a

    elif diff_t >= 0 and diff_a >= 0:
        m = max(diff_t, diff_a)
        v_T = t
        v_A = a

print(v_T+v_A)

Submission status: Accepted

Solution code:
def floor(a, b):
    return ((-a)//b)*-1


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

v_T = ratio[0][0]
v_A = ratio[0][1]

for t, a in ratio[1:]:
    diff_t = t - v_T
    diff_a = a - v_A

    if diff_t < 0 or diff_a < 0:
        d = max(floor(v_T, t), floor(v_A, a))
        v_T = d*t
        v_A = d*a

    elif diff_t < 0:
        d = floor(v_T, t)
        v_T = d*t
        v_A = d*a

    elif diff_a < 0:
        d = floor(v_A, a)
        v_T = d*t
        v_A = d*a

    elif diff_t >= 0 and diff_a >= 0:
        m = max(diff_t, diff_a)
        v_T = t
        v_A = a

print(v_T+v_A)

Submission status: Accepted

Solution code:
def floor(a, b):
    return ((-a)//b)*-1


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

v_T = ratio[0][0]
v_A = ratio[0][1]

for t, a in ratio[1:]:
    diff_t = t - v_T
    diff_a = a - v_A

    if diff_t < 0 and diff_a < 0:
        d = max(floor(v_T, t), floor(v_A, a))
        v_T = d*t
        v_A = d*a

    elif diff_t < 0:
        d = floor(v_T, t)
        v_T = d*t
        v_A = d*a

    elif diff_a < 0:
        d = floor(v_A, a)
        v_T = d*t
        v_A = d*a

    elif diff_t >= 0 and diff_a >= 0:
        m = max(diff_t, diff_a)
        v_T = t
        v_A = a

print(v_T+v_A)

Submission status: Accepted

Solution code:
# ABC 062
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
def db(x): 
    global debug
    if debug: print(x)
debug = False
N = getInt()
TA = zeros(N)
for i in range(N):
    TA[i] = getIntList()
db(TA)
real = TA[0]
for i in range(1,N):
#    if real[0]>real[1]:
    if real[0]/TA[i][0] > real[1]/TA[i][1]:
        lc = (real[0]+TA[i][0]-1)//TA[i][0]*TA[i][0]
        real[0] = lc 
        real[1] = lc * TA[i][1] // TA[i][0]  
    else:
        lc = (real[1]+TA[i][1]-1)//TA[i][1]*TA[i][1]
        real[1] = lc 
        real[0] = lc * TA[i][0] // TA[i][1]  
    db((lc,real))
print(sum(real))

Submission status: Accepted

Solution code:
# ABC 062
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
def gcd(x, y): # 最大公約数
    m = max(x,y)
    n = min(x,y)
    while m%n != 0:
        w = m%n
        m = n
        n = w
    return n

def lcm(x, y): return x*y//gcd(x,y) # 最小公倍数
def db(x): 
    global debug
    if debug: print(x)
debug = False
N = getInt()
TA = zeros(N)
for i in range(N):
    TA[i] = getIntList()
db(TA)
real = TA[0]
for i in range(1,N):
#    if real[0]>real[1]:
    if real[0]/TA[i][0] > real[1]/TA[i][1]:
        lc = (real[0]+TA[i][0]-1)//TA[i][0]*TA[i][0]
        real[0] = lc 
        real[1] = lc * TA[i][1] // TA[i][0]  
    else:
        lc = (real[1]+TA[i][1]-1)//TA[i][1]*TA[i][1]
        real[1] = lc 
        real[0] = lc * TA[i][0] // TA[i][1]  
    db((lc,real))
print(sum(real))

Submission status: Accepted

Solution code:
N = int(input())
T = 1
A = 1
for i in range(N):
    t, a = (int(i) for i in input().split())
    #n*t <= T かつ n*a <= Aであることが条件
    n = max((T-1+t)//t, (A-1+a)//a)
    T = n * t
    A = n * a
print(T+A)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007

#A
"""
a,b = LS()
c = int(a+b)
for i in range(1,1000):
    if i * i == c:
        print("Yes")
        quit()
print("No")
"""

#B
"""
a,b = LI()
if a*b <= 0:
    print("Zero")
else:
    if b < 0:
        if (a-b) %2 == 1:
            print("Positive")
        else:
            print("Negative")
    else:
        print("Positive")
"""

#C
"""
n = II()
s = SR(n)
march = [[] for i in range(5)]
ch = list("MARCH")
for i in s:
    for j in range(5):
        if i[0] == ch[j]:
            march[j].append(i)
ans = 0
for i in range(5):
    for j in range(i):
        for k in range(j):
            if len(march[i])*len(march[j])*len(march[k]) == 0:
                break
            ans += len(march[i])*len(march[j])*len(march[k])
print(ans)
"""

#D
"""
n = II()
d = LIR(n)
q = II()
p = IIR(q)
d.insert(0,[0 for i in range(n+1)])
for i in range(n):
    d[i+1].insert(0,0)
for i in range(n):
    for j in range(n):
        d[i+1][j+1] += d[i+1][j]+d[i][j+1]-d[i][j]
ans = [0 for i in range(n**2+1)]

for a in range(n+1):
    for b in range(n+1):
        for c in range(a):
            for e in range(b):
                ans[(a-c)*(b-e)] = max(ans[(a-c)*(b-e)],d[a][b]-d[c][b]-d[a][e]+d[c][e])

for i in p:
    an = 0
    for a in range(i+1):
        an = max(an, ans[a])
    print(an)
"""
#E

"""
s = list(S())
ans = -1
s.insert(0,0)
d = 0
for i in range(1,len(s)):
    if s[i] != d:
        d = s[i]
        ans += 1
print(ans)
"""

#F
n = II()
x,y = LI()
for _ in range(n-1):
    a,b = LI()
    if a == b:
        x = max(x,y)
        y = x
    else:
        i = max(x//a,y//b)
        while 1:
            if a*i >= x and b*i >= y:break
            i += 1
        x = a*i
        y = b*i
print(x+y)
#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

Solution code:
#a
"""
import math
a, b = map(str, input().split())
j = int(a + b)
if math.sqrt(j).is_integer():
    print("Yes")
else:
    print("No")
"""

#b
"""
ans = ["Positive","Negative"]
a, b = map(int, input().split())
if 0 < a:
    print("Positive")
elif b >= 0:
    print("Zero")
else:
    print(ans[(b-a)%2-1])
"""

#C
"""
import itertools
n = int(input())
MARCH = ["M", "A", "R", "C", "H"]
march = [0 for _ in range(5)]
for i in range(n):
    s = input()
    if s[0] in MARCH:
        march[MARCH.index(s[0])] += 1
num = list(range(5))
a = list(itertools.combinations(num, 3))
ans = 0
for i in a:
    bf = 1
    for k in i:
        bf *= march[k]
    ans += bf
print(ans)
"""

#D
"""
n = int(input())
D = [list(map(int, input().split())) for _ in range(n)]
q = int(input())
p = [int(input()) for _ in range(q)]
for i in range(n):
    D[i].insert(0, 0)
    D[i].insert(n+1, 0)
D.insert(0,[0 for _ in range(n + 2)])
D.insert(n + 2, [0 for _ in range(n + 2)])
print(D)
"""

#E
"""
s = input()
bf = s[0]
ans = 0
for i in range(1,len(s)):
    if bf != s[i]:
        ans += 1
        bf = s[i]
print(ans)
"""

#F
n = int(input())
ta = [list(map(int, input().split())) for _ in range(n)]
bf = ta[0]
ans = ta[0]
for i in ta:
    if ans == i:
        continue
    elif ans[0] <= i[0] and ans[1] <= i[1]:
        ans = i
    else:
        a = ((ans[0] - 1) // i[0]) + 1
        b = ((ans[1] - 1) // i[1]) + 1
        ab = max(a,b)
        ans = [i[0] * ab, i[1] * ab]

print(ans[0]+ans[1])

Submission status: Accepted

Solution code:
n = int(input())
t,a = [],[]
for i in range(n):
    tmp = [int(i) for i in input().split()]
    t.append(tmp[0])
    a.append(tmp[1])

p,q = 1,1
for i in range(n):
    tmp = max(-(-p//t[i]),-(-q//a[i]))
    p = t[i]*tmp 
    q = a[i]*tmp
print(p+q)

Submission status: Accepted

Solution code:
N = int(input())
T, A = list(map(int, input().split()))
for i in range(N - 1):
	t, a = list(map(int, input().split()))
	if t >= T and a >= A:
		T, A = t, a
	else:
		if T / t >= A / a:
			if T % t == 0:
				T, A = int(t * (T // t)), int(a * (T // t))
			else:
				T, A = int(t * (T // t + 1)), int(a * (T // t + 1))
		else:
			if A % a == 0:
				T, A = int(t * (A // a)), int(a * (A // a))
			else:
				T, A = int(t * (A // a + 1)), int(a * (A // a + 1))

print(T + A)

Submission status: Accepted

Solution code:
N = int(input())

ratio = [list(map(int, input().split(' '))) for i in range(N)]
ans_T = ratio[0][0]
ans_A = ratio[0][1]
for i in range(1, N):
    now_T = ratio[i][0]
    now_A = ratio[i][1]

    count = max((ans_T+now_T-1)//now_T, (ans_A+now_A-1)//now_A)

    ans_T = ratio[i][0] * count
    ans_A = ratio[i][1] * count
print(ans_A + ans_T)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
#a = int(input())
# スペース区切りの整数の入力
#b, c = map(int, input().split())
# 文字列の入力
#s = input()
# 出力
#print("{} {}".format(a+b+c, s))

import math

N = int(input())
for i in range(0, N):
    t, a = map(int, input().split())
    if i == 0:
        tt = t
        aa = a
        ratio = 1
    else:
        if tt % t == 0:
            rt = tt // t
        else:
            rt = tt // t + 1
        if aa % a == 0:
            ra = aa // a
        else:
            ra = aa // a + 1

        ratio = max(rt, ra)

        tt = t * ratio
        aa = a * ratio
    # print("{} {} {} {} {}".format(t, a, tt, aa, ratio))

print("{}".format(tt + aa))

Submission status: Accepted

Solution code:
n = int(input())
x,y = map(int,input().split())

for i in range(n-1):
    t,a = map(int,input().split())
    if x%t == 0:
        x1 = x
        y1 = (x//t)*a
    else:
        x1 = (x//t+1)*t
        y1 = (x//t+1)*a
    
    if y % a == 0:
        x2 = (y//a)*t
        y2 = y
    else:
        x2 = (y//a+1)*t
        y2 = (y//a+1)*a
    
    if y1 < y:
        x1 = y1 = float("inf")
    if x2 < x:
        x2 = y2 = float("inf")
    
    if x1 + y1 >= x2 + y2:
        x = x2
        y = y2
    else:
        x = x1
        y = y1

print(x+y)

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
N = int(input())
t, a = 1, 1
for _ in range(N):
    Ti, Ai = inpl()
    n = max(-(-t//Ti), -(-a//Ai))
    t, a = n*Ti, n*Ai
print(t+a)

Submission status: Accepted

Solution code:
n = int(input())
l ,r = 1, 1
for i in range(n):
    t, a = list(map(int, input().split()))

    k = max(l//t + 1 if l%t else l//t, r//a + 1 if r%a else r//a)
    l = t*k
    r = a*k
print(l+r)

Submission status: Accepted

Solution code:
N=int(input().strip())
a=1
b=1
for i in range(N):
    x, y= map(int, input().split())
    m=max(-(-a//x),-(-b//y))
    a=m*x
    b=m*y
print(a+b)

Submission status: Accepted

Solution code:
#from math import ceil
N=int(input())
table=[]
for i in range(N):
    t,x=map(int,input().split())
    table.append((t,x))
A,B=1,1
def f(A,x):
    t=A//x
    if A%x!=0:
        t+=1
    return t
    
for x,y in table:
    t=max(f(A,x),f(B,y))
    A=t*x
    B=t*y
print(A+B)

Submission status: Accepted

Solution code:
N = int(input())
a = 1
b = 1
for _ in range(N):
    A,B = map(int,input().split())
    x = max(-(-a//A),-(-b//B))
    a = A*x
    b = B*x
print(a+b)

Submission status: Accepted

Solution code:
T,A=1,1
for _ in range(int(input())):
    a,b=map(int,input().split())
    r=max(-(-T//a),-(-A//b))
    T=r*a
    A=r*b
print(T+A)

Submission status: Accepted

Solution code:
T,A=1,1
for _ in range(int(input())):
    a,b=map(int,input().split())
    r=max(-(-T//a),-(-A//b))
    T,A=r*a,r*b
print(T+A)

Submission status: Accepted

Solution code:
T=1
A=1
for _ in range(int(input())):
    a,b=map(int,input().split())
    r=max(-(-T//a),-(-A//b))
    T=r*a
    A=r*b
print(T+A)

Submission status: Accepted

Solution code:
T = 1
A = 1
for _ in range(int(input())):
    a, b = map(int, input().split())
    r = max(-(-T//a), -(-A//b))
    T = r * a
    A = r * b
print(T + A)

Submission status: Accepted

Solution code:
taka, aoki = 1, 1
for _ in range(int(input())):
    a, b = map(int, input().split())
    ratio = max(-(-taka//a), -(-aoki//b))
    taka = ratio * a
    aoki = ratio * b
print(taka + aoki)

Submission status: Accepted

Solution code:
taka, aoki = 1, 1
for _ in range(int(input())):
    a, b = map(int, input().split())
    ratio = max(taka//a + [0, 1][taka%a != 0], aoki//b + [0, 1][aoki%b != 0])
    taka = a * ratio
    aoki = b * ratio
print(taka + aoki)

Submission status: Accepted

Solution code:
from fractions import math


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

    for i in range(N - 1):
        if T[i] / T[i + 1] > 1 or A[i] / A[i + 1] > 1:
            KT = T[i]//T[i + 1] if T[i]%T[i + 1]==0 else (T[i]//T[i + 1])+1
            KA = A[i]//A[i + 1] if A[i]%A[i + 1]==0 else (A[i]//A[i + 1])+1
            mul = max([KT,KA])
            A[i + 1] *= mul
            T[i + 1] *= mul
    print(T[N - 1] + A[N - 1])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

T,A = 0,0

for i in range(N):
    F = 0
    Ti,Ai = map(int,input().split())
    if Ti<T or Ai<A: #両方大
        KT = T//Ti if T%Ti==0 else (T//Ti)+1
        KA = A//Ai if A%Ai==0 else (A//Ai)+1
        K = max([KT,KA])
        T = Ti*K
        A = Ai*K
    else: #両方小
        T = Ti
        A = Ai
        
print(T+A)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
TAs = [tuple(map(int, input().split())) for _ in range(N)]

numT, numA = 1, 1
for T, A in TAs:
    m = max(-(-numT // T), -(-numA // A))
    numT, numA = m * T, m * A

print(numT + numA)

Submission status: Accepted

Solution code:
n = int(input())
x, y = map(int, input().split())
for _ in range(n - 1):
    nx, ny = map(int, input().split())
    mul = max((x + nx - 1) // nx, (y + ny - 1) // ny, 1)
    x = mul * nx
    y = mul * ny
print(x + y)

Submission status: Accepted

Solution code:

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

ans_t = list_ta[0][0]
ans_a = list_ta[0][1]
for i in range(1,n):
    if ans_t % list_ta[i][0] != 0:
        ans_t = list_ta[i][0] * (ans_t // list_ta[i][0] + 1)
    if ans_a % list_ta[i][1] != 0:
        ans_a = list_ta[i][1] * (ans_a // list_ta[i][1] + 1)
    if ans_t * list_ta[i][1] // list_ta[i][0] >= ans_a:
        ans_a = ans_t * list_ta[i][1] // list_ta[i][0]
    else:
        ans_t = ans_a * list_ta[i][0] // list_ta[i][1]

print(ans_a + ans_t)

Submission status: Accepted

Solution code:
N = int(input())
A = 1
B = 1
for i in range(N):
    t, a = map(int, input().split())
    n = max(-(-A // t), -(-B // a))

    A = t * n
    B = a * n
print(A + B)

Submission status: Accepted

Solution code:
ri = lambda: list(map(int,input().split()))

n = int(input())
v = [ri() for _ in range(n)]
for i in range(n-1):
    a,t = v[i]
    aa,tt = v[i+1]
    p = max(-(-a//aa),-(-t//tt))
    if p:
        v[i+1] = [aa*p, tt*p]
print(sum(v[-1]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
n = int(input())
T = 1
A = 1
for _ in range(n):
    line = input().split(" ")
    t = int(line[0])
    a = int(line[1])
    c = max((T+t-1)//t, (A+a-1)//a)
    T = c*t
    A = c*a

print(A+T)

Submission status: Accepted

Solution code:
n = input()

v = [[0, 0] for i in range(n)]
for i in range(n):
	v[i] = map(int, raw_input().split(' '))

a, b = 1, 1

for i in range(n):
	change = v[i][0] * b - a * v[i][1]
	while change != 0:
		if change < 0:
			additions = (-change + v[i][0] - 1) / v[i][0]
			change += additions * v[i][0]
			b += additions
		if change > 0:
			additions = (change + v[i][1] - 1) / v[i][1]
			change -= additions * v[i][1]
			a += additions

print a + b

Submission status: Accepted

Solution code:
n = int(input())
a, b = 1, 1
for i in range(n):
  x, y = list(map(int, input().split()))
  n = max(-(-a//x), -(-b//y))
  a, b = n*x, n*y
print(a+b)

Submission status: Accepted

Solution code:
N = int(input())
ratio = []
for i in range(N):
  ratio.append([int(j) for j in input().split(" ")])
               
vote = ratio[0]

for i in range(len(ratio)-1):
  if ratio[i+1][0] >= vote[0] and ratio[i+1][1] >= vote[1]:
    vote = ratio[i+1]
  
  else:
    a0 = 0
    b0 = 0
    if ratio[i+1][0] < vote[0]:
      seed = ratio[i+1][0]
      mul = vote[0]//ratio[i+1][0]
      if vote[0]%ratio[i+1][0] != 0:
        mul += 1
      seed *= mul
      a0 = seed
      a1 = mul * ratio[i+1][1]
      la = [a0,a1] 
    
    if ratio[i+1][1] < vote[1]:
      seed = ratio[i+1][1]
      base = ratio[i+1][1]
      
      mul = vote[1]//ratio[i+1][1]
      if vote[1]%ratio[i+1][1] != 0:
        mul += 1
      seed *= mul
      b1 = seed
      b0 = mul * ratio[i+1][0]
      lb = [b0,b1]
    if a0 > b0:
      vote = [la[0],la[1]]
    else:
      vote = [lb[0],lb[1]]
print(sum(vote))