Harryxun/stf_run huggingface dataset:
import math
 
PI = math.pi
A=int(input())
B=int(input())
H=int(input())
M=int(input())
 
arg_a = (H + M/60) / 12.0 * 360
arg_b = M / 60.0 * 360
 
 
C = (A*A + B*B - 2*A*B* math.cos((arg_a-arg_b)/360*2*PI)) ** (1/2)
print(C)

### FIXED CODE ###

import math
 
PI = math.pi
A, B, H, M = map(int, input().split())
 
arg_a = (float(H) + float(M)/60) / 12.0 * 360
arg_b = float(M) / 60.0 * 360
 
 
C = math.sqrt(A*A + B*B - 2*A*B* math.cos((arg_a-arg_b)/360*2*PI))
print(C)
n, m = map(int, input().split())

adj = [[] for i in range(n)]

for i in range(m):
    s, t = map(int, input().split())
    adj[s].append(t)
    adj[t].append(s)

isVisited = [False] * n

CC = [None] * n

import sys
sys.setrecursionlimit(10000000000000)

def dfs(u, group):
    isVisited[u] = True
    CC[u] = group
    for v in adj[u]:
        if not isVisited[v]:
            dfs(v, group)

def makeCC(group):
    for i in range(n):
        if not isVisited[i]:
            dfs(i, group)
            group += 1

def isConnected(v1, v2):
    if CC[v1] == CC[v2]:
        print('yes')
    else:
        print('no')


makeCC(0)


q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    isConnected(s, t)

### FIXED CODE ###

n, m = map(int, input().split())

adj = [[] for i in range(n)]

for i in range(m):
    s, t = map(int, input().split())
    adj[s].append(t)
    adj[t].append(s)

groups = [None] * n


def dfs(u, group_num):
    path = []
    path.append(u)
    groups[u] = group_num
    while path:
        u = path[-1]
        if adj[u]:
            v = adj[u].pop(0)
            if not groups[v]:
                groups[v] = group_num
                path.append(v)
        else:
            path.pop()


g_n = 1
for i in range(n):
    if not groups[i]:
        dfs(i, g_n)
        g_n += 1

def isConnected(v1, v2):
    if groups[v1] == groups[v2]:
        print('yes')
    else:
        print('no')


q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    isConnected(s, t)
N, M = map(int, input().split())
A = []
B = []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
C = sorted(A)
while M > 0:
    

### FIXED CODE ###

def resolve():
    N, M = map(int, input().split())
    A = []
    for _ in range(N):
        a = list(map(int, input().split()))
        A.append(a)
    ans = 0
    for i, j in sorted(A, key=lambda x: x[0]):
        if j >= M:
            ans += M * i
            print(ans)
            exit()
        ans += i * j
        M -= j
resolve()
(n, x) = map(int, input().split())
l = list(map(int, input().split()))

count = 0
for i in range(n):
    if count > x:
        ans = i
        break
    else:
        count += l[i]
print(ans)

### FIXED CODE ###

n, x = map(int, input().split())
l = list(map(int, input().split()))

d = 0
ans = 0
for i in l:
    if d > x:
        break
    else:
        d += i
        ans += 1

if x >= sum(l):
    ans += 1

print(ans)
n=int(input())
a=list(map(int,input().split()))
a.sort()
q=int(input())
for i in range(q):
    bi,ci=map(int,input().split())
    t=0
    for j in range(n):
        if a[j]==bi:
            a[j]=ci
            d=j+1
            while a[d]==bi:
                a[d]=ci
                d+=1
            t=1
        elif t==1:
            break
    print(sum(a))

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
b=[0]
b*=10**5+1
for i in a:
    b[i]+=1
ans=sum(a)
q=int(input())
for i in range(q):
    bi,ci=map(int,input().split())
    d=b[bi]
    b[ci]+=d
    b[bi]=0
    ans=ans+d*(ci-bi)
    print(ans)
from queue import Queue
import itertools
import numpy as np
 
R, C = map(int, input().split())
sy, sx = 1, 1
gy, gx = R, C
 
field = [["#"] * (C + 2)] + \
        [list("#" + input() + "#") for i in range(R)] + \
        [["#"] * (C + 2)]
