Harryxun/stf_run huggingface dataset:
import sys
from collections import deque
sys.setrecursionlimit = 10 ** 5
input = sys.stdin.readline
N, u, v = [int(x) for x in input().strip().split()]

m = [[] for n in range(N)]
f_aokikun = [False for n in range(N)]
f_takahashikun = [False for n in range(N)]
aokikun = [0 for n in range(N)]
takahashikun = [0 for n in range(N)]
takahashikun_enable = [1 for n in range(N)]
for n in range(N-1):
    a, b = [int(x) for x in input().strip().split()]
    m[a-1].append(b)
    m[b-1].append(a)

def aokifunc(c, f, p, cost):
    c[p-1] = cost
    f[p-1] = True
    for p_next in m[p-1]:
        if f[p_next-1]:
            continue
        aokifunc(c, f, p_next, cost+1)

def takahashifunc(c, f, p, cost, aokikun, takahashikun_enable):
    if aokikun[p-1] <= cost:
        takahashikun_enable[p-1] = 0
    c[p-1] = 0
    f[p-1] = True
    for p_next in m[p-1]:
        if f[p_next-1]:
            continue
        takahashifunc(c, f, p_next, cost+1, aokikun, takahashikun_enable)

aokifunc(aokikun, f_aokikun, v, 0)
takahashifunc(takahashikun, f_takahashikun, u, 0, aokikun, takahashikun_enable)
print(max([i * j for i, j in zip(aokikun, takahashikun_enable)])-1)

### FIXED CODE ###

import sys
from collections import deque
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline
N, u, v = [int(x) for x in input().strip().split()]

m = [[] for n in range(N)]
f_aokikun = [False for n in range(N)]
f_takahashikun = [False for n in range(N)]
aokikun = [0 for n in range(N)]
takahashikun = [0 for n in range(N)]
takahashikun_enable = [1 for n in range(N)]
for n in range(N-1):
    a, b = [int(x) for x in input().strip().split()]
    m[a-1].append(b)
    m[b-1].append(a)

def aokifunc(c, f, p, cost):
    c[p-1] = cost
    f[p-1] = True
    for p_next in m[p-1]:
        if f[p_next-1]:
            continue
        aokifunc(c, f, p_next, cost+1)

def takahashifunc(c, f, p, cost, aokikun, takahashikun_enable):
    if aokikun[p-1] <= cost:
        takahashikun_enable[p-1] = 0
    c[p-1] = 0
    f[p-1] = True
    for p_next in m[p-1]:
        if f[p_next-1]:
            continue
        takahashifunc(c, f, p_next, cost+1, aokikun, takahashikun_enable)

aokifunc(aokikun, f_aokikun, v, 0)
takahashifunc(takahashikun, f_takahashikun, u, 0, aokikun, takahashikun_enable)
print(max([i * j for i, j in zip(aokikun, takahashikun_enable)])-1)
import math
r = int(input())
print("{0:.10f}".format(r*r*math.pi),"{0:.10f}".format(2*r*math.pi))

### FIXED CODE ###

import math
r = float(input())
print("{0:.10f}".format(r*r*math.pi),"{0:.10f}".format(2*r*math.pi))
S = list(input()) 
if S[0] != "A":
    S.insert(0,"A")
if S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H":
    S.insert(4,"A")
if S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H" and S[4] =="A" and S[5] =="B":
    S.insert(6,"A")
if S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H" and S[4] =="A" and S[5] =="B" and S[6] == "A" and S[7] == "R":
    S.insert(8,"A")
if   S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H" and S[4] =="A" and S[5] =="B" and S[6] == "A" and S[7] == "R" and S[8] == "A" and len(S) ==9:
    print("YES")
else:
    print("NO")

### FIXED CODE ###

import sys
S = list(input()) 
#judge =1
if S[0] != "A":
    S.insert(0,"A")
#if S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H" and S[4] !="A":
if len(S) < 4:
    print("NO")
    sys.exit()
elif S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H":
    if len (S) == 4:
        S.insert(4,"A")
    elif S[4] != "A":
        S.insert(4,"A")    
else:
    print("NO")
    sys.exit()
if len(S) <6:
    print("NO")
    sys.exit()
elif S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H" and S[4] =="A" and S[5] =="B":
    if len (S) == 6:
        S.insert(6,"A")
    elif S[6] != "A":
        S.insert(6,"A")
else:
    print("NO")
    sys.exit()
if len(S) < 8:
    print("NO")
    sys.exit()
