Harryxun/stf_run huggingface dataset:
x,k,d=map(int,input().split())

a=x//d

if a>=k:
  x=x-(d*k)
  print(abs(x))
else:
  x=x-(d*a)
  b=k-a
  if b%2==0:
    print(abs(x))
  else:


### FIXED CODE ###

x,k,d=map(int,input().split())

a=abs(x)//d

if x<0:
  d=d*(-1)

if a>=k:
  x=x-(d*k)
  print(abs(x))
else:
  x=x-(d*a)
  b=k-a
  if abs(x)<abs(x-d):
    if b%2==0:
      print(abs(x))
    else:
      print(abs(x-d))
  else:
    x=x-d
    b-=1
    if b%2==0:
      print(abs(x))
    else:
      print(abs(x+d))
import collections
import math
import numpy as np
n = int(input())
ai = [int(i) for i in input().split()]

num = 10**6+1

mod = 10**9+7

if n == 1:
    print(1)
    exit()
    
    
# 素因数分解、辞書で返すやつ
# mathをimportする
def prime(n):
    factor = {}
    tmp = int(math.sqrt(n)) + 1
    for num in range(2, tmp):
        while n % num == 0:
            n //= num
            if not num in factor.keys():
                factor[num] = 1
            else:
                factor[num] += 1
        if num > n:
            break
    if n != 1:
        if not n in factor.keys():
            factor[n] = 1
        else:
            factor[n] += 1
    return factor


pri_li = []

for i in range(n):
    pri_li.append(collections.Counter(prime(ai[i])))
com_ab = [0]*(num)

dic = {}
for a in ai:
    pr = prime(a)
    for num, count in pr.items():
        if not num in dic.keys():
            dic[num] = count
        else:
            if dic[num] < count:
                dic[num] = count

kari = 1
for num,count in dic.items():
    kari *= pow(num,count,mod)

kari_old = kari
ans = 0

for i in range(n):
    kari = kari_old
    #com_ab_new = copy.deepcopy(com_ab)
    #print(com_ab,com_ab_new)
    keys = pri_li[i].keys()
    #kari = 1
    #for j in range(kosu):
    #    kari *= pow(sosu_li[j][0],com_ab[sosu_li[j][0]],mod)
    for j in keys:
        kari *= pow(pow(j,pri_li[i][j],mod),mod-2,mod)
    ans += kari
    ans %= mod
    #print(ans)
        
print(ans)


### FIXED CODE ###

import collections
import math
n = int(input())
ai = [int(i) for i in input().split()]

mod = 10**9+7

if n == 1:
    print(1)
    exit()
    
    
# 素因数分解、辞書で返すやつ
# mathをimportする
def prime(n):
    factor = {}
    tmp = int(math.sqrt(n)) + 1
    for num in range(2, tmp):
        while n % num == 0:
            n //= num
            if not num in factor.keys():
                factor[num] = 1
            else:
                factor[num] += 1
        if num > n:
            break
    if n != 1:
        if not n in factor.keys():
            factor[n] = 1
        else:
            factor[n] += 1
    return factor


dic = {}
for a in ai:
    pr = prime(a)
    for num, count in pr.items():
        if not num in dic.keys():
            dic[num] = count
        else:
            if dic[num] < count:
                dic[num] = count

kari = 1
for num,count in dic.items():
    kari *= pow(num,count,mod)
    kari %= mod

kari_old = kari
ans = 0

for i in range(n):
    kari = kari_old
    #com_ab_new = copy.deepcopy(com_ab)
    #print(com_ab,com_ab_new)
    #kari = 1
    #for j in range(kosu):
    #kari *= pow(sosu_li[j][0],com_ab[sosu_li[j][0]],mod)
    kari *= pow(ai[i],mod-2,mod)
    ans += kari
    ans %= mod
    #print(ans)
    #print(ans)
        
print(ans)
a,b,c=map(int,input().split())

