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 Decimal,getcontext
from math import ceil
getcontext().prec = 1000
N = int(input())
 
takahashi,aoki = 1,1
 
for _ in range(N):
    a,b = map(int,input().split())
    c = max(ceil(Decimal(str(takahashi))/Decimal(a)),ceil(Decimal(str(aoki))/Decimal(b)))
    takahashi = c*a
    aoki = c*b
 
print(takahashi+aoki)

Submission status: Accepted

Solution code:
from decimal import Decimal,getcontext
from math import ceil

N = int(input())

takahashi,aoki = 1,1

for _ in range(N):
    a,b = map(int,input().split())
    c = max(ceil(takahashi/Decimal(a)),ceil(aoki/Decimal(b)))
    takahashi = c*a
    aoki = c*b

print(takahashi+aoki)

Submission status: Accepted

Solution code:
from decimal import Decimal,getcontext
from math import ceil
getcontext().prec = 1000
N = int(input())

takahashi,aoki = 1,1

for _ in range(N):
    a,b = map(int,input().split())
    c = max(ceil(Decimal(takahashi)/Decimal(a)),ceil(Decimal(aoki)/Decimal(b)))
    takahashi = c*a
    aoki = c*b

print(takahashi+aoki)

Submission status: Accepted

Solution code:
N = int(input())

takahashi,aoki = 1,1