elif S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H" and S[4] =="A" and S[5] =="B" and S[6] == "A" and S[7] == "R":
    if len (S) == 8:
        S.insert(8,"A")
    elif S[8] != "A":
        S.insert(8,"A")
else:
    print("NO")
    sys.exit()
if   S[0] =="A" and S[1] == "K" and S[2] == "I" and S[3] == "H" and S[4] =="A" and S[5] =="B" and S[6] == "A" and S[7] == "R" and S[8] == "A" and len(S) ==9:
    print("YES")
else:
    print("NO")
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math

# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def main():
    N = int(input())
    A = input().split()
    
    ans = []
    hafe = N // 2
    for i in range(N, 0, -1):
        if i >= hafe:
            if A[i-1] == "1":
                ans.append(i)
            continue
            
        cons = 0
        for j in range(i, N+1, i):
            if j in ans:
                cons += 1
        if A[i-1] == "1" and cons % 2 == 0:
            ans.append(i)
            continue
        if A[i-1] == "0" and cons % 2 == 1:
            ans.append(i)
            continue
            
    print(len(ans))
    if len(ans) != 0:
        anss = ""
        for i in ans:
            anss = anss + str(i) + " "
        
            print(anss[:-1])
            	
if __name__ == "__main__":
	main()


### FIXED CODE ###

import sys
 
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
 
N=INT()
A=LIST()
 
# その位置にボールを入れるかを保持するA2
A2=[0]*N
B=[]
# 後ろから決めていく
for i in range(N-1, -1, -1):
    cur=i+1
    cnt=0
    # curの倍数を全部見に行く
    for j in range(cur*2, N+1, cur):
        cnt^=A2[j-1]
    # ボールを入れるべきかどうか
    if A[i]!=cnt:
        B.append(cur)
        A2[i]=1
 
print(len(B))
print(*B)
n=int(input())
a = n / 1.08
if a-int(a) == 0.00:
      x = int(a)
else:
      x = int(a) + 1
v = int(x * 1.08)
if v == n:
  print(x)
else:
  print(":(")



### FIXED CODE ###

M1, D1 = map(int, input().split())
M2, D2 = map(int, input().split())

if D2-D1 == 1:
  print(0)
else:
  print(1)
C = input()
k = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,g]
for i in range(len(k)):
  if C == k[i]:
    print(k[i+1])

### FIXED CODE ###

C = input()
k = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for i in range(len(k)):
  if C == k[i]:
    print(k[i+1])
H, W = map(int, input().split())
C = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]
for k in range(5):
    for i in range(10):
        for j in range(10):
            C[i][j] = min(C[i][j], C[i][k] + C[k][j])
print(sum([sum(map(lambda a: 0 if a == -1 else C[a][1], row)) for row in A])

### FIXED CODE ###

#http://abc079.contest.atcoder.jp/
# -*- coding: utf-8 -*-

n = list(input())

if n[0]==n[1]==n[2] or n[1]==n[2]==n[3]:
    print('Yes')
else:
    print('No')
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)

N,M,K = mips()
A = [i for i in mips()]
B = [i for i in mips()]
# N,M <= 200000
ans = 0
clock = K
A2 = [0]
B2 = [0]
for i in range(1,N+1):
    ai = A2[i-1] + A[i-1]
    A2.append(ai)
for i in range(1,M+1):
    bi = B2[i-1] + B[i-1]
    B2.append(bi)
for i in range(N+1):
    j = M
    if A2[i] > K:
        break
    while (B2[j] + A[i] > K):
        j -= 1
    ans = max(ans,i+j)
print(ans)

### FIXED CODE ###

import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)

N,M,K = mips()
A = [i for i in mips()]
B = [i for i in mips()]
# N,M <= 200000
ans = 0
clock = K
A2 = [0]
B2 = [0]
for i in range(1,N+1):
    ai = A2[i-1] + A[i-1]
    A2.append(ai)
for i in range(1,M+1):
    bi = B2[i-1] + B[i-1]
    B2.append(bi)
j = M
for i in range(N+1):
    if A2[i] > K:
        break
    while (B2[j] + A2[i] > K):
        j -= 1
    ans = max(ans,i+j)
print(ans)
N = int(input())
d = [int(input()) for i in range(N)]
a = 0
b = 0
e = sorted(d)

while b == 0:
    for l in range(N):
        if e[l] < e[l+1]:
            a += 1
        else:
            b += 1

print(a)

### FIXED CODE ###