print(len(set(a,b,c))

### FIXED CODE ###

a,b,c=map(int,input().split())

print(len({a,b,c}))
K = int(input())
A, B = map(int, input().split())

If A%K == 0 or B%K == 0
	print 'OK'

elif A <= K*(A//K + 1) and K*(A//K + 1) <= B :
	print 'OK'

else :
	print 'NG'


### FIXED CODE ###

K  = int((input() .split())[0])
A, B = map(int, input().split())

if A%K == 0 or  K*(A//K + 1) <= B :
	print('OK')

else :
	print('NG')
N,Q = map(int,input().split())
S = str(input())


idx = []

for i in range(len(S)):
        if S[i] != "A" and S[i] != "C":
            flag = 0
            continue
        if S[i] == "A":
            flag = 1
            continue
        if S[i] == "C" and flag == 1:
            flag = 0
            idx.append(i-1)
            continue
        if S[i] == "C" and flag == 0:
            continue

#print(idx)
L = []
R = []
for _ in range(Q):
    l,r = map(int,input().split())
    ans = 0
    for i in idx:
        if i in range(l-1,r-1):
            ans += 1
    print(ans)
    


### FIXED CODE ###

N,Q = map(int,input().split())
s = input()
t = [0]
ans = []
for i in range(N):
    if s[i:i+2] == "AC":
        t.append(t[i] + 1)
    else:
        t.append(t[i])
for i in range(Q):
    l,r = map(int,input().split())
    ans.append(t[r-1]-t[l-1])
for i in ans:
    print(i)
#潜 影 蛇 手 ! !
N=int(input())
T,A=pin()
c=(T-A)/0.006
HH=[abs(h-c) for h in pin()]
#print(c,HH)
print(HH.index(min(HH))+1)

### FIXED CODE ###

#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
#どっかで最悪計算量の入力データを用意する関数を作ろう？
"""

"""
#solution:
#潜 影 蛇 手 ! !
N=int(input())
T,A=pin()
H=[i for i in pin()]
c=(T-A)/0.006
HH=[abs(h-c) for h in H]
#print(c,HH)
print(HH.index(min(HH))+1)
n, m = map(int, input().split())
l = sorted(list(map(int, input().split())), reverse=True)

if m == 1:
    S =0
else:
    d = sorted([l[i] - l[i+1]  for i in range(m-1)], reverse=True)
    S = max(l) - min(l)

    if n > 1:
        for i in range(n-1):
            S -= d[i]
    else:
        S = l[0]

print(S)


### FIXED CODE ###

n, m = map(int, input().split())
l = sorted(list(map(int, input().split())), reverse=True)

if n>=m or m == 1:
    S =0
else:
    d = sorted([l[i] - l[i+1]  for i in range(m-1)], reverse=True)
    S = max(l) - min(l)
    for i in range(n-1):
        S -= d[i]

print(S)
N = int(input())
S = list(input())
def cal(a, b, c):
    x = a[0]
    for bb in b:
        if bb > x:
            y = bb
            for cc in c:
                if cc > y:
                    return 1
    return 0
li = [[] for _ in range(10)]
for i, n in enumerate(S):
    num = int(n)
    li[num].append(i)
cnt = 0
for i in range(10):
    if len(li[i]) > 0:
        for j in range(10):
            if len(li[j]) > 0:
                for k in range(10):
                    if len(li[k]) > 0:
print(cnt)

### FIXED CODE ###

N = int(input())
S = list(input())

def cal(x, a):
    for aa in a:
        if aa > x:
            return aa
    return False

li = [[] for _ in range(10)]
for i, n in enumerate(S):
    num = int(n)
    li[num].append(i)
    
cnt = 0
flag = True
for i in range(10):
    if len(li[i]) > 0:
        for j in range(10):
            if len(li[j]) > 0:
                ret = cal(li[i][0], li[j])
                if  ret == False:
                    flag = False
                else: 
                    flag = True
                for k in range(10):
                    if len(li[k]) > 0 and flag == True:
                        ret2=cal(ret, li[k]) 
                        if  ret2 != False:
                            cnt += 1
print(cnt)
import math

N,d,a = map(int, input().split())
xh = sorted([list(map(int, input().split())) for i in range(N)], key=itemgetter(0))
x = []
for X,h in xh:
  x.append(X)
data0 = [0]*(N+1)
data1 = [0]*(N+1)

def _add(data, k, x):
    while k <= N:
        data[k] += x
        k += k & -k
def add(l, r, x):
    _add(data0, l, -x*(l-1))
    _add(data0, r, x*(r-1))
    _add(data1, l, x)
    _add(data1, r, -x)

def _get(data, k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
def query(l, r):
    return _get(data1, r-1) * (r-1) + _get(data0, r-1) - _get(data1, l-1) * (l-1) - _get(data0, l-1)

ans = 0

for L,(X,H) in enumerate(xh):
  L += 1
  R = bisect.bisect_right(x,X+2*d)
  res = math.ceil((H - query(L,L+1) * a) / a) #ansを変える
  ans += res
  add(L,R+1,res)
print(ans)  

### FIXED CODE ###

from operator import itemgetter
import bisect
import math

N,d,a = map(int, input().split())
xh = sorted([list(map(int, input().split())) for i in range(N)], key=itemgetter(0))
x = []
for X,h in xh:
  x.append(X)
ans = 0
attack_start = [i for i in range(N)]
now = 0
cumsum = [0] * (N+1)
for L,(X,H) in enumerate(xh):
  R = bisect.bisect_right(x,X+2*d)
  for i in range(now,R):
    attack_start[i] = L
  now = R
  damage = (cumsum[L] - cumsum[attack_start[L]]) * a
  attack_time = max(0,math.ceil((H - damage) / a))
  ans += attack_time
  cumsum[L+1] = ans
print(ans)
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 20 20:52:23 2019

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

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

x = [0]*N
x[:] = map(int, input().split())

P = [0]*M
Y = [0]*M
for m in range(M):
    P[m], Y[m] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""
N = int(input())

h = [0]*N
h[:] = map(int, input().split())

h = np.array(h)

cnt = 0
while np.sum(h ==0) != N:
    if np.sum(h ==0) == 0:        
        h = h-1
        cnt +=1
    else:
        for n in range(N-1):
            if h[n] == 0 and h[n+1] !=0:
                l = n+1
            elif h[0] != 0:
                l = 0
            else:
                pass
            if h[n] != 0 and h[n+1] == 0:
                r = n
                break
        for x in range(l, r+1):
            h[x] = h[x] -1
        cnt +=1
print(cnt)
            
            
            
        
            


### FIXED CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 20 20:52:23 2019

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

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

x = [0]*N
x[:] = map(int, input().split())

P = [0]*M
Y = [0]*M
for m in range(M):
    P[m], Y[m] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""
N = int(input())

h = [0]*N
h[:] = map(int, input().split())

h = np.array(h)

cnt = 0
while np.sum(h ==0) != N:
    if np.sum(h ==0) == 0:        
        h = h-1
        cnt +=1
    else:
        r=N-1
        for n in range(N-1):
            if h[n] == 0 and h[n+1] !=0:
                l = n+1
            elif h[0] != 0:
                l = 0
            else:
                pass
            if h[n] != 0 and h[n+1] == 0:
                r = n
                break
        for x in range(l, r+1):
            h[x] = h[x] -1
        cnt +=1
print(cnt)
_ = input()
a_L = list(map(int,input().split()))

if len(set(a_L)) == 3:
    print("Three")
else:
    print("Four")

### FIXED CODE ###

_ = input()
a_L = list(map(str,input().split()))

if len(set(a_L)) == 3:
    print("Three")
else:
    print("Four")
#一個だけRE、わからねえ
#解答見る
h,w=map(int,input().split())

def go_next(I,J):
    global dp,w,h
    if 0<=I<h-1:
        if x[I+1][J]=="." and dp[I+1][J]>dp[I][J]+1:
            dp[I+1][J]=dp[I][J]+1
            go_next(I+1,J)
    if 0<I<=h-1:
        if x[I-1][J]=="." and dp[I-1][J]>dp[I][J]+1:
            dp[I-1][J]=dp[I][J]+1
            go_next(I-1,J)
    if 0<=J<w-1:
        if x[I][J+1]=="." and dp[I][J+1]>dp[I][J]+1:
            dp[I][J+1]=dp[I][J]+1
            go_next(I,J+1)
    if 0<J<=w-1:
        if x[I][J-1]=="." and dp[I][J-1]>dp[I][J]+1:
            dp[I][J-1]=dp[I][J]+1
            go_next(I,J-1)

x=[]
for i in range(h):
    x.append(list(input()))
#print(x)
c=0
for i in range(h):
    for j in range(w):
        if x[i][j]==".":
            c+=1
lnum=10000000000
dp=[[lnum for i in range(w)]for j in range(h)]
dp[0][0]=1
go_next(0,0)
if dp[h-1][w-1]==lnum:
    print(-1)
else:
    print(c-dp[h-1][w-1])


### FIXED CODE ###

#一個だけRE、わからねえ
#解答見る
import sys
sys.setrecursionlimit(1005)
h,w=map(int,input().split())

def go_next(I,J,n):
    n+=1
    global dp,w,h
    if n>1000:
        return
    if 0<=I<h-1:
        if x[I+1][J]=="." and dp[I+1][J]>dp[I][J]+1:
            dp[I+1][J]=dp[I][J]+1
            go_next(I+1,J,n)
    if 0<I<=h-1:
        if x[I-1][J]=="." and dp[I-1][J]>dp[I][J]+1:
            dp[I-1][J]=dp[I][J]+1
            go_next(I-1,J,n)
    if 0<=J<w-1:
        if x[I][J+1]=="." and dp[I][J+1]>dp[I][J]+1:
            dp[I][J+1]=dp[I][J]+1
            go_next(I,J+1,n)
    if 0<J<=w-1:
        if x[I][J-1]=="." and dp[I][J-1]>dp[I][J]+1:
            dp[I][J-1]=dp[I][J]+1
            go_next(I,J-1,n)

x=[]
for i in range(h):
    x.append(list(input()))
#print(x)
c=0
for i in range(h):
    for j in range(w):
        if x[i][j]==".":
            c+=1
dp=[[10000000000 for i in range(w)]for j in range(h)]
dp[0][0]=1
go_next(0,0,0)
if dp[h-1][w-1]==10000000000:
    print(-1)
else:
    print(c-dp[h-1][w-1])
a,b = map(int ,input().split())
if(a*b % 2 == 0):
	return(Even)
return(Odd)

### FIXED CODE ###

a, b = map(int,input().split())
if(a*b)%2==0:
	print("Even")
else:
	print("Odd")
import numpy as np

def main():
    N, K = (int(x) for x in input().split(" "))
    A = [int(x) for x in input().split(" ")]
    lastest_score = np.array(A[0:K]).prod()
    for i in range(1, N-K+1):
        current_score = np.array(A[i:i+K]).prod()
        if lastest_score < current_score:
            print("Yes")
        else:
            print("No")
        lastest_score = current_score


if __name__ == '__main__':
    main()


### FIXED CODE ###


def main():
    base = 400
    increment = 200
    X = int(input())
    for i in range(8):

        if base+increment*(i) <= X and X < base+increment*(i+1):
            print(8-i)


if __name__ == '__main__':
    main()
hh, ww = map(int, input(),split())
h, w = map(int, input(),split())

print(hh*ww - h - w + 1)

### FIXED CODE ###

hh,ww = map(int, input().split())
h, w = map(int, input().split())
s = hh*ww - h*ww - hh*w + h*w
print(s)
import decimal
a,b = input().split()
print(int(decimai.Decimal(a) * decimal.Decimal(b)))

### FIXED CODE ###

import decimal
a,b = input().split()
print(int(decimal.Decimal(a) * decimal.Decimal(b)))
K=int(input())
ans=0

def gcd2(a,b):
    if b==0:
        return a
    return gcd2(b,a%b)
def gcd3(a,b,c):
    return gcd2(gcd2(a,b),c)

for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(k,K+1):
            if i==j==k:
                ans+=gcd3(i,j,k)
            elif i==j or j==k:
                ans+=gcd3(i,j,k)*3
            else:
                ans+=gcd3(i,j,k)*6

print(ans)


### FIXED CODE ###

K=int(input())
ans=0

def gcd2(a,b):
    if b==0:
        return a
    return gcd2(b,a%b)
def gcd3(a,b,c):
    return gcd2(gcd2(a,b),c)

for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            if i==j==k:
                ans+=gcd3(i,j,k)
            elif i==j or j==k:
                ans+=gcd3(i,j,k)*3
            else:
                ans+=gcd3(i,j,k)*6

print(ans)
a1,bi = map(int, input().split())
a2,b2 = map(int, input().split())
a3,b3 = map(int, input().split())

ne = [1,1,2,3,4]

ne[max(a1,b1)] = min(a1,b1)
ne[max(a2,b2)] = ne[min(a2,b2)]
ne[max(a3,b3)] = ne[min(a3,b3)]

ne = list(set(ne))

if len(ne) == 1:
  print("YES")
else:
  print("NO")


### FIXED CODE ###


def find(x):
    while table[x] >= 0:
        x = table[x]
    return x

def union(x, y):
    s1 = find(x)
    s2 = find(x)
    if s1 != s2:
        if table[s1] != table[s2]:
            if table[s1] < table[s2]:
                table[s2] = s1
            else:
                table[s1] = s2
        else:
            table[s1] -= 1
            table[s2] = s1
    return
    


a1, b1 = map(int, input().split())
a2, b2 = map(int, input().split())
a3, b3 = map(int, input().split())

ans = [0 for i in range(4)]

ans[a1-1] += 1 
ans[a2-1] += 1 
ans[a3-1] += 1 
ans[b1-1] += 1 
ans[b2-1] += 1 
ans[b3-1] += 1 

if 0 in ans or 3 in ans:
    print("NO")
else:
    print("YES")
import sys
import collections
input = sys.stdin.readline

N = int(input())

for c in str(N):
	if c == '7':
		print('Yes')
		exit(1)
print('No')


### FIXED CODE ###

import sys
import collections
input = sys.stdin.readline

N = int(input())

flag = False
for c in str(N):
	if c == '7':
		flag = True
if flag:
	print('Yes')
else:
	print('No')
import sys
H,W = map(int,input().split())
C = set(map(int, sys.stdin))
for i in C:
    print(C[i])
    print(C[i])


### FIXED CODE ###

import sys

H, W = sys.stdin.readline().split()
C = map(str, sys.stdin)
for i in C:
    print(i)
    print(i)
r,g,b=map(int.input().split())
if ((g*10+b)%4)==0:
  print('YES')
else:
  print('NO')


### FIXED CODE ###

r,g,b=map(int,input().split())
if ((g*10+b)%4)==0:
  print('YES')
else:
  print('NO')
a=int(input())
import math
b=int(math.log(a, 2)
c=1
if b!=1:
  for i in range(2,b+1,1):
    c=max(c,int(a**(1/i))**i)
print(c)

### FIXED CODE ###

X = int(input())
if X != 1:
  A =[]
  for b in range(1,int(X**0.5)+1):
    for p in range(2,X):
      if b**p > X:
        A.append(b**(p-1))
        break
  print(max(A))
else:
  print(1)
a,b,h = map(int,input().split())
print( (a+b) * h // 2 )

### FIXED CODE ###

#11:10
a = int(input())
b = int(input())
h = int(input())
print((a+b)*h//2)
N,M=map(int,input().split())
A=[]
for i in range(M):
  A.append(int(input()))
ans=0
MOD=1000000007
dp=[]
for i in range(N-1,0,-1):
  if i in A:
    dp[i]=0
    continue
  dp[i]=dp[i+1]+dp[i+2]
  
ans=dp[0]
ans/=MOD
print(ans)
           


### FIXED CODE ###

n,m = map(int,input().split())
A = set([int(input()) for _ in range(m)])
mod = 1000000007
dp = [0]*(n+1)
dp[0] = 1
for i in range(1,n+1):
    dp[i] = (dp[i-1] + dp[i-2])%mod
    if i in A:dp[i] = 0
print(dp[n])
from collections import deque
import sys
sys.setrecursionlimit(200000)
N, M = map(int, input().split())
graph = [[] for j in range(M)]
q = deque([])
depth_array = [-1 for i in range(N)]

for i in range(M):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

def bfs(graph, q, depth_array):
    # 終了条件
    if len(q) == 0:
        return depth_array

    cur_room = q.popleft()

    # キューに追加
    for r in graph[cur_room]:
        if depth_array[r] == -1: #未訪問なら
            q.append(r)
            depth_array[r] = depth_array[cur_room] + 1

    return bfs(graph, q, depth_array)
    
# init
q.append(0)
depth_array[0] = 0
depth_array = bfs(graph, q, depth_array)

mark_array = [-1 for i in range(N)]
#print(depth_array)

for room, depth in enumerate(depth_array):
    for node in graph[room]:
        if depth_array[node] + 1 == depth_array[room]:
            mark_array[room] = node
if -1 in mark_array[1:]:
    print("No")
else:
    print("Yes")
    for m in mark_array[1:]:
        print(m+1)


### FIXED CODE ###

from collections import deque
import sys
sys.setrecursionlimit(200000)
N, M = map(int, input().split())
graph = [[] for j in range(N)]
q = deque([])
depth_array = [-1 for i in range(N)]

for i in range(M):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

def bfs(graph, q, depth_array):
    # 終了条件
    if len(q) == 0:
        return depth_array

    cur_room = q.popleft()

    # キューに追加
    for r in graph[cur_room]:
        if depth_array[r] == -1: #未訪問なら
            q.append(r)
            depth_array[r] = depth_array[cur_room] + 1

    return bfs(graph, q, depth_array)
    
# init
q.append(0)
depth_array[0] = 0
depth_array = bfs(graph, q, depth_array)

mark_array = [-1 for i in range(N)]
#print(depth_array)

for room, depth in enumerate(depth_array):
    for node in graph[room]:
        if depth_array[node] + 1 == depth_array[room]:
            mark_array[room] = node

if -1 in mark_array[1:]:
    print("No")
else:
    print("Yes")
    for m in mark_array[1:]:
        print(m+1)
a,b,c=map(int,input().split())

if c=='+':
  print(a+c)
else:
  print(a-c)
  

### FIXED CODE ###

a,b,c=input().split(' ')

if b=='+':
  print(int(a)+int(c))
else:
  print(int(a)-int(c))
import math

N  = int(raw_input())

ans = [0, 0]
T, A = [0] * 1001, [0] * 1001

for i in range(N):
    T[i], A[i] = map(float, raw_input().split())

    if (i != 0) and ((T[i - 1] - T[i] > 0) or (A[i - 1] - A[i] > 0)):

        if (T[i  - 1] / T[i]) > (A[i - 1] / A[i]):
            m  = math.ceil(T[i  - 1] / T[i]) 

        else:
            m = math.ceil(A[i - 1] / A[i])

        T[i] *= m
        A[i] *= m

    ans = [T[i], A[i]]

    T[i] /= m
    A[i] /= m

print "%d" % sum(ans)

### FIXED CODE ###

N  = int(raw_input())

ans = [0, 0]
T, A = [0] * 1001, [0] * 1001

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

    if i != 0 and T[i - 1] - T[i] > 0 or A[i - 1] - A[i] > 0:

        if (T[i  - 1] / T[i]) > (A[i - 1] / A[i]):
            m  = T[i  - 1] / T[i]
            if T[i - 1] % T[i] != 0:
                m += 1 

        else:
            m = A[i - 1] / A[i]
            if A[i - 1] % A[i] != 0:
                m += 1

        T[i] *= m
        A[i] *= m

    ans = [T[i], A[i]]

print sum(ans)
a = input()
b = input()

if a * b % 2 = 0:
    print(Even)
else:
    print(Odd)


### FIXED CODE ###

a, b = map(int, input().split())
c = a * b

if c % 2 == 0:
  print('Even')
else:
  print('Odd')
import numpy as np

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = np.array(list(input()))

i = 0
minus = 0
for i in np.arange(K):
    arr = T[np.arange(i, N, K)]
    c = 1
    for j in np.arange(arr.shape[0] - 1):
        if arr[j] == arr[j + 1]:
            c += 1
        else:
            if c >= 2:
                if arr[j] == 'r':
                    score = P
                elif arr[j] == 's':
                    score = R
                else:
                    score = S
                minus += c // 2 * score
            c = 1
    else:
        if c >= 2:
            if arr[j] == 'r':
                score = P
            elif arr[j] == 's':
                score = R
            else:
                score = S
            minus += c // 2 * score

u, counts = np.unique(T, return_counts=True)

max_score = 0
for i, j in zip(u, counts):
    if i == 'r':
        max_score += P * j
    elif i == 's':
        max_score += R * j
    else:
        max_score += S * j
print(max_score - minus)


### FIXED CODE ###

import numpy as np
from collections import Counter

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = np.array(list(input()))

i = 0
minus = 0
for i in np.arange(K):
    arr = T[np.arange(i, N, K)]
    c = 1
    for j in np.arange(arr.shape[0] - 1):
        if arr[j] == arr[j + 1]:
            c += 1
        else:
            if c >= 2:
                if arr[j] == 'r':
                    score = P
                elif arr[j] == 's':
                    score = R
                else:
                    score = S
                minus += c // 2 * score
            c = 1
    else:
        if c >= 2:
            if arr[j] == 'r':
                score = P
            elif arr[j] == 's':
                score = R
            else:
                score = S
            minus += c // 2 * score

counter = Counter(T)


max_score = 0
for i, j in counter.items():
    if i == 'r':
        max_score += P * j
    elif i == 's':
        max_score += R * j
    else:
        max_score += S * j
print(max_score - minus)