Harryxun/stf_run huggingface dataset:
def distances(tree: list, treeSize: int, root: int)->list:
    dists = [0] * treeSize

    def dfs(v: int, prev: int,  dist: int):
        dists[v] = dist
        for u, c in tree[v]:
            if u == prev:
                continue
            dfs(u, v, dist+c)

    dfs(root, -1, 0)

    return dists


def transit_tree_path(
        N: int, edges: list, Q: int, K: int, queries: list)->list:
    tree = [[] for _ in range(N)]
    for u, v, c in edges:
        tree[u-1].append((v-1, c))
        tree[v-1].append((u-1, c))

    d = distances(tree, N, K-1)

    return [d[x-1]+d[y-1] for x, y in queries]


if __name__ == "__main__":
    N = int(input())
    edges = [tuple(map(int, input().split())) for _ in range(N-1)]
    Q = 0
    Q, K = map(int, input().split())
    queries = [tuple(map(int, input().split())) for _ in range(Q)]

    ans = transit_tree_path(N, edges, Q, K, queries)
    for a in ans:
        print(a)


### FIXED CODE ###

from queue import Queue


def distances(tree: list, treeSize: int, root: int)->list:
    dists = [0] * treeSize

    q = Queue()
    q.put((root, -1, 0))

    while not q.empty():
        v, prev, d2v = q.get()

        dists[v] = d2v
        for u, dv2u in tree[v]:
            if prev == u:
                continue
            q.put((u, v, d2v + dv2u))

    return dists


def transit_tree_path(
        N: int, edges: list, Q: int, K: int, queries: list)->list:
    tree = [[] for _ in range(N)]
    for u, v, c in edges:
        tree[u-1].append((v-1, c))
        tree[v-1].append((u-1, c))

    d = distances(tree, N, K-1)

    return [d[x-1]+d[y-1] for x, y in queries]


if __name__ == "__main__":
    N = int(input())
    edges = [tuple(map(int, input().split())) for _ in range(N-1)]
    Q = 0
    Q, K = map(int, input().split())
    queries = [tuple(map(int, input().split())) for _ in range(Q)]

    ans = transit_tree_path(N, edges, Q, K, queries)
    for a in ans:
        print(a)
def solve():
	N = int(input())
	h = [int(i) for i in input().split()]
    return N * max(h) - sum(h)

print(solve())

### FIXED CODE ###

def solve():
	N = int(input())
	h = [int(i) for i in input().split()]
	ans = 0
	for i in range(0, N):
		ans += max(h[i] - (0 if i - 1 < 0 else h[i - 1]), 0)
	return ans

print(solve())
def forbidden(x,p):
    res = []
    for num in range(1,101):
        if x!=num and num not in p:
            res.append(abs(x-num))
    if x-min(res) not in p and x-min(res)!=0:
        return x-min(res)
    return x+min(res)

if __name__ == "__main__":
    x,n = map(int,input().split())
    p = list(map(int,input().split()))
    if n==0:
        print(x)
        exit()
    print(forbidden(x,p))

### FIXED CODE ###

x,n = map(int,input().split())
p = list(map(int,input().split()))
if x not in p:
    print(x)
    exit()
a,b = x,x
while True:
    a+=1
    b-=1
    if b not in p:
        print(b)
        break
    elif a not in p:
        print(a)
        break
n,w=map(int, input().split())
weight=[0]*n
value=[0]*n
for i in range(n):
    weight[i],value[i]=map(int,input().split())

dp=[[0]*(n+10) for i in range(100100)]
for i in range(n):
    for sum_w in range(w+1):
        # i番目の品物を選ぶ場合
        if sum_w -  weight[i] >= 0:
            dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w - weight[i]]+value[i])
        # i番目の品物を選ばない場合
        dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w])
print(dp[n][w])

### FIXED CODE ###

n,w=map(int, input().split())
weight=[0]*n
value=[0]*n
for i in range(n):
    weight[i],value[i]=map(int,input().split())