N = int(input())
d = [int(input()) for i in range(N)]
a = 0
b = 0
e = sorted(d)

while b == 0:
    for l in range(N):
        if e[l-1] < e[l]:
            a += 1
        else:
            b += 1

print(a+1)
# coding : utf-8
n = int(input())
lst = map(int, input().split())
for i in range(n-1, -1, -1):
    print ('%d ' % lst[i], end = '')
print ('')


### FIXED CODE ###

n = int(input())
lst = list(map(int, input().split()))
counter = 0
for i in (reversed(lst)):
    counter += 1
    print (i, end="")
    if counter != n:
        print ("", end=" ")
print ("")
def adx(p,oya,tox,total):
  tox[p] += total
  total = tox[p]
  if(len(oya[p]) == 0):
    return 0

  else:
    for i in oya[p]:
      adx(i,oya,tox,total)

  return 0

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

oya = [ [] for i in range(n + 1)]
tox = [0 for i in range(n + 1)]

hit = list(range(1,n+1))

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

for i in range(q):
  p,x = map(int,input().split())
  tox[p] += x


adx(1,oya,tox,0)

tox.pop(0)

print(*tox)

### FIXED CODE ###

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

def addc(p,b):
  for i in e[p]:
    if(i == b):
      continue
    c[i] += c[p]
    addc(i,p)

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

e = [ [] for i in range(n + 1)]
c = [0]*(n+1)

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

for i in range(q):
  p,x = map(int,input().split())
  c[p] += x

addc(1,-1)

print(*c[1:])
n = int(input())
d = list(map(int,input().split()))
m = int(input())
t = list(map(int,input().split()))
d.sort()
t.sort()
j = 0
for i in range(n):
    if d[i] == t[j]:
        j += 1

if j == m:
    print('YES')
else:
    print('NO')

### FIXED CODE ###

n = int(input())
d = list(map(int,input().split()))
m = int(input())
t = list(map(int,input().split()))
d.sort()
t.sort()
j = 0
for i in range(n):
    if d[i] == t[j]:
        j += 1
    if j == m:
        break

if j == m:
    print('YES')
else:
    print('NO')
n = int(input())
s = input()
x = 0
l = [0]
for n in range(n):
  if s[n] == "I":
    x += 1
  	l.append(x)
  else:
    x -= 1
    l.append(x)
print(max(l))

### FIXED CODE ###

n = int(input())
s = input()
x = 0
l = [0]
for i in range(n):
  if s[i] == "I":
    x += 1
    l.append(x)
  else:
    x -= 1
    l.append(x)
print(max(l))
import numpy as np
a,b = map(int, input().split())
grid1 = np.full((50,100), '.')
grid2 = np.full((50,100), '#')
a = a-1
b = b-1
x = y = 1
for i in range(a):
    grid2[x, y] = '.'

### FIXED CODE ###

import numpy as np
a,b = map(int, input().split())
grid1 = [['.' for j in range(100)] for i in range(50)]
grid2 = [['#' for j in range(100)] for i in range(50)]
a -= 1
b -= 1
x = y = 1

for i in range(a):
    grid2[x][y] = '.'
    y += 2
    if y != y % 100:
        x += 2
    y = y % 100
    if x >= 50 or y >= 100:break

x = y = 1
for i in range(b):
    grid1[x][y] = '#'
    y += 2
    if y != y % 100:
        x += 2
    y = y % 100
    if x >= 50 or y >= 100:break

print(100, 100)
grid = np.concatenate((grid1, grid2), axis=0)
for row in grid:
    print(''.join(row))
import bisect

n, m = map(int, input().split())
a_list = []
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        a_list.append([a, b])
a_list = sorted(a_list, key=lambda x: (x[0], -x[1]))
a_list_1 = [aa[0] for aa in a_list]
a_list_2 = [aa[1] for aa in a_list]
ans = 0
for i in range(m):
    k = bisect.bisect_right(a_list_1, m - i) - 1
    if k >= 0:
        l = len(a_list_2) - reversed(a_list_2).index(max(a_list_2[:k + 1])) - 1
        if a_list_1[l] <= m - i:
            ans += a_list_2[l]
            del a_list_1[l], a_list_2[l]
print(ans)

### FIXED CODE ###

from collections import defaultdict
import heapq

a_dict = defaultdict(lambda: [])
n, m = map(int, input().split())
for _ in range(n):
    a, b = map(int, input().split())
    a_dict[a].append(-b)