dxy = [(0,1), (0,-1), (1,0), (-1,0)]
flg = False
cnt = 0
 
field_1d = list(itertools.chain.from_iterable(field))
field01 = [1 if i == '.' else 0 for i in field_1d]
dot_num = np.array(field01).sum()
 
q = Queue()
q.put([sy, sx])
field[sy][sx] = 0
 
while flg == False:
    yx = q.get()
    cnt = field[yx[0]][yx[1]]
    for dy, dx in dxy:
        y_n, x_n = yx[0] + dy, yx[1] + dx
        if y_n == gy and x_n == gx:
            field[y_n][x_n] = cnt + 1
            flg = True
            print(dot_num - (cnt+2))
        if field[y_n][x_n] == '#':
            continue
        if field[y_n][x_n] == '.':
            q.put([y_n, x_n])
            field[y_n][x_n] = cnt + 1
if flg == False:
    print(-1)

### FIXED CODE ###

n = int(input())
a = int(input())
if n % 500 <= a:
    print('Yes')
else:
    print('No')
n=int(input())
l=list(map(int,input().split()))

ans=[]
left=0
right=0

for i in range(n):
  left=sum(l[:i+1])
  right=sum(l[i+1:])
  ans+=abs(left-right)
  
print(min(ans))

### FIXED CODE ###

n=int(input())
l=list(map(int,input().split()))

ans=[]
left=0
right=0

for i in range(n):
  left=sum(l[:i+1])
  right=sum(l[i+1:])
  ans.append(abs(left-right))
  
print(min(ans))
A,B=map(int,input().split())

if 1<=A,B<=9:
  print(A*B)
else:
  print(-1)

### FIXED CODE ###

A,B=map(int,input().split())

if 1<=A<=9 and 1<=B<=9:
  print(A*B)
else:
  print(-1)
N, X = map(int, input().split())

result = 'P'

for i in range(1, N+1):
    result = 'B' + result + 'P' + result + 'B'
    
print(result[:X].count('P'))

### FIXED CODE ###

N, X = map(int, input().split())

a, p = [1], [1]
for i in range(N):
    a.append(a[i] * 2 + 3)
    p.append(p[i] * 2 + 1)

def f(N, X): # X <= 0 や X > a_N を許容し解説本文から簡略化
    if N == 0:
        return 0 if X <= 0 else 1
    elif X <= 1 + a[N-1]:
        return f(N-1, X-1)
    else:
        return p[N-1] + 1 + f(N-1, X-2-a[N-1])

print(f(N, X))
N, L = map(int, input().split())
S = [input().rstrip() for i in range(L)]

print(sorted(S))

### FIXED CODE ###

N, L = map(int, input().split())
S = [input() for i in range(N)]
s = sorted(S)
print(*s, sep='')
str = input()
p = ""
resut = "Good"
for s in str:
  if p == s:
    result = "Bat"
  p = s
print(result)

### FIXED CODE ###

strings = list(input())
before_number = 100000
flag = False
for number in strings:
  #前の番号と今の番号が一致したら
  if before_number == int(number):
    flag = True
  else:
    before_number = int(number)

if flag == True:
  print("Bad")
else:
  print("Good")
import heapq
import sys
sys.setrecursionlimit(10**6)

N=int(input())

xylist,yxlist=[],[]
par_list={}
for i in range(N):
  x,y=map(int,input().split())
  xylist.append((x,y))
  yxlist.append((y,x))
  par_list[(x,y)]=(x,y)
xylist.sort()
yxlist.sort()
#print(xylist)

hq=[]
for i in range(1,N):
  x1,y1=xylist[i-1]
  x2,y2=xylist[i]
  heapq.heappush(hq,(min(abs(x2-x1),abs(y1-y2)),(x1,y1),(x2,y2)))
for i in range(1,N):
  y1,x1=yxlist[i-1]
  y2,x2=yxlist[i]
  heapq.heappush(hq,(min(abs(x2-x1),abs(y1-y2)),(x1,y1),(x2,y2)))
#print(hq)