dp=[[0]*(w+1) for i in range(n+1)]
for i in range(n):
    for sum_w in range(w+1):
        # i番目の品物を選ぶ場合
        if sum_w -  weight[i] >= 0:
            dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w - weight[i]]+value[i])
        # i番目の品物を選ばない場合
        dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w])
print(dp[n][w])
a=input()
b=input()
if (a>b):
 print"a>b"
if (a<b):
 print"a<b"
else:
 print"a=b"

### FIXED CODE ###

a, b = map(int, raw_input().split())
if a < b:
 print "a < b"
elif a > b:
 print "a > b"
else:
 print "a == b"
W, H, x, y = map(int,input().split())
hantei = 0
if (x != 0) or (y != 0):
    if (W%x == 0):
        if y*(W/x) == H:
            hantei = 1
print("{0} {1}".format(W*H/2, hantei))


### FIXED CODE ###

W, H, x, y = map(int,input().split())
hantei = 0
if (x != 0) and (y != 0):
    if (W/2 == x) and (H/2 == y):
        hantei = 1
print("{0} {1}".format(W*H/2, hantei))
while 1:
    a, b, c, d, e, f = map(float, i)
    x = (e*c - b*f)/(e*a - b*d)
    y = (c*d - f*a)/(d*b - e *a)
    print(x,y) 

### FIXED CODE ###

while(True):
        try:
            a,b,c,d,e,f=map(float,input().split(" "))
            z=a*e-b*d
            if(z!=0):
                x=(c*e-b*f)/z
                y=(a*f-c*d)/z
            print("{:.3f} {:.3f}".format(x+0,y+0))
        except:
            break
inp = input().split()
n,k = map(int, inp)
nums = list(input().split())
nums_list = list(map(int, nums))
kill_en_hp = 0
if len(nums_list) < k:
    print(0)
else:
    nums_list.sort()
    nums_list_2 = nums_list[k:]
    print(sum(nums_list_2u))

### FIXED CODE ###

inp = input().split()
n,k = map(int, inp)
nums = list(input().split())
nums_list = list(map(int, nums))
kill_en_hp = 0
if len(nums_list) < k:
    print(0)
else:
    nums_list.sort(reverse=True)
    nums_list_2 = nums_list[k:]
    print(sum(nums_list_2))
print(max(0,eval(input().replace('','-2*'))))

### FIXED CODE ###

a,b=map(int,input().split());print(max(a-b-b,0))
K=input()
a,b=map(int,input().split())
p=1
for i in range(a,b+1):
  if i%K==0:
    print("OK")
    p=0
    break
if p==1:
  print("NG")
    

### FIXED CODE ###

K=int(input())
a,b=map(int,input().split())
p=1
for i in range(a,b+1):
  if i%K==0:
    print("OK")
    p=0
    break
if p==1:
  print("NG")
n = int(input())
a = list(map(int, input().split()))
b = []

for i in range(n):
    b.append(a[i])

i = 1
j = 0
c = b[0]
ans = n
num = 0

while i < n-1 or j < n-1:
    if b[j] ^ b[i] == b[j] | b[i]:
        c = b[j] | b[i]
        num += 1
        ans += num
        i += 1
    else:
        c -= b[j]
        j += 1

print(ans)


### FIXED CODE ###

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

i = 1
j = 0
c = a[0]
ans = n
num = 0

while i <= n-1:
    if c ^ a[i] == c + a[i]:
        c = c ^ a[i]
        num += 1
        ans += num
        i += 1
    else:
        c = c & ~a[j]
        j += 1
        num -= 1

print(ans)
import numpy as np
N,K,Q=map(int,input().split())
A=[]
for i in range(Q):
    a=int(input())
    A.append(a)

P=[K]*N
P1=np.array(P)
for i in A:
    P1=P1-1
    P1[i-1]=P1[i-1]+1
for i in range(len(P1)):
    if P1[i]>0:
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

S=input()
if S=='Sunny':
    print('Cloudy')