ans = 0
heap_list = []
for i in range(1, m + 1):
    for aa in a_dict[i]:
        heapq.heappush(heap_list, aa)
    if len(heap_list) > 0:
        ans -= heapq.heappop(heap_list)
print(ans)
import sys

input = sys.stdin.readline

d = []
ABC = []

def solve(i, dis):
    global d
    global ABC
    d[i] = dis

    for n, c in ABC[i]:
        if d[n] == -1:
            solve(n, dis + c)



def main():
    N = int(input())
    global ABC
    ABC = [[] for j in range(N)]
    global d
    d = [-1] * N
    for _ in range(N - 1):
        a, b, c = [int(x) for x in input().split()]
        ABC[a - 1].append((b - 1, c))
        ABC[b - 1].append((a - 1, c))

    Q, K = [int(x) for x in input().split()]

    solve(K - 1, 0)

    for _ in range(Q):
        x, y = [int(x) for x in input().split()]
        print(d[x - 1] + d[y - 1])


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

input = sys.stdin.readline

d = []
ABC = []

sys.setrecursionlimit(10 ** 8)


def solve(i, dis):
    global d
    global ABC
    d[i] = dis

    for n, c in ABC[i]:
        if d[n] == -1:
            solve(n, dis + c)


def main():
    N = int(input())
    global ABC
    ABC = [[] for _ in range(N + 1)]
    global d
    d = [-1] * (N + 2)
    for _ in range(N - 1):
        a, b, c = [int(x) for x in input().split()]
        ABC[a - 1].append([b - 1, c])
        ABC[b - 1].append([a - 1, c])

    Q, K = [int(x) for x in input().split()]

    solve(K - 1, 0)

    for _ in range(Q):
        x, y = [int(x) for x in input().split()]
        print(d[x - 1] + d[y - 1])


if __name__ == '__main__':
    main()
arr = input().split()
arr = list(map(str,arr))
if a == 'H' and b == 'H':
    print('H')
elif a == 'D' and b == 'H':
    print('D')
elif a == 'D' and b == 'D':
    print('H')
elif a == 'H' and b == 'D':
    print('D')


### FIXED CODE ###

arr = input().split()
arr = list(map(str,arr))
a = arr[0]
b = arr[1]
if a == 'H' and b == 'H':
    print('H')
elif a == 'D' and b == 'H':
    print('D')
elif a == 'D' and b == 'D':
    print('H')
elif a == 'H' and b == 'D':
    print('D')