def get_par(x):
  if x == par_list[x]:
    return x
  else:
    par_list[x] = get_par(par_list[x])
    return par_list[x]
def merge(x,y):
  par_x = get_par(x)
  par_y = get_par(y)
  if par_x != par_y:
    par_list[par_y] = par_x
def is_same(x,y):
  return get_par(x) == get_par(y)

answer=0
cnt=0
while cnt<N-1:
  d,p1,p2=heapq.heappop(hq)
  #print(d,p1,p2)
  if not is_same(p1,p2):
    #print(p1,p2)
    merge(p1,p2)
    answer+=d
    cnt+=1
  
print(answer)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)
input=sys.stdin.readline

N=int(input())

xylist,yxlist=[],[]
par_dic={}
for i in range(N):
  x,y=map(int,input().split())
  xylist.append((x,y))
  yxlist.append((y,x))
  par_dic[(x,y)]=(x,y)
xylist.sort()
yxlist.sort()
#print(xylist)

elist=[]
for i in range(1,N):
  x1,y1=xylist[i-1]
  x2,y2=xylist[i]
  elist.append((min(abs(x2-x1),abs(y1-y2)),(x1,y1),(x2,y2)))
  y1,x1=yxlist[i-1]
  y2,x2=yxlist[i]
  elist.append((min(abs(x2-x1),abs(y1-y2)),(x1,y1),(x2,y2)))
#print(hq)
elist.sort(reverse=True)

def get_par(x):
  if x == par_dic[x]:
    return x
  else:
    par_dic[x] = get_par(par_dic[x])
    return par_dic[x]
def merge(x,y):
  par_x = get_par(x)
  par_y = get_par(y)
  if par_x != par_y:
    par_dic[par_y] = par_x
def is_same(x,y):
  return get_par(x) == get_par(y)

answer=0
while elist:
  d,p1,p2=elist.pop()
  #print(d,p1,p2)
  if not is_same(p1,p2):
    merge(p1,p2)
    answer+=d
  
print(answer)
bingonum=[[0,0,0],[0,0,0],[0,0,0]]
bingo=[["False","False","False"],["False","False","False"],["False","False","False"]]
for i in range(3):
    bingonum[i][0],bingonum[i][1],bingonum[i][2] = map(int,input().split())