elif S=='Cloudy':
    print('Rainy')
else:
    print('Sunny')
#Coding:Utf-8
import sys
input=sys.stdin.readline

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

for i in range(N):
    ans[(A[i]-1)]=i+1

for l in range(N):
    print(ans[l],end=" ")

print("")


### FIXED CODE ###

#Coding:Utf-8

N=int(input())
A=[int(input()) for i in range(N)]
B=sorted(A,reverse=True)

for l in range(N):
    if A[l]!=B[0]:
        print(B[0])
    else:
        print(B[1])
N = int(input())
ls = [int(input()) for range(5)]
X = min(ls)
print((N+X-1)//X+4)

### FIXED CODE ###

N=int(input())
A=[int(input()) for i in range(5)]
X=min(A)
ans=(N+X-1)//X +4
print(ans)
import numpy as np

n = int(input())
a_list = []

a_list = list(map(int,input().split()))

result = np.prod(a_list)

if result > 10 ** 18:
    print('-1')
else:
    print(result)


### FIXED CODE ###

import sys

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

if 0 in a:
    print(0)
else:
    cur = 1
    for x in a:
        cur *= x
        if cur > 10 ** 18:
            print(-1)
            sys.exit()
    print(cur)
h,n=map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
maxAi= max(a for a,b in AB)
dp=[0]*(H+maxAi)
i=1
try:
    dp[i]=min(dp[i-a]+b for a,b in AB)
    i+=1
except:
    print(min(dp[H:]))

### FIXED CODE ###

H,n=map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(n)]
maxAi= max(a for a,b in AB)
dp=[0]*(H+maxAi)
i=1
while True:
  try:
    dp[i]=min(dp[i-a]+b for a,b in AB)
    i+=1
  except:
    print(min(dp[H:]))
    break
N = int(input())
C=[0]*N
C[0]=0
C[1]=0
C[2]=1
n=2
#フィボナッチ数列のイメージ
while True:
    n=n+1
    C[n]=C[n-1]+C[n-3]
    #print(n,C[n])
    if n==(N-1):
        break
print(C[N-1]%(10**9+7))

### FIXED CODE ###

import sys
N = int(input())
if N>=4:
    C=[0]*N
    C[0]=0
    C[1]=0
    C[2]=1
    n=2
    #フィボナッチ数列のイメージ
    while True:
        n=n+1
        C[n]=C[n-1]+C[n-3]
        #print(n,C[n])
        if n==(N-1):
            break
    print(C[N-1]%(10**9+7))
if N==3:
    print(1)
if N<=2:
    print(0)
N = int(raw_input())
H = map(int, raw_input().split())

def sumOfCost(i):
  if i == 0:
    return 0
  elif i == 1:
    return sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    return min(c1,c2)
print sumOfCost(N-1)




### FIXED CODE ###

import sys

sys.setrecursionlimit(100000)

N = int(raw_input())
H = map(int, raw_input().split())
cost = [-1]*N

def sumOfCost(i):
  if cost[i] >= 0:
    return cost[i]
  if i == 0:
    soc=0
  elif i == 1:
    soc= sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    soc=min(c1,c2)
  cost[i] = soc
  return soc

print sumOfCost(N-1)
n = int().input()
print (str(int(n*(n+1)/2)))

### FIXED CODE ###

n = int(input())
print (int(n*(n+1)/2))
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra
import numpy as np

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

Us=[0]*M
Vs=[0]*M
As=[0]*M
Bs=[0]*M
for i in range(M):
    Us[i],Vs[i],As[i],Bs[i] = map(int,input().split())
    Us[i] -= 1
    Vs[i] -= 1

# ---
V={}
Ns = max(As)*N
Nv=N*(Ns+1)
cnt=0
for i in range(N):
    for ns in range(Ns+1):
        V[(i,ns)]=cnt
        cnt+=1

graph = np.zeros((Nv,Nv),dtype=np.int64)