for _ in range(N):
    a,b = map(int,input().split())
    c = max((takahashi-1)//a+1,(aoki-1)//b+1)
    takahashi = c*a
    aoki = c*b

print(takahashi+aoki)

Submission status: Accepted

Solution code:
from sys import stdin
import math
N = int(stdin.readline().rstrip())
t,a = [int(x) for x in stdin.readline().rstrip().split()]
 
for _ in range(N-1):
    T,A = [int(x) for x in stdin.readline().rstrip().split()]
    n = max((t + T - 1) // T,(a + A - 1) // A)
    t = n*T
    a = n*A
    
print(t+a)

Submission status: Accepted

Solution code:
from sys import stdin
import math
N = int(stdin.readline().rstrip())
t,a = 1,1
 
for _ in range(N):
    T,A = [int(x) for x in stdin.readline().rstrip().split()]
    n = max((t + T - 1) // T,(a + A - 1) // A)
    t = n*T
    a = n*A
    
print(t+a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N = int(input())

A, B = 1, 1

for i in range(N):
    XA, XB = map(int, input().split())

    if A * XB > XA * B:
        d = (A + XA - 1) // XA
        A, B = d * XA, d * XB
    else:
        d = (B + XB - 1) // XB
        A, B = d * XA, d * XB

print(A + B)

Submission status: Accepted

Solution code:
N=int(input())

vt=va=1
for _ in range(N):
  T,A=map(int,input().split())
  kt=-(-vt//T)
  ka=-(-va//A)
  
  k=max(kt,ka)
  vt=k*T
  va=k*A  
  #print(vt,va)
  
print(vt+va)

Submission status: Accepted

Solution code:
N=int(input())

vt=va=0
for _ in range(N):
  T,A=map(int,input().split())
  kt=-(-vt//T)
  ka=-(-va//A)
  
  k=max(kt,ka,1)
  vt=k*T
  va=k*A  
  #print(vt,va)
  
print(vt+va)

Submission status: Accepted

Solution code:
import fractions
N=int(input())

vt=va=0
for _ in range(N):
  T,A=map(int,input().split())
  kt=-(-vt//T)
  ka=-(-va//A)
  if kt==0 and ka==0:
    vt=T
    va=A
  else:
    k=max(kt,ka)
    vt=k*T
    va=k*A
  
  #print(vt,va)
  
print(vt+va)

Submission status: Accepted

Solution code:
import math
N = int(input())
ta = [tuple(map(int,input().split())) for _ in range(N)]
left = ta[0][0]
right = ta[0][1]
res = left+right
for t,a in ta[1:]:
    tmp1 = (left+t-1)//t
    tmp2 = (right+a-1)//a
    buff = max(tmp1,tmp2)
    right = buff*a
    left = buff*t
print(int(right+left))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
# import time
import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())

    def calc_minimum_card_pair(a, b, s, t):
        """
        現在 a, b 枚であるとする
        カードを増やす操作を行い比を s : t にするとき、変化後のカード枚数を返す
        """
        # multiply_rate = max(math.ceil(a / s), math.ceil(b / t))
        multiply_rate = max((a + s - 1) // s, (b + t - 1) // t)
        return (s * multiply_rate, t * multiply_rate)
    
    
    n = ii()
    L = [lmi() for _ in range(n)]
    a, b = 1, 1
    for s, t in L:
        a, b = calc_minimum_card_pair(a, b, s, t)
        # print(a, b)
    
    # print('')
    print(a + b)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
ary = list(map(int, input().split()))
for i in range(n - 1):
  tmp_ary = list(map(int, input().split()))
  # 桁が大きいとceil, floorでは正しく判定ができない
  t_rate = (ary[0] - 1) // tmp_ary[0] + 1
  a_rate = (ary[1] - 1) // tmp_ary[1] + 1
  rate = max([t_rate, a_rate])
  ary = list(map(lambda x: x * rate, tmp_ary))
print(int(ary[0] + ary[1]))

Submission status: Accepted

Solution code:
n = int(input())
ary = list(map(int, input().split()))
for i in range(n - 1):
  tmp_ary = list(map(int, input().split()))
  t_rate = (ary[0] - 1) // tmp_ary[0] + 1
  a_rate = (ary[1] - 1) // tmp_ary[1] + 1
  rate = max([t_rate, a_rate])
  ary = list(map(lambda x: x * rate, tmp_ary))
print(int(ary[0] + ary[1]))

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
import sys

def resolve():
    readline=sys.stdin.readline

    n=int(readline())
    a,b=map(int, readline().rstrip().split())
    for _ in range(n-1):
        x,y=map(int, readline().rstrip().split())
        start=max(a//x,b//y)
        for z in range(start,start+1000):
            if x*z>=a and y*z>=b:
                a,b=x*z,y*z
                break
    print(a+b)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #a,b,c=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    n=int(readline())
    for i in range(n):
        x,y=map(int, readline().rstrip().split())
        if i==0:
            a,b=x,y
            continue
        start=max(a//x,b//y)
        zz=start-1
        for z in range(start,start+1000):
            if x*z>=a and y*z>=b:
                zz=z
                break
        a,b=x*z,y*z
    print(a+b)
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

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

def cino(test=False):
    if not test:
        return int(input())
    else:
        return input()
def cina():
  return list(map(int,input().split()))

a = cino()
x,y = 1,1
for _ in range(a):
    p,q = cin()
    k = max((x+p-1)//p,(y+q-1)//q)
    x,y = k*p,k*q
print(x+y)

Submission status: Accepted

Solution code:
def main():
    from math import ceil
    n = int(input())
    a, b = 1, 1
    for _ in range(n):
        i, j = map(int, input().split())
        x = max((a + i - 1) // i, (b + j - 1) // j)
        a, b = x * i, x * j
    print(a + b)


if __name__ == '__main__':
    main()

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):
    T, A = TA[i]
    if t > T or a > A:
        tmul = (t+T-1) // T
        amul = (a+A-1) // A
        mul = max(tmul, amul)
        T *= mul
        A *= mul
    t = T
    a = A
print(t+a)

Submission status: Accepted

Solution code:
n = int(input())
tvote = 1
avote = 1

for i in range(n):
	t, a = [int(n) for n in input().split()]
	k = (tvote-1)//t + 1
	tnow = t * k
	anow = a * k
	if avote > anow:
		k = (avote-1)//a + 1
		tnow = t * k
		anow = a * k
	tvote = tnow
	avote = anow
	
print(avote+tvote)

Submission status: Accepted

Solution code:
n = int(input())
t = [0]*n
a = [0]*n
for i in range(n): t[i],a[i] = map(int,input().split())
tcnt = t[0]
acnt = a[0]
for i in range(1,n):
    x = max((tcnt+t[i]-1)//t[i],(acnt+a[i]-1)//a[i])
    tcnt = t[i]*x
    acnt = a[i]*x
print(tcnt+acnt)

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())
  p = (T-1)//t + 1
  q = (A-1)//a + 1
  n = max(p, q)
  T = n*t
  A = n*a
print(T+A)

Submission status: Accepted

Solution code:
n=int(input())
t_prev,a_prev=map(int,input().split())
for i in range(n-1):
  t,a=map(int,input().split())
  k=max(t_prev//t+min(1,t_prev%t),a_prev//a+min(1,a_prev%a))
  t_prev,a_prev=t*k,a*k
    
print(t_prev+a_prev)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
t_lst, a_lst = [], []
for i in range(n):
    ti, ai = map(int, input().split())
    t_lst.append(ti)
    a_lst.append(ai)

t, a = 1, 1
for i in range(n):
    ti, ai = t_lst[i], a_lst[i]
    n = max((t+ti-1)//ti, (a+ai-1)//ai)
    t = n*ti
    a = n*ai
print(t+a)

Submission status: Accepted

Solution code:
n = int(input())
t, a = map(int, input().split())
for i in range(n - 1):
    r1, r2 = map(int, input().split())
    if r1 >= t and r2 >= a:
        multiple = 1
    elif r1 < t and r2 >= a:
        multiple = (t-1) // r1 + 1
    elif r1 >= t and r2 < a:
        multiple = (a-1) // r2 + 1
    else:
        multiple = max((t-1) // r1, (a-1) // r2) + 1
    t, a = r1 * multiple, r2 * multiple
print(t+a)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
def ceil(a, b):
    return -(-a//b)

def main():
    N = int(readline())
    inp = [tuple(map(int, readline().rstrip().split())) for _ in range(N)]
    scr_t, scr_a = 0, 0
    for t, a in inp:
        if t >= scr_t and a >= scr_a:
            scr_t = t
            scr_a = a
        elif t < scr_t and a >= scr_a:
            scr_t = t * ceil(scr_t, t)
            scr_a = scr_t * a // t
        elif a < scr_a and t >= scr_t:
            scr_a = a * ceil(scr_a, a)
            scr_t = scr_a * t // a
        else:
            if t / a >= scr_t / scr_a:
                scr_a = a * ceil(scr_a, a)
                scr_t = scr_a * t // a
            else:
                scr_t = t * ceil(scr_t, t)
                scr_a = scr_t * a // t

    print(scr_t + scr_a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

def ceil(a, b):
    return -(-a//b)

def main():
    N = int(readline())
    inp = [tuple(map(int, readline().rstrip().split())) for _ in range(N)]
    scr_t, scr_a = 1, 1
    for t, a in inp:
        n = max(ceil(scr_t, t), ceil(scr_a, a))
        scr_t = n * t
        scr_a = n * a

    print(scr_t + scr_a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
from math import floor, ceil

def main():
    N = int(readline())
    inp = [tuple(map(int, readline().rstrip().split())) for _ in range(N)]
    scr_t, scr_a = 1, 1
    for t, a in inp:
        # n = max(ceil(scr_t/t), ceil(scr_a/a))
        n = max((scr_t//t)+(scr_t%t!=0), (scr_a//a)+(scr_a%a!=0))
        scr_t = n * t
        scr_a = n * a

    print(scr_t + scr_a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
x, y = 1, 1

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

    l = -(-x // t)
    r = -(-y // a)
    if l > r:
        x = t * l
        y = a * l
    else:
        x = t * r
        y = a * r

    #print(x, y)

print(x + y)

Submission status: Accepted

Solution code:
n = int(input())
mt = 0
ma = 0
for i in range(n):
    t , a = map(int, input().split())
    if t>=mt and a>=ma:
        mt = t
        ma = a
    elif t<mt or a<ma:
        k1 = mt//t
        k2 = ma//a
        k=max(k1,k2)
        while t*k < mt or a*k <ma:
            k+=1
        mt=t*k
        ma=a*k
print(mt+ma)

Submission status: Accepted

Solution code:
import math
def resolve():
    N = int(input())
    TA = [list(map(int, input().split())) for i in range(N)]
    t, a = TA[0]
    for i in range(1, len(TA)):
        t_next, a_next = TA[i]
        div = max((t//t_next)+(t%t_next!=0), (a//a_next)+(a%a_next!=0))
        t = t_next*div
        a = a_next*div
    print(t+a)


if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:

def resolve():
	n = int(input())
	v = []
	for _ in range(n):
		v.append(list(map(int, input().split())))
	t, a = 1, 1
	for i in range(n):
		tv, av = v[i]
		c = max(t//tv + (t%tv!=0), a//av + (a%av!=0))
		t, a = c*tv, c*av
	print(t+a)
resolve()

Submission status: Accepted

Solution code:
n=int(input())
ta=[list(map(int,input().split())) for _ in range(n)]
ans=ta[0]
for i in range(1,n):
  if (ta[i-1][0]<=ta[i][0]) and (ta[i-1][1]<=ta[i][1]):
    pass
  else:
    l=ta[i-1][0]//ta[i][0]
    if ta[i-1][0]%ta[i][0]!=0:
      l+=1
    r=ta[i-1][1]//ta[i][1]
    if ta[i-1][1]%ta[i][1]!=0:
      r+=1
    ta[i][0]*=max(l,r)
    ta[i][1]*=max(l,r)
print(sum(ta[-1]))

Submission status: Accepted

Solution code:
n=int(input())
t,a=[0]*n,[0]*n
for i in range(n):
  t[i],a[i]=map(int,input().split())
tt,aa=t[0],a[0]
for i in range(1,n):
  tt=t[i]*max((tt+t[i]-1)//t[i],(aa+a[i]-1)//a[i])
  aa=a[i]*max((tt+t[i]-1)//t[i],(aa+a[i]-1)//a[i])
print(aa+tt)

Submission status: Accepted

Solution code:
ma = lambda : map(int,input().split())
ni = lambda :int(input())

n = ni()
pt,pa = ma()
for i in range(n-1):
    t,a = ma()
    l = max((pt+t-1)//t,(pa+a-1)//a)
    pt = t*l
    pa = a*l
print(pt+pa)

Submission status: Accepted

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

for _ in range(N-1):
  T, S = map(int, input().split())
  x = max((A+T-1)//T, (B+S-1)//S)
  A = x*T
  B = x*S

print(A+B)

Submission status: Accepted

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

for i in range(1,N):
	Cnt_pre = Cnt[i-1]
	if T[i]<T[i-1]*Cnt_pre:
		if (T[i-1]*Cnt_pre)%T[i]==0:
			Cnt[i] = (T[i-1]*Cnt_pre)//T[i]
		else:Cnt[i]= (T[i-1]*Cnt_pre)//T[i] + 1
	if A[i]*Cnt[i]<A[i-1]*Cnt_pre:
		if (A[i-1]*Cnt_pre)%A[i]==0:
			Cnt[i]=(A[i-1]*Cnt_pre)//A[i]
		else:Cnt[i]=(A[i-1]*Cnt_pre)//A[i]+1
print(Cnt[N-1]*(T[N-1]+A[N-1]))

Submission status: Accepted

Solution code:

n = int(input())
t, a = [0] * n, [0] * n
for i in range(n):
    t[i], a[i] = map(int, input().split())
ans = a[0] + t[0]
j_pre = 1
for i in range(1, n):
    j = max(-(-(t[i - 1] * j_pre) // t[i]), -(-(a[i - 1] * j_pre) // a[i]) )
    ans = (t[i] + a[i]) * j
    j_pre = j
    #print(t[i] * j, a[i] * j)
    #print(ans)

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    TA = [[int(_) for _ in input().split()] for __ in range(N)]
    x, y = TA[0] #初期値
    for i in range(1, N):
        T, A = TA[i]
        if x%T != 0:
            x += T - x%T
        if A * x // T < y:
            if y%A != 0:
                y += A - y%A
            x = T * y // A
        else:
            y = A * x // T
    print(x+y)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
t, a = map(int, input().split())
for i in range(1, n):
    x, y = map(int, input().split())
    i = max(t // x, a // y)
    c, d = x, y
    while c < t or d< a:
        c = x * i
        d = y * i
        i += 1
    t, a = c, d
print(t + a)

Submission status: Accepted

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

vt = t[0]
va = a[0]

for i in range(n):
    if (vt * a[i] == va * t[i]):
        continue
    
    if(vt * a[i] < va * t[i]):
        x = (va + a[i] - 1)//a[i]
    elif (vt * a[i] > va * t[i]):
        x = (vt + t[i] - 1)//t[i]
    vt = x * t[i]
    va = x * a[i]

ans = vt + va
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
tv=1
av=1
for i in range(n):
    ti,ai=map(int,input().split())
    m=max(int((tv-1)//ti)+1,int((av-1)//ai)+1)
    tv=m*ti
    av=m*ai
print(tv+av)

Submission status: Accepted

Solution code:
n = int(input())

t_vote = 1
a_vote = 1

for _ in range(n):
    t,a = map(int,input().split())
    if t_vote*a > a_vote*t:
        t_vote = (t_vote + t-1)//t*t
        a_vote = t_vote//t*a
    else:
        a_vote = (a_vote + a-1)//a*a
        t_vote = a_vote//a*t
print(t_vote+a_vote)

Submission status: Accepted

Solution code:
N = int(input())

t, a = map(int, input().split())

for i in range(N - 1):
    nt, na = map(int, input().split())
    p = max((t + nt - 1) // nt, (a + na - 1) // na)
    t = nt * p
    a = na * p
print(t + a)

Submission status: Accepted

Solution code:
N = int(input())

t, a = map(int, input().split())

for i in range(N - 1):
    nt, na = map(int, input().split())
    p = max(t // nt, a // na)
    pt = nt * p
    pa = na * p
    while pt < t or pa < a:
        pt += nt
        pa += na
    t = pt
    a = pa
print(t + a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A,B = map(int,input().split())
for _ in range(n-1):
    a,b = map(int,input().split())
    aa = 0
    bb = 0
    if A % a == 0:
        aa = A // a
    else:
        aa = A // a + 1
    if B % b == 0:
        bb = B // b
    else:
        bb = B // b + 1
    m = max(aa, bb)
    A,B = a * m, b * m
print(A+B)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""
n = onem()

a,b = 1,1

for i in range(n):
    x,y = m()
    po = max(-(-a//x),-(-b//y))
    a = po*x
    b = po*y

print(a+b)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    N = int(input())
    T, A = 1, 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
import itertools
#from scipy.misc import comb

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

A, B = 1, 1
for x,y in L:
    n = max(0-(-A//x), 0-(-B//y))
    A, B = n*x, n*y

print(A+B)

Submission status: Accepted

Solution code:
n=int(input())
A=[1,1]
for i in range(n):
  t,a=map(int,input().split())
  tt=(A[0]+t-1)//t
  aa=(A[1]+a-1)//a
  A[0]=max(tt,aa)*t
  A[1]=max(tt,aa)*a
print(A[0]+A[1])

Submission status: Accepted

Solution code:
import io
import math

data = int(input())
nim,mike = 1,1
for _ in range(data):
  n,m = [int(_) for _ in input().split(" ")]
  l = max(-(-nim//n), -(-mike//m))
  nim,mike = l*n,l*m
print(nim+mike)

Submission status: Accepted

Solution code:
n = int(input())

votes = [0, 0]

for i in range(n):
    a, b = map(int, input().split())
    if i == 0:
        votes = [a, b]
    else:
        num = max(-(-votes[0]//a), -(-votes[1]//b))
        x = a * num
        y = b * num
        votes = [x, y]
print(sum(votes))

Submission status: Accepted

Solution code:
N = int(input())
TA = [[int(i) for i in input().split()] for _ in range(N)]
ans = [i for i in TA[0]]
for ta in TA[1:]:
    t, a = ta
    if t == a == 1:
        m = max(ans)
        ans = [m, m]
        continue
    si = 0
    sn = t
    ln = a
    if t > a:
        si = 1
        sn = a
        ln = t
    if ans[si] % sn:
        ans[si] = ans[si] // sn * sn + sn
    n = ans[si] // sn * ln
    if ans[si ^ 1] <= n:
        ans[si ^ 1] = n
    else:
        if ans[si ^ 1] % ln:
            ans[si ^ 1] = ans[si ^ 1] // ln * ln + ln
        ans[si] = ans[si ^ 1] // ln * sn
print(sum(ans))

Submission status: Accepted

Solution code:
N=int(input())
table=[[int(i) for i in input().split()] for j in range(0,N)]

for i in range(0,N-1):
    a=table[i][0]//table[i+1][0]
    A=table[i][0]%table[i+1][0]
    b=table[i][1]//table[i+1][1]
    B=table[i][1]%table[i+1][1]
    if a>b and A==0:
        table[i+1][0]=a*table[i+1][0]
        table[i+1][1]=a*table[i+1][1]
    elif a>b and not A==0:
        table[i+1][0]=(a+1)*table[i+1][0]
        table[i+1][1]=(a+1)*table[i+1][1]
    elif a<b and B==0:
        table[i+1][0]=b*table[i+1][0]
        table[i+1][1]=b*table[i+1][1]
    elif a<b and not B==0:
        table[i+1][0]=(b+1)*table[i+1][0]
        table[i+1][1]=(b+1)*table[i+1][1]
    elif a==b and A==0 and B==0:
        table[i+1][0]=a*table[i+1][0]
        table[i+1][1]=a*table[i+1][1]
    elif a==b and not A==0 and B==0:
        table[i+1][0]=(a+1)*table[i+1][0]
        table[i+1][1]=(a+1)*table[i+1][1]
    elif a==b and not B==0 and A==0:
        table[i+1][0]=(a+1)*table[i+1][0]
        table[i+1][1]=(a+1)*table[i+1][1]
    elif a==b and not B==0 and not A==0:
        table[i+1][0]=(a+1)*table[i+1][0]
        table[i+1][1]=(a+1)*table[i+1][1]

print(table[N-1][0]+table[N-1][1])

Submission status: Accepted

Solution code:
n = int(input())

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

a = tlist[0][0]
b = tlist[0][1]

for t in tlist:
    multi = max((a//t[0]) + (a%t[0] > 0), (b//t[1]) + (b%t[1] > 0))
    # print('multi', multi)
    # print('a b', a, b)
    a = t[0]*multi
    b = t[1]*multi

print(a + b)

Submission status: Accepted

Solution code:
import math

def solve(ratio_list):
  a = 0
  b = 0
  for p, q in ratio_list:
    if a == 0 and b == 0:
      a = p
      b = q
    else:
      fact1 = -(-a//p)
      fact2 = -(-b//q)
      factor = int(max(fact1, fact2))
      a = factor*p
      b = factor*q
  return a,b


if __name__ == "__main__":
  n = int(input())
  ratio = [tuple(map(int, input().split())) for _ in range(n)]
  a,b = solve(ratio)
  print(a+b)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    at = [list(map(int, input().split())) for _ in range(n)]
    x, y = at[0]
    for i in range(1, n):
        nx, ny = at[i]
        if nx >= x and ny >= y:
            x = nx
            y = ny
        else:
            a = max((x+nx-1)//nx, (y+ny-1)//ny)
            x = nx * a
            y = ny * a
    print(x+y)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np

n = int(sys.stdin.readline().rstrip())
res = np.array(sys.stdin.read().split(), dtype=np.int64).reshape(n, 2)

def main():
  for i in range(n - 1):
    res[i+1] *= ((res[i] + res[i+1] - 1) // res[i+1]).max()
  print(res[n-1].sum())
  
if __name__ ==  '__main__':
  main()