import sys
read = sys.stdin.read
from math import gcd
from collections import deque
from operator import mul
from functools import reduce
def main():
    n = int(input())
    a = list(map(int, read().split()))

    from math import sqrt
    from math import ceil
    maxa = max(a)
    maxa2 = ceil(sqrt(maxa))
    maxa2 = max(6, maxa2)  # maxa2が6未満だとエラーになる
    p = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(maxa2)]
    p[0] = p[1] = False
    p[2] = p[3] = p[5] = True
    for is1 in range(3, maxa2, 2):
        for is2 in range(is1 ** 2, maxa2, is1):
            p[is2] = False
    prime_list = [i for i, b in enumerate(p) if b]
    div = []
    koho = -1
    for pe in prime_list:
        while True:
            check = [i % pe == 0 for i in a]
            if all(check):
                div.append(pe)
                a = [i // pe for i in a]
            else:
                if n - sum(check) == 1:
                    koho = pe
                break
    if koho > 0:
        div.append(koho)
    r = reduce(mul, div)
    print(r)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
read = sys.stdin.read
from math import gcd
def main():
    n, *a = map(int, read().split())
    if n == 1:
        print(a[0])
        sys.exit()
    l = [0] * (n + 1)
    for i1 in range(n):
        l[i1 + 1] = gcd(l[i1], a[i1])
    r = [0] * (n + 1)
    for j1 in range(n - 1, -1, -1):
        r[j1] = gcd(r[j1 + 1], a[j1])
    m = [0] * (n + 1)
    for k1 in range(n):
        m[k1] = gcd(l[k1], r[k1 + 1])
    r = max(m)
    print(r)

if __name__ == '__main__':
    main()
n = int(input())
x = list(map(int, input().split()))

sortedx = x[:]
sortedx.sort()

for i in range(n):
    #ix = sortedx.index(x[i])
    #print(sortedx)
    #print(ix)
    if x[i] <= sortedx[n//2-1]:
        print(sortedx[n//2])
    else:
        print(sortedx[n//2-1])

### FIXED CODE ###

a, b, x = list(map(int, input().split()))

if a>x:
    print('NO')
elif a+b<x:
    print('NO')
else:
    print('YES')
s=input()
t=input()
n=len(s)
m=len(t)
dp=[[0 for i in range(m+1)] for j in range(n+1)]
​
​
#メイン
​
#dp[i][j]:=s_1~s_iとt_1~t_jに対するLCSの長さ
for i in range(n):
    for j in range(m):
        if s[i]==t[j]:
            dp[i+1][j+1]=dp[i][j]+1 #同じならくっつけられる
        else:
            dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j])
#print(dp[n][m])
#dpを逆からたどって文字列を復元
ans = ''
while(dp[n][m]>0):
  if dp[n][m] == dp[n-1][m-1] +1 and dp[n][m] == dp[n-1][m] +1 and dp[n][m] == dp[n][m-1] +1:
    ans += s[n-1]
    n -= 1
    m -= 1
  elif dp[n-1][m] > dp[n][m-1]:
    n -= 1
  else:
    m -= 1
print(''.join(reversed(list(ans))))

### FIXED CODE ###

s=input()
t=input()
n=len(s)
m=len(t)
dp=[[0 for i in range(m+1)] for j in range(n+1)]
for i in range(n):
    for j in range(m):
        if s[i]==t[j]:
            dp[i+1][j+1]=dp[i][j]+1
        else:
            dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j])
ans = ''
while(dp[n][m]>0):
  if dp[n][m] == dp[n-1][m-1] +1 and dp[n][m] == dp[n-1][m] +1 and dp[n][m] == dp[n][m-1] +1:
    ans = ''.join([s[n-1],ans])
    n -= 1
    m -= 1
  elif dp[n-1][m] > dp[n][m-1]:
    n -= 1
  else:
    m -= 1
print(ans)
from collections import deque
sin = [i for i in input()]
tin = [i for i in input()]
s = deque(sin)
s_origin = s.copy()
t = deque(tin)
s.rotate(1)
while s!=s_origin:
    if s == t or s_origin == t:
        print("Yes")
        break
    s.rotate(1)
else:
    print("No")

### FIXED CODE ###

from collections import deque
sin = [i for i in input()]
tin = [i for i in input()]
s = deque(sin)
s_origin = deque(sin)
t = deque(tin)
s.rotate(1)
while s!=s_origin:
    if s == t or s_origin == t:
        print("Yes")
        break
    s.rotate(1)
else:
    if s_origin == t:
        print("Yes")
    else:
        print("No")
S = input()
MOD = 2019
cnt = [0]*MOD
cur = 0 # 現在考えている部分文字列
cnt[cur] = 1 # 部分文字列'0'も作れると考える
d = 1 # 位を表す
for s in for S[::-1]:
    cur += int(s)*d # 新たな桁を追加
    r %= MOD
    cnt[cur] += 1
    # 後処理
    d *= 10 # 位を上げる
    d %= MOD # ただし数が巨大になるため、剰余をとる
ans = 0
for c in cnt:
    ans += c*(c-1)//2
print (ans)



### FIXED CODE ###

import sys
input = sys.stdin.readline

sys.setrecursionlimit(10**7)
INF = 10**10

N = 2019
cnt = [0] * N
#print (cnt)

def dec(cnt,s):
    new = [0] * N
    for n in range(N):
        idx = (10 * n + s) % N
        new[idx] += cnt[n]
    return new


S = input()[:-1]
ans = 0
for s in S:
    s = int(s)
    cnt = dec(cnt,s)
    cnt[s] += 1
    ans += cnt[0]
    # if cnt[0] != 0:
    #     print (cnt[0])
print (ans)
from decimal import *
a,b,c = map(int,input().split())
getcontext().prec = 100
print("Yes" if sqrt(a) + sqrt(b) < sqrt(c) else "No")

### FIXED CODE ###

a, b, c = map(int, input().split())
if c - a - b > 0 and 4 * a * b < (c - a - b) ** 2: print("Yes")
else: print("No")
nums={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
while True:
    try:
        s=input()
    except:
        break
    ans=nums[s[0]]
    for i in range(1,len(s)):
        if num[s[i-1]]<num[s[i]]:
            ans+=num[s[i]]-2*num[s[i-1]]
        else:
            ans+=num[s[i]]
    print(ans)
        

### FIXED CODE ###

nums={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
while True:
    try:
        s=input()
    except:
        break
    ans=nums[s[0]]
    for i in range(1,len(s)):
        if nums[s[i-1]]<nums[s[i]]:
            ans-=2*nums[s[i-1]]
        ans+=nums[s[i]]
    print(ans)
import math
def f(b,n):
    if n<b:
        return n
    return f(b,n//b)+n%b

def main():
    n=int(input())
    s=int(input())
    if n==s:
        print(n+1)
        exit()
    elif n <s:
        print(-1)
        exit()
    else:
        for b in range(2,int(math.sqrt(n))+2):
            if f(b,n)==s:
                print(b)
                exit()
        for j in range(int(math.sqrt(n)+1),0,-1):
            b=(n-s)//j+1
            if f(b,n)==s:
                print(int(b))
                exit()
    print(-1)

if __name__ == '__main__':
    main()


### FIXED CODE ###

n = int(input())
s = int(input())

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


def solve(n,s):
  if s > n:
    return -1
  if s == n:
    return n+1
  nsq = int(n**0.5 + 1)
  for b in range(2,nsq+1):
    if f(b,n) == s:
      return b
  # 2桁で作れるかどうか
  # n = bx+y, s = x+y, x,y < b
  # n - s = (b-1)x, x < b
  m = n-s
  msq = int(m**0.5+1)
  for x in range(msq,0,-1):
    if m%x:
      continue
    b = m//x+1
    if f(b,n) == s:
      return b
  return -1
  
answer = solve(n,s)
print(answer)
a,b,c,d = map(int, input().split())
print("Bad" if a==b or b==c or c==d else "Good")

### FIXED CODE ###

a,b,c,d = input()
print("Bad" if a==b or b==c or c==d else "Good")
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

h,w,d = li()
a = [list(li()) for _ in range(h)]

num2point = [(-1,-1)]*(h*w+1)
for r, ai in enumerate(a):
    for c, aij in enumerate(ai):
        num2point[aij] = (r, c)
    

dist = [[] for _ in range(d)]
for num in range(h*w):
    i = num+1
    if i // d == 0:
        dist[i%d].append(0)
        
    else:
        dist[i%d].append(abs(num2point[i][0]-num2point[i-d][0]) + abs(num2point[i][1]-num2point[i-d][1]))
        
dist_cum = [list(accumulate(dist_row)) for dist_row in dist]


q = ni()
for _ in range(q):
    l,r = li()
    print(dist_cum[l%d][r//d] - dist_cum[l%d][l//d])

### FIXED CODE ###

import sys
sdin = sys.stdin.readline

H,W,D = map(int, sdin().split())
A = []
for h in range(H):
    A.append(list(map(int, sdin().split())))
Q = int(sdin())
RL = []
for q in range(Q):
    RL.append(tuple(map(int, sdin().split())))


codn = {}
for h in range(H):
    for w in range(W):
        codn[A[h][w]] = (h+1, w+1)
        
# mod D上での累積和
cums = []
for d in range(D):
    cum = []
    res = D
    if d!=0:
        res = d
    
    cum = [0]
    while res+D <= H*W:
        prev = codn[res]
        nex = codn[res+D]
        cost = abs(nex[0]-prev[0]) + abs(nex[1]-prev[1])
        cum.append(cum[-1]+cost)
        
        res += D
    
    cums.append(cum)
    
# 各クエリの処理
ans = 0
for query in range(Q):
    q = RL[query]
    mod = q[0] % D
    start = (q[0]-mod) // D
    last = (q[1]-mod) // D
    if mod == 0:
        start -= 1
        last -= 1
    
    ans = cums[mod][last] - cums[mod][start]   
    
    print(ans)
N, K = [int(x) for x in input().strip().split(" ")]
hs = [int(x) for x in input().strip().split(" ")]
d = [0] * N

for i in range(N-1):
    i = i+1
    tmp = pow(10, 18)
    for j in range(K):
        j = j + 1
        if i-j < 0:
            continue
        tmp = min(tmp, d[i-j] + abs(hs[i-j] - hs[i]))
    d[i] = tmp
print(d[-1])


### FIXED CODE ###

N, = [int(x) for x in input().strip().split(" ")]
hs = [int(x) for x in input().strip().split(" ")]
d = [0] * N
d[1] = abs(hs[0] - hs[1])

for i in range(N-2):
    i = i+2
    d[i] = min(d[i-1] + abs(hs[i-1] - hs[i]), d[i-2] + abs(hs[i-2] - hs[i]))

print(d[-1])