if S > Ns:
    S = Ns

# U <-> V
for _ in range(M):
    u = Us.pop()
    v = Vs.pop()
    a = As.pop()
    b = Bs.pop()
    #
    for i in range(Ns+1): # i = 0,..,Ns
        ns_s = Ns - i
        ns_g = ns_s - a
        if ns_g >= 0:
            # U -> V
            graph[ V[(u,ns_s)], V[(v,ns_g)]] = b
            # V -> U
            graph[ V[(v,ns_s)], V[(u,ns_g)]] = b
# -----

for i in range(N):
    c,d = map(int,input().split())
    for j in range(Ns): # j = 0,...,Ns-1
        ns_s = j
        ns_g = j + c
        if ns_g > Ns:
            ns_g = Ns
        graph[ V[(i,ns_s)], V[(i,ns_g)]] = d

s=V[(0,S)]
dist_matrix = dijkstra(csgraph=graph, directed=True, indices = s)

for i in range(1,N):
    ans = min([dist_matrix[i*(Ns+1)+v] for v in range(Ns+1)])
    print(int(ans))

### FIXED CODE ###

import math
import heapq

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

Us=[0]*M
Vs=[0]*M
As=[0]*M
Bs=[0]*M
for i in range(M):
    Us[i],Vs[i],As[i],Bs[i] = map(int,input().split())
    Us[i] -= 1
    Vs[i] -= 1

# ---
V={}
Ns = max(As)*N
Nv=N*(Ns+1)
cnt=0
for i in range(N):
    for ns in range(Ns+1):
        V[(i,ns)]=cnt
        cnt+=1

G=[[] for _ in range(Nv)]

if S > Ns:
    S = Ns

# U <-> V
for _ in range(M):
    u = Us.pop()
    v = Vs.pop()
    a = As.pop()
    b = Bs.pop()
    #
    for i in range(Ns+1): # i = 0,..,Ns
        ns_s = Ns - i
        ns_g = ns_s - a
        if ns_g >= 0:
            # U -> V
            G[ V[(u,ns_s)] ].append( (V[(v,ns_g)],b) )
            # V -> U
            G[ V[(v,ns_s)] ].append( (V[(u,ns_g)],b) )
            #print(u,v,a,b,ns_s,ns_g,V[(u,ns_s)],V[(v,ns_g)])

# -----

for i in range(N):
    c,d = map(int,input().split())
    for j in range(Ns): # j = 0,...,Ns-1
        ns_s = j
        ns_g = j + c
        if ns_g > Ns:
            ns_g = Ns
        G[ V[(i,ns_s)] ].append( (V[(i,ns_g)],d) )

# -----
D=[math.inf]*Nv
q=[]
# -----

s=V[(0,S)]
D[s] = 0
heapq.heappush(q,(0,s))

while len(q) > 0:
    p_cost,p_v = heapq.heappop(q)
    if D[p_v] < p_cost:
        continue
    for e_to,e_cost in G[p_v]:
        if D[ e_to ] > D[p_v] + e_cost:
            D[e_to] = D[p_v] + e_cost
            heapq.heappush(q, (D[e_to], e_to))

for i in range(1,N):
    ans = min([D[i*(Ns+1)+v] for v in range(Ns+1)])
    print(ans)
import sys
X = int(input())
# = map(int, input().split())
# = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]

for i in range(100000):
    C = X + (i**5)
    if C**(1/5)%1 ==0:
        print(int(C**(1/5)), i)
        sys.exit()
    C = X - (i**5)
    if C**(1/5)%1 ==0:
        print(int(C**(1/5)), -i)
        sys.exit()


### FIXED CODE ###

X = int(input())
# = map(int, input().split())
# = list(map(int, input().split()))
A = [i**5 for i in range(200)]
B = [i**5 for i in range(200)]
# = [list(map(int, input().split())) for i in range(N)]