N = int(input())
b=[]
for _ in range(N):
    b.append(int(input())
for x in range(N):
    for i in range(3):
        for j in range(3):
            if bingonum[i][j]==b[x]:
                bingo[i][j]="True"

IsBingo = "No"
for i in range(3):
    if bingo[i][0] =="True" and bingo[i][1] =="True" and bingo[i][2] =="True":
        IsBingo="Yes"
    if bingo[0][i] =="True" and bingo[1][i] =="True" and bingo[2][i] =="True":
        IsBingo="Yes"
if (bingo[0][0] =="True" and bingo[1][1] =="True" and bingo[2][2] =="True") or (bingo[0][2] =="True" and bingo[1][1] =="True" and bingo[2][0]=="True"):
    IsBingo="Yes"
print(IsBingo)

### FIXED CODE ###

#Biginner Contest 157 B-Bingo
#二次元配列を使う(3*3)
bingonum=[[0,0,0],[0,0,0],[0,0,0]]
bingo=[["False","False","False"],["False","False","False"],["False","False","False"]]
for i in range(3):
    bingonum[i][0],bingonum[i][1],bingonum[i][2] = map(int,input().split())

N = int(input())
b=[]
for _ in range(N):
    b.append(int(input()))

for x in range(N):
    for i in range(3):
        for j in range(3):
            if bingonum[i][j]==b[x]:
                bingo[i][j]="True"

IsBingo = "No"
for i in range(3):
    if bingo[i][0] =="True" and bingo[i][1] =="True" and bingo[i][2] =="True":
        IsBingo="Yes"
    if bingo[0][i] =="True" and bingo[1][i] =="True" and bingo[2][i] =="True":
        IsBingo="Yes"
if (bingo[0][0] =="True" and bingo[1][1] =="True" and bingo[2][2] =="True") or (bingo[0][2] =="True" and bingo[1][1] =="True" and bingo[2][0]=="True"):
    IsBingo="Yes"
print(IsBingo)
n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = map(int, input().split())
S = {}

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    return res

for j in range(q):
    if solve(0, mi):
        print("yes")
    else:
        print("no")

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = map(int, input().split())
t = {}

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    if (i,m) in t:
        return t[(i,m)]
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    t[(i,m)] = res
    return res

for m in mi:
    if solve(0, m):
        print("yes")
    else:
        print("no")
S = input()
S.sort()
if (S[0] == S[1]) and (S[2] == S[3]) and (S[1] != S[2]):
  print("Yes")
else
  print("No")


### FIXED CODE ###

S = input()
S = sorted(S)
if (S[0] == S[1]) and (S[2] == S[3]) and (S[1] != S[2]):
  print("Yes")
else:
  print("No")
s=list(input())
n=int(input())
for i in range(n):
	l=input().split()
	if l[0]=="replace":
		a=int(l[1])
		b=int(l[2])
		s[a:b+1]=list(l[3])
	elif l[0]=="reverse":
		a=int(l[1])
		b=int(l[2])
		c=s[a:b+1][::-1]
		s[a:b+1]=c
	else:
		a=int(l[1])
		b=int(l[2])
		print("".join(s[a:b+1]))

### FIXED CODE ###

n=int(input())
A=0
B=0
for i in range(n):
	a,b=input().split()
	if a>b:
		A+=3
	elif a==b:
		A+=1
		B+=1
	else:
		B+=3
print(A,B)
N = int(input())
P = list(map(int,input().split(' ')))
num = 0
for i in range(1,N-1):
	if min(P[i-1],P[i+1]) < P[i]  and P[i] <max(P[i-1],P[i+1]):
    	num += 1
print(num)


### FIXED CODE ###

N = int(input())
P = list(map(int,input().split(' ')))
num = 0
for i in range(1,N-1):
  if min(P[i-1],P[i+1]) < P[i]  and P[i] <max(P[i-1],P[i+1]):
    num += 1
print(num)
n, x, m = map(int, input().split())

lst, num, frag = set(), [], False
for i in range(1, n + 1):
    lst.add(x), num.append(x)
    x = x ** 2 % m
    if x in lst:
        flag = True
        break

ans = sum(num)
if flag:
    cnt, idx = i, num.index(x)
    div, mod = divmod(n - cnt, len(num) - idx)
print(ans)


### FIXED CODE ###

n, x, m = map(int, input().split())

lst = set()
num = []
for i in range(1, n + 1):
    lst.add(x), num.append(x)
    x = x ** 2 % m
    if x in lst:
        cnt, idx = i, num.index(x)
        div, mod = divmod(n - cnt, len(num) - idx)
        ans = sum(num[:idx + mod])
        ans += sum(num[idx:]) * (div + 1)
        break
else:
    ans = sum(num)
print(ans)
import sys

values = [int(input()) for i in range(int(input()))]
maxv = - sys.maxsize - 1
minv = values[0]

for val in enumerate(values):
    minv = min(minv, val)
print(maxv)



### FIXED CODE ###

values = [int(input()) for _ in range(int(input()))]
maxv = values[1] - values[0]
minv = values[0]

for val in values[1:]:
    maxv = max(maxv, val - minv)
    minv = min(minv, val)         # 今までの最小値を保持(自分と自分を比較しない様に、最大値の評価後に計算)
print(maxv)
inf=(10**7)+1

n,k=get_ints()
h=get_array()
li=[inf]*n
li[0]=0
 
for i in range(1,n):
    
    for j in range(i-1,i-k-1,-1):
      if j<0:
        break
 
 
 
 
      li[i]=min(li[i],li[j]+abs(h[i]-h[j]))
##print(li)
print(li[n-1])

### FIXED CODE ###


n=int(input())
h=list(map(int,input().split()))
li=[0]*n
li[1]=abs(h[1]-h[0])
for i in range(2,n):
    li[i]=min(li[i-1]+abs(h[i]-h[i-1]),li[i-2]+abs(h[i]-h[i-2]))
##print(li)
print(li[n-1])
from fractions import gcd
n, m = map(int, input().split())
a = list(map(int, input().split()))

def lcm(a, b):
    return int(a*b / gcd(a, b))
"""
akが偶数だから、bk=2akとして、何かの奇数で共通可できるようにすればよい。
Xが存在するには、奇数部分はbkの最小公倍数と奇数を掛けたもの
"""
lcm_all = lcm(a[0]//2, a[1]//2)
for i in range(1, n):
    lcm_all = lcm(lcm_all, a[i]//2)

if lcm_all > m:
    print(0)
else:
    print((m-lcm_all) // (lcm_all *2 ) +1)

### FIXED CODE ###

from fractions import gcd
n, m = map(int, input().split())
a = list(map(int, input().split()))

def lcm(a, b):
    return a*b // gcd(a, b)
"""
akが偶数だから、bk=2akとして、
bk × (2p + 1)を満たすpが存在すれば良い
2p+1は奇数のため、Xとbkの2の素因数は一致しなければならない
akに含まれる最大の2^kを探す。
"""
if len(a) == 1: lcm_all = a[0]//2
else: lcm_all = lcm(a[0]//2, a[1]//2)
amax = 0
for i in range(1, n):
    lcm_all = lcm(lcm_all, a[i]//2)
    amax = max(amax, a[i]//2)

f = True
if lcm_all > m:
    f = False
for i in range(n):
    if n == 1: break
    if (lcm_all//(a[i]//2))%2 == 0:
        # akの2の約数の数がそろわないので、どんな奇数を掛けても共通のXを作れない
        f = False
if f:
    # amaxの奇数倍でmを超えないもの
    ans = (m // lcm_all + 1)//2
    print(ans)
else:
    print(0)
n = int(input())
    nStr = str(n)
    number = nStr[len(nStr)-1]
    if number == '3':
        print('bon')
    elif number  == '0'or number=='1'or number =='6'or number =='8':
        print('pon')
    else:
        print('hon')


### FIXED CODE ###

n = int(input()) % 10
if n == 3:
  print("bon")
elif n in(0,1,6,8):
  print("pon")
else:
  print("hon")
d = input()
a = int(input())
p = []
k = len(d)
for i in range(k):
  for j in range(min({5,k-i+1})):
    p.append(d[i:i+j])
    
x = list(set(p))
x.sort()
del x[0]
 
print(x[a-1])

### FIXED CODE ###

d = input()
a = int(input())
p = []
k = len(d)
for i in range(k):
  for j in range(min({6,k-i+1})):
    p.append(d[i:i+j])
    
x = list(set(p))
x.sort()
del x[0]
 
print(x[a-1])
N,K=map(int,input().split())
S=0
l=[0 for _ in range(K)]
for i in range(K):
    j=K-i
    v=(K//j)**N-l[j-1]
    for w in range(j//2):
        if j%(w+1)==0:
            l[w]+=v
    S+=j*v
print(S%(10**9+7))

### FIXED CODE ###

s=str(input())
if '7' in s:
    print('Yes')
else:
    print('No')
import math
N, X = map(int, input().split())
x = list(map(int, input().split()))

xx = [abs(xxx - X) for xxx in x]

l = xx[0]
for i in xx[1:]:
    l = math.gcd(l, i)


print(l)

### FIXED CODE ###

N, X = map(int, input().split())
x = list(map(int, input().split()))

xx = [abs(xxx - X) for xxx in x]

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

l = xx[0]
if len(xx) > 1:
    for i in xx[1:]:
        l = gcd(l, i)


print(l)
n,m = map(int,input().split())
a = list(map(int,input().split()))

sum = 0
for i in range(a):
    sum = sum + a[i]
sum_divided = sum/(4*m)
cnt = 0
for i in range(a):
    if a[i] >= sum_divided:
        cnt = cnt + 1
if cnt >= m:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

n,m = map(int,input().split())
a = list(map(int,input().split()))

sum = 0
for i in range(n):
    sum = sum + a[i]
sum_divided = sum/(4*m)
cnt = 0
for i in range(n):
    if a[i] >= sum_divided:
        cnt = cnt + 1
if cnt >= m:
    print('Yes')
else:
    print('No')
for i in int(input()):
	values = sorted(map(lambda x: x^2, input().split()))
	if values[2] = values[1] + values[0]:
		print 'YES'
	else:
		print 'NO'


### FIXED CODE ###

def judge_triangle(edges):
    edges.sort(reverse = True)
    if edges[0]**2 == edges[1]**2 + edges[2]**2:
        return 'YES'
    else:
        return 'NO'

lines = int(input())

for i in range(lines):
    edges = list(map(int, raw_input().split()))
    print judge_triangle(edges)
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x:x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
        else:
            break

print(sum(A))


### FIXED CODE ###

N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))
n,m=map(int,input().split())
s=set(range(1,m+1))
for i in range(n):
    k,*a=list(map(int,input().split()))
    s&=set(a)
print(les(s))

### FIXED CODE ###

n,m=map(int,input().split())
s=set(range(1,m+1))
for i in range(n):
    k,*a=list(map(int,input().split()))
    s&=set(a)
print(len(s))
from itertools import product
N,C=map(int,input().split())
D=[]
for i in range(C):
    Dlist=list(map(int,input().split()))
    D.append(Dlist)
c=[[0]*N*N for i in range(3)]
x=[]
X=[]
for i in range(N):
    x=list(map(int,input().split()))
    X.append(x)
for i in range(N):
    for j in range(N):
        x=X[i][j]
        c[(i+j)%3][x-1]+=1
def func(res):
    print(res)
    for i in range(C):
        for j in range(C):
            if i!=j:
                for k in range(C):
                    if i!=k and j!=k:
                        total=0
                        for l in range(C):                           
                            total+=D[l][i]*c[0][l]
                            total+=D[l][j]*c[1][l]
                            total+=D[l][k]*c[2][l]
                        if total<res:
                            res=total
    return res
x=10**6
res=func(x)
print(res)                                

### FIXED CODE ###

N,C=map(int,input().split())
D=[]
for i in range(C):
    Dlist=list(map(int,input().split()))
    D.append(Dlist)
c=[[0]*(10**6) for i in range(3)]
x=[]
X=[]
for i in range(N):
    x=list(map(int,input().split()))
    X.append(x)
for i in range(N):
    for j in range(N):
        x=X[i][j]
        c[(i+j)%3][x-1]+=1
def func(res):
    for i in range(C):
        for j in range(C):
            if i!=j:
                for k in range(C):
                    if i!=k and j!=k:
                        total=0
                        for l in range(C):                           
                            total+=D[l][i]*c[0][l]
                            total+=D[l][j]*c[1][l]
                            total+=D[l][k]*c[2][l]
                        if total<res:
                            res=total
    return res
y=10**20
res=func(y)
print(res)
X, Y = map(int,input().split())

sum = 0
flug =0

for i in range(X):
    sum = i*4 + (X-i)*2
    if sum == Y:
        print('Yes')
        flag = 1
        break

if flag == 0:
    print('No')

### FIXED CODE ###

X, Y = map(int, input().split())

add = 0
flag =0

for i in range(X+1):
    add = i*4 + (X-i)*2
    if add == Y:
        print('Yes')
        flag = 1
        break

if flag == 0:
    print('No')
from math import gcd
from math import ceil

a, b = 1, 1
for _ in range(int(input())):
  x, y = map(int, input().split())
  k = int(max(ceil(x / a), ceil(y / b)))
  a = x * k
  b = y * k
print(a + b)


### FIXED CODE ###

a, b = 1, 1
for _ in range(int(input())):
  x, y = map(int, input().split())
  k = max((a + x - 1) // x, (b + y - 1) // y)
  a = x * k
  b = y * k
print(a + b)
import collections

def inputN(typ=str):
    return map(typ, raw_input().split())


N = inputN(int)[0]
v = inputN(int)

col0 = collections.Counter(v[::2])
col1 = collections.Counter(v[1::2])
X0 = col0.most_common()
X1 = col1.most_common()
#print X0
#print X1

if X0[0][0] == X1[0][0]:
    if len(X0) == 1 and len(X1) == 1:
        k0 = X[0][1]
        k1 = 0
    elif len(X0) == 1:
        k0 = X0[0][1]
        k1 = X1[1][1]
    elif len(X1) == 1:
        k0 = X0[1][1]
        k1 = X1[0][1]
    elif X0[1][1] >= X1[1][1]:
        k0 = X0[0][1]
        k1 = X1[1][1]
    elif X0[1][1] < X1[1][1]:
        k0 = X0[1][1]
        k1 = X1[0][1]
else:
    k0 = X0[0][1]
    k1 = X1[0][1]

print N - k0 - k1


### FIXED CODE ###

import collections

def inputN(typ=str):
    return map(typ, raw_input().split())


N = inputN(int)[0]
v = inputN(int)

col0 = collections.Counter(v[::2])
col1 = collections.Counter(v[1::2])
X0 = col0.most_common()
X1 = col1.most_common()
#print X0
#print X1

if X0[0][0] == X1[0][0]:
    if len(X0) == 1 and len(X1) == 1:
        k0 = X0[0][1]
        k1 = 0
    elif len(X0) == 1:
        k0 = X0[0][1]
        k1 = X1[1][1]
    elif len(X1) == 1:
        k0 = X0[1][1]
        k1 = X1[0][1]
    elif X0[1][1] >= X1[1][1]:
        k0 = X0[1][1]
        k1 = X1[0][1]
    elif X0[1][1] < X1[1][1]:
        k0 = X0[0][1]
        k1 = X1[1][1]
else:
    k0 = X0[0][1]
    k1 = X1[0][1]

print N - k0 - k1
r=int(input())
x=math.pi
print("{:.7f}".format(r*r*x),"{:.7f}".format(2*r*x))



### FIXED CODE ###

from math import pi

r=float(input())
print("{:.7f}".format(pi*r*r),"{:.7f}".format(2*r*pi))
import sys
input = sys.stdin.readline()
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q
num = [0]*100000
s = sum(A)
for i in range(Q):
    B[i], C[i] = map(int,input().split())
for i in range(100000):
    num[i] = A.count(i+1)
for i in range(Q):
    s += (C[i]-B[i])*num[B[i]-1]
    print(s)
    num[C[i]-1] += num[B[i]-1]
    num[B[i]-1] = 0

### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
cnt = [0]*100005
s = sum(A)
for i in A:
    cnt[i] += 1
for i in range(Q):
    B, C = map(int,input().split())
    s += (C-B)*cnt[B]
    print(s)
    cnt[C] += cnt[B]
    cnt[B] = 0
x, k, d = map(int, input().split())

x = abs(x)
k = k - int(x/d)
if k <= 0:
    print(x - k * d)
    exit()
    
a = x - int(x/d)*d
if k % 2 == 0:
    ans = a
else:
    ans = abs(a - d)

print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

s = input()
if s[1] == 'R':
    print(s.count('R'))
else:
    if s[0] == 'S' and s[2] == 'S':
        print(0)
    else:
        print(1)
s = input()
print(2 * min(s.count(0), s.count(1)))


### FIXED CODE ###

s = input()
print(2 * min(s.count('0'), s.count('1')))
s=input()
n=len(n)

c=0
i=0
while i<n-i-1
  if s[i]!=s[n-i-1]:
    c+=1
  i+=1
print(c)

### FIXED CODE ###

s=input()
n=len(s)
 
c=0
i=0
while i<n-i-1:
  if s[i]!=s[n-i-1]:
    c+=1
  i+=1
print(c)
n=int(input())
a_list=[str(i)*3 for i in range(1,10)]

for i in range(1,9):
  if a_list[i]< n <= a_list[i+1]:
    print(a_list[i+1])
    break
  elif a_list[i]= n:
    print(a_list[i])
    
    

### FIXED CODE ###

n=int(input())
a_list=[str(i)*3 for i in range(1,10)]
flag=False

for i in range(0,9):
  if int(a_list[i])==n:
    print(a_list[i])
    flag=True
    break
  elif n < 111:
    print("111")
    break
if flag==False:
  for i in range(0,8):
    if int(a_list[i])< n < int(a_list[i+1]):
      print(a_list[i+1])
      break