for i in range(200):
    for j in range(200):
        if A[i] + B[j] == X:
            print(int(A[i]**(1/5)), int(B[j]**(1/5)*(-1)))
            exit()
        if A[i] - B[j] == X:
            print(int(A[i]**(1/5)), int(B[j]**(1/5)))
            exit()
N = int(input())
A = []
for i in len(N):
    A.append(int(input))
print(len(set(A)))

### FIXED CODE ###

N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
print(len(set(A)))
#ABC105B
N = int(input())
OK = False
for i in range(N/4):
    for j in range(N/7):
        if i*4 + j*7 == N:
            OK = True
if OK:
    print("Yes")
else:
    print("No") 

### FIXED CODE ###

#ABC105B
N = int(input())
flag = False
for i in range(30):
    for j in range(30):
        if i*4 + j*7 == N:
            flag = True
if flag:
    print("Yes")
else:
    print("No")
i = lambda:map(int,input().split())
_,_,x,y = i()
print('No War' if max(*i(),x) < min(*i(),y) else 'War')


### FIXED CODE ###


i = lambda:map(int,input().split())
_,_,x,y = i()
print('No War' if len(set(range(max(i())+1,min(i())+1)).intersection(set(range(x+1,y+1)))) > 0 else 'War')
import sys
input = sys.stdin.readline
# 整数の入力
N = int(input())
A = [int(input()) for _ in range(N)]

sum_water = sum(A)
A = A[1:] + A[0:N-2]
for i in range(N):
    tmp=sum_water
    for j in range((N-1)//2):
        tmp-=A[i+j*2]*2
    print(tmp, end="")
    print(" ", end="")

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))

sum_water = sum(A)

tmp=sum_water
for j in range((N-1)//2):
    tmp-=A[j*2+1]*2
print(tmp, end="")
print(" ", end="")

for i in A[:-1]:
    tmp = 2*i - tmp
    print(tmp, end="")
    print(" ", end="")
H,W = map(int,input().split(' '))
S = []
for i in [0]*H:
	l = input()
	S.append([1 if x == '#' else -1 for x in l])
#c = [[False]*W for i in range(H)]

def check(S,x,y):
	global bk
	global wh
	if S[y][x] == 0:
		return
	if S[y][x] == 1:
		S[y][x]=0
		if 0 < y and S[y-1][x] == -1:
			check(S,x,y-1)
		if 0 < x and S[y][x-1] == -1:
			check(S,x-1,y)
		if y < H-1 and S[y+1][x] == -1:
			check(S,x,y+1)
		if x < W-1 and S[y][x+1] == -1:
			check(S,x+1,y)
		bk += 1
	else:
		wh += 1
		S[y][x]=0
		if 0 < y and S[y-1][x] == 1:
			check(S,x,y-1)
		if 0 < x and S[y][x-1] == 1:
			check(S,x-1,y)
		if y < H-1 and S[y+1][x] == 1:
			check(S,x,y+1)
		if x < W-1 and S[y][x+1] == 1:
			check(S,x+1,y)
	return

cnt = 0
bk = 0
wh = 0
for y in range(H):
	for x in range(W):
		if S[y][x] == 1:
			check(S,x,y)
			cnt += wh*bk
			wh = 0
			bk = 0
print(cnt)




### FIXED CODE ###

import sys
sys.setrecursionlimit(200000)

H,W = map(int,input().split(' '))
S = []
for i in [0]*H:
	l = input()
	S.append([1 if x == '#' else -1 for x in l])

bk = 0
wh = 0
def check(S,x,y):
	global bk
	global wh
	if S[y][x] == 0:
		return
	if S[y][x] == 1:
		bk += 1
		S[y][x]=0
		if 0 < y and S[y-1][x] == -1:
			check(S,x,y-1)
		if 0 < x and S[y][x-1] == -1:
			check(S,x-1,y)
		if y < H-1 and S[y+1][x] == -1:
			check(S,x,y+1)
		if x < W-1 and S[y][x+1] == -1:
			check(S,x+1,y)
		return
	if S[y][x] == -1:
		wh += 1
		S[y][x]=0
		if 0 < y and S[y-1][x] == 1:
			check(S,x,y-1)
		if 0 < x and S[y][x-1] == 1:
			check(S,x-1,y)
		if y < H-1 and S[y+1][x] == 1:
			check(S,x,y+1)
		if x < W-1 and S[y][x+1] == 1:
			check(S,x+1,y)
		return

cnt = 0
for y in range(H):
	for x in range(W):
		if S[y][x] == 1:
			check(S,x,y)
			cnt += wh*bk
			wh = 0
			bk = 0
print(cnt)
s = []
ans = 0
flag = True
N, M = map(int, input().split())
for i in range(M):
    a = list(map(int, input().split()))
    if a not in s:
        s.append(a)
S = sorted(s)

for i in range(len(S)-1):
    if S[i][0]==S[i+1][0] and S[i][1]!=S[i+1][1]:
        flag = False

if N>1 and S[0]==[1,0]:
    flag = False

if flag == True:
    for i in range(len(S)):
        for j in range(N+1):
            if S[i][0]==j:
                ans+=S[i][1]*10**(N-j)
    print(ans)
            
else:
    print(-1)

### FIXED CODE ###

s = []
ans = 0
flag = True
N, M = map(int, input().split())
if M ==0:
    if N==1:
        print(0)
    elif N ==2:
        print(10)
    else:
        print(100)
else:
    for i in range(M):
        a = list(map(int, input().split()))
        if a not in s:
            s.append(a)
    S = sorted(s)

    for i in range(len(S)-1):
        if S[i][0]==S[i+1][0] and S[i][1]!=S[i+1][1]:
            flag = False

    if N>1 and S[0]==[1,0]:
        flag = False

    if flag == True:
        for i in range(len(S)):
            for j in range(N+1):
                if S[i][0]==j:
                    ans+=S[i][1]*10**(N-j)
        if N==3 and ans<100:
            ans+=100
        elif N ==2 and ans<10:
            ans+=10
        
        print(ans)
            
    else:
        print('-1')
import sys
input = sys.stdin.readline

def main():
  
  def knapsack_price(single=True):
    V = sum(price_list)
    dp_max = W + 1
    dp = [dp_max] * (V-1)
    dp[0] = 0
    
    for item in range(N):
      if single:
        S = reversed(range(price_list[item], V+1))
      else:
        S = range(price_list[item], V+1)
      
      for price in S:
        dp[price] = min(dp[price], dp[price - price_list[item]] + weight_lsit[item])
    return max(price for price in range(V+1) if dp[price] <= W)
  
  N,W = map(int,input().split())
  
  price_list = []
  weight_list = []
  for _ in range(N):
    weight, price = map(int,input().split())
    price_list.append(price)
    weight_list.append(weight)
    
  print(knapsack_price(single=True)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
  
  def knapsack_price(single=True):
    V = sum(price_list)
    dp_max = W + 1
    dp = [dp_max] * (V+1)
    dp[0] = 0
    
    for i in range(N):
      if single:
        S = list(reversed(range(price_list[i], V+1)))
      else:
        S = list(range(price_list[i], V+1))
      for price in S:
        dp[price] = min(dp[price], dp[price - price_list[i]] + weight_list[i])
    
    return max(price for price in range(V+1) if dp[price] <= W)
  
  N,W = map(int,input().split())
  
  price_list = []
  weight_list = []
  for _ in range(N):
    weight, price = map(int,input().split())
    price_list.append(price)
    weight_list.append(weight)
    
  print(knapsack_price(single=True))

if __name__ == '__main__':
    main()
a,b=map(int,input())
if(a+b%2==0):
    print('Yay!')
elif(a < 9 and b < 9):
    print('Yay!')
else:
    print(':(')

### FIXED CODE ###

a,b=map(int,input().split())
if(a+b%2==0):
    print('Yay!')
elif(a < 9 and b < 9):
    print('Yay!')
else:
    print(':(')
a=int(input())
b=[input() for i in range(a)]
c=[AC,WA,TLE,RE]
for value in c:
  e=b.count(value)
print("{0}x{1}".format(value.e)) 
  
  


### FIXED CODE ###

N = int(input())
s = [input() for i in range(N)]
for v in ['AC', 'WA', 'TLE', 'RE']:
  print('{0} x {1}'.format(v, s.count(v)))
import math
n = int(input())
s = int(input())

def f(b, n):
    if n < b:
        return n
    else:
        return f(b, n//b) + (n%b)

if n == s:
    print(n+1)
elif n < s:
    print(-1)
else:
    for b in range(2, int(math.sqrt(n)) + 1):
        if f(b, n) == s:
            print(b)
            break
    else:
        for p in range(1, int(math.sqrt(n)) + 1):
            b = int(((n-s)/p) + 1)
            if f(b, n) == s:
                print(b)
                break
        else:
            print(-1)

### FIXED CODE ###

import math
n = int(input())
s = int(input())

def f(b, n):
    ans = 0
    while b <= n:
        ans += n%b
        n //= b
    ans += n
    return ans

if n == s:
    print(n+1)
elif n < s:
    print(-1)
else:
    limit = math.ceil(math.sqrt(n))
    for b in range(2, limit + 1):
        if f(b, n) == s:
            print(b)
            break
    else:
        for p in range(limit, 0 ,-1):
            if s - p >= 0 and n-s+p > 0 and (n-s+p)//p > limit:
                b = (n-s+p)//p
                if f(b, n) == s:
                    print(b)
                    break
        else:
            print(-1)
import bisect 
n,m = map(int,input().split())
a = sorted(list(map(int,input().split())))
b = a[-1]

for i in range(m):
  if int(b/2) >= a[-2]:
    b = int(b/2)
  else:
    bisect.insort(a,b)
    b = a[-1]
    b = int(b/2)

a.append(b)
print(sum(a))

### FIXED CODE ###

import heapq

n,m = map(int,input().split())
a = list(map(int,input().split()))
a = list(map(lambda x: x*(-1), a))  # 各要素を-1倍することで，最小最大を逆転

heapq.heapify(a)#優先度付きキューに変換

for i in range(m):
  tmp = heapq.heappop(a)
  heapq.heappush(a,(-tmp//2)*(-1))#負の剰余では切り捨て方向が異なってしまうので，いったんプラスにする．
  
print(-sum(a))
import math

N,K = map(int,input().split())
A = list(map(int,input().split()))

m = 0
M = max(A)

while(M-m>0):
    mm = 0

    mid = (M+m)//2
    for i in range(N):
        mm += math.ceil(A[i]/mid)-1

    if mm>K:
        m = mid+1
    else:
        M = mid

print(math.ceil(m))




### FIXED CODE ###

import math

N,K = map(int,input().split())
A = list(map(int,input().split()))

m = 1
M = max(A)

while(M-m>0):
    mm = 0

    mid = (M+m)//2
    for i in range(N):
        mm += math.ceil(A[i]/mid)-1

    if mm>K:
        m = mid+1
    else:
        M = mid

print(math.ceil(m))
a = list(map(int,input().split()))
k = int(input())
a.sort(reverse = True)
a[0] = a*2**k
print(sum(a))


### FIXED CODE ###

a = list(map(int,input().split()))
k = int(input())
a.sort(reverse = True)
a[0] = a[0]*2**k
print(sum(a))
import sys
n = int(sys.stdin.readline())
s = sys.stdin.readline().strip()
if not n % 2:
  if s[0:n/2] == s[n/2:]:
    print('Yes')
  else:
    print('No')
else:
  print('No')

### FIXED CODE ###

import sys
n = int(sys.stdin.readline())
s = sys.stdin.readline().strip()
if not n % 2:
  if s[0:n//2] == s[n//2:]:
    print('Yes')
  else:
    print('No')
else:
  print('No')