Harryxun/stf_run huggingface dataset:
import sys
N=int(sys.stdin.readline())
H=list(map(int,sys.stdin.readline().split()))

dp=[0]*N
def rec(i):
    if i==N-1:
        return 0
    elif i==N-2:
        return abs(H[i]-H[i+1])
    return min(rec(i+1)+abs(H[i]-H[i+1]),rec(i+2)+abs(H[i]-H[i+2]))
print(rec(0))

### FIXED CODE ###

import sys
N=int(sys.stdin.readline())
H=list(map(int,sys.stdin.readline().split()))

dp=[0]*N

dp[1]=abs(H[0]-H[1])
for i in range(2,N):
    dp[i]=min(dp[i-1]+abs(H[i-1]-H[i]),dp[i-2]+abs(H[i-2]-H[i]))
print(dp[-1])
import sys
input = sys.stdin.readline

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

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import math
n=int(input())
a=list(map(int,input().split()))
for i in range(n-1):
    a[i+1]=math.gcd(a[i],a[i+1])
print(min(a))

### FIXED CODE ###

import sys
input = sys.stdin.readline

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

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import fractions
n=int(input())
a=list(map(int,input().split()))
for i in range(n-1):
    a[i+1]=fractions.gcd(a[i],a[i+1])
print(min(a))
import math
N, K = map(int, input().split())

def main()
    plist = [i - 1 for i in map(int, input().split())]
    slist = list(map(int, input().split()))
    max_score = -math.inf
    for start in range(N):
        total_score_in_loop = 0
        now = start
        score_loop = []
        while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        score_rem = 0
        loop_score = 0
        for index, score in enumerate(score_loop, start=1):
            score_rem += score
            if total_score_in_loop > 0:
                loop_score = total_score_in_loop * ((K-index) // len(score_loop))
            max_score = max(max_score, score_rem+loop_score)
    print(max_score)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import numpy as np
from numba import njit

@njit('i8(i8,i8,i8[:],i8[:],i8)', cache=True)
def main(N, K, plist, slist, max_score):
    for start in range(N):

        total_score_in_loop = 0
        score_loop = []
        now = start
        while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        
        rem_score, loop_score = 0, 0
        for idx in range(1, len(score_loop)+1):
            rem_score += score_loop[idx-1]
            if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
            max_score = max(max_score, rem_score + loop_score)
    return max_score


N, K = map(int, input().split())
plist = np.array([i-1 for i in map(int, input().split())], dtype='int64')
slist = np.array([i for i in map(int, input().split())], dtype='int64')
max_score = -10**15
max_score = main(N, K, plist, slist, max_score)
print(max_score)
s=input()
k=int(input())
for a,i in enumerate(s):
    if i!="1":
        x=a
        y=i
        break

if x<=k: print(y)
else: print(1)


### FIXED CODE ###

s=input()
k=int(input())
x=-1
y=""
for a,i in enumerate(s):
    if i!="1":
        x=a
        y=i
        break

if x!=-1 and x+1<=k: print(y)
else: print(1)
sp_list = list(map(int, input.split()))

if 500 * sp_list[0] >= sp_list[1]:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

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

if 500 * sp_list[0] >= sp_list[1]:
  print("Yes")
else:
  print("No")
s = input()
t = input()

if sorted(s) < list(reversed(t)):
    print('Yes')
else:
    print('No')

### FIXED CODE ###

a, b = map(int, input().split())
import math
print(math.ceil((a + b)/2))
import sys
sys.setrecursionlimit(int(1e+10))
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

counters = [0 for _ in range(N)]
for i in range(Q):
    counters[px[i][0]-1] += px[i][1]

results = [0 for _ in range(N)]

def repeat(node, counter, pre_node):
    counter += counters[node]
    for candidate in branch[node]:
        if candidate == pre_node:
            continue
        else:
            repeat(candidate, counter, node)
    results[node] = counter
    return
    
repeat(0, 0, 0)

print((" ").join([str(i) for i in results]))

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

counters = [0 for _ in range(N)]
for i in range(Q):
    counters[px[i][0]-1] += px[i][1]
results = [0 for _ in range(N)]

seen = [False for _ in range(N)]
from collections import deque
seen[0] = True
todo = deque([0])
results[0] = counters[0]

while len(todo)>0:
    checking = todo.pop()
    candidates = branch[checking]
    for candidate in candidates:
        if seen[candidate] == False:
            todo.append(candidate)
            seen[candidate] = True
            results[candidate] = results[checking]+counters[candidate]


print((" ").join([str(i) for i in results]))
while True:
    try:
        N = int(raw_input())
        p = 0
        b = [0,0,0]
        o = 0
        for i in range(N):
            e = raw_input()
            if e == 'HIT':
                if b[-1] == 1:
                    p += 1
                b[0],b[1],b[2] = 1,b[0],b[1]
            elif e == 'HOMERUN':
                p += sum(b)+1
                b = [0,0,0]
            elif e == 'OUT':
                o += 1
                if o == 3:
                    print p
                    p = 0
                    b = [0,0,0]
                    o = 0
    except EOFError:
        break

### FIXED CODE ###

for i in range(int(input())):
    point = 0
    out = 0
    runner = [0, 0, 0]
    while True:
        event = input()
        if event == 'HIT':
            if runner[2] == 1:
                point += 1
                runner[2] = 0
            if runner[1] == 1:
                runner[2] = 1
                runner[1] = 0
            if runner[0] == 1:
                runner[1] = 1
            runner[0] = 1
        elif event == 'HOMERUN':
            point += (sum(runner)+1)
            runner = [0, 0, 0]
        elif event == 'OUT':
            out += 1
            if out == 3:
                print(point)
                break
n,k = map(int,input().split())
a = list(map(int, input().split()))
ns = [0] * (n+1)
tree = []

ns = [0] * n
ns[0] = 1
# max 2*10**5
# どこかでループしてる
nexti = 0
starti = 0
endi = 0
tree.append(nexti)

while  k > 0 :
    nexti = a[nexti] - 1
    if ns[nexti] == 1:
        starti = nexti
        break
    ns[nexti] = 1
    endi = nexti
    tree.append(nexti)

x = tree.index(starti) 
# y = tree[starti:]
y = len(tree[x:])

if k - x <= 0:
    print(tree[k-1])
else:
    k = k - x
    z = k % y
    if starti == 0:
        starti = -1
    temp = tree[starti+1:]

    print(temp[z] + 1,temp,z)

### FIXED CODE ###

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    num = 1
    l = []
    s = [1]*n

    while s[num-1]:
        l.append(num)
        s[num-1] = 0
        num = a[num-1]

    if l.index(num) <= k:
        l2 = l[l.index(num):]
        kk = (k-len(set(l)-set(l2)))%len(l2)
        print(l2[kk])
    else:
        print(l[k])

main()
n = int(input())
d = [int(i) for i in input().split()]
d.sort()
s=0
N=int(n/2)
k=int(abs(d[N]-d[N-1]))
K=(d[N]+d[N-1])/2)
for i in range(n):
    if d[i]>=K:
        s=s+1
    else:
        s=s
if s==N:
    print(k)
else:
    print(0)

### FIXED CODE ###

n = int(input())
d = [int(i) for i in input().split()]
d.sort()
s=0
N=int(n/2)
k=int(abs(d[N]-d[N-1]))
K=(d[N]+d[N-1])/2
for i in range(n):
    if d[i]>=K:
        s=s+1
    else:
        s=s
if s==N:
    print(k)
else:
    print(0)
import numpy as np
 
a,b=map(int,input().split())
A=np.array(sorted(list(map(int,input().split()))))
F=np.array(sorted(list(map(int,input().split())),reverse=True))
 
s=A.sum()
 
x=-1
y=10**12
while x+1<y:
  mid=(x+y)//2
  if s-np.minimum(A,mid//F).sum()<=b:
    y=mid
  else:
    x=mid
print(y)

### FIXED CODE ###

a,b=map(int,input().split())
if a <= 9 and b <= 9:
  print(a*b)
else:
  print(-1)
N, T = map(int, input().split())

l = []

for i in range(N):
    a = list(map(int, input().split()))
    if a[1] <= T:
        l.append(a[0])

print('TLE' if len(l) == 0 else min(l))


### FIXED CODE ###

n = int(input())
if n == 1:
    print('Hello World')
else:
    a, b = [int(input()) for _ in range(2)]
    print(a+b)
a,b,k=map(int,input.split())
printf(a-min(a,k),b-min(b,a-min(a,k))

### FIXED CODE ###

a,b,k=map(int,input().split())
print(a-min(a,k),b-min(b,k-min(a,k)))
X = int(input())

for A in range(100):
  for B in range(100):
    if A**5 - B**5 == X:
      print(str(A) + " " + str(B))
  break
    if A**5 + B**5 == X:
      print(str(A) + " -" + str(B))
  break


### FIXED CODE ###

X = int(input())
for A in range(-120, 120):
  for B in range(-120, 120):
    if A**5 - B**5 == X:
      print(str(A) + " " + str(B))
      quit()
def main():
    import sys
    input = sys.stdin.buffer.readline
    n = int(input())
    mod = 10**9 + 7
    C = []
    for i in range(n):
        a,b = map(int, input().split())
        if b == 0:
            C.append(0)
        C.append(a/b)
    import numpy as np
    C = np.array(C)
    C_p = C[C>=0]
    C_n = C[C<0]
    from collections import Counter as cc
    C_p,C_n = cc(C_p),cc(C_n)
    count= 0
    re = 1
    for c in C_p:
        if c == 0:
            re *= C_p[c]
            count += C_p[c]
            continue
        
        if -1/c in C_n:
            count += C_n[-1/c] + C_p[c]
            re *= (2**C_n[-1/c] + 2**C_p[c] -1)%mod
    ans = pow(2,n-count,mod)*re%mod -1
    print(ans)


if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import Counter as cc
    from fractions import gcd
    n = int(input())
    mod = 10**9 + 7
    C_p,C_n = cc(),cc()
    count= 0
    count_zero_a = count_zero_b = 0
    ans = 1
    for i in range(n):
        a,b = map(int, input().split())
        if a == 0 and b== 0:
            count += 1
        else:
            gcd_ab = gcd(a,b)
            a //= gcd_ab
            b //= gcd_ab
            if b == 0:
                count_zero_b += 1
            elif a == 0: 
                count_zero_a += 1
            else:
                if a/b>0:
                    C_p[a,b] += 1
                else:
                    C_n[a,b] += 1
    count_zero = count
    ans *= (2**count_zero_a + 2**count_zero_b -1)%mod
    count += count_zero_a + count_zero_b    
    for a,b in C_p:
        if (-b,a) in C_n:
            count += C_n[-b,a] + C_p[a,b]
            ans *= (2**C_n[-b,a] + 2**C_p[a,b] -1)%mod
    ans *= pow(2,n-count,mod)%mod
    ans = (ans +count_zero -1) % mod
    print(ans)


if __name__ == '__main__':
    main()
N = int(input())
A1 = list(map(int,input().split()))
A2 = list(map(int,input().split()))
A3=[]
A4=[0]
for i in range(N):
  A3.append(sum(A1[0:i]))
for i in range(N):
  A4.append(sum(A2[0:i]))
ans=0
for i in range(N):
  ans = max(ans, A3[:i]+A4[i:])
print(ans)


### FIXED CODE ###

N = int(input())
A1 = list(map(int,input().split()))
A2 = list(map(int,input().split()))

A3=[sum(A1[0:i+1]) for i in range(N)]
A4=[0] + [sum(A2[0:i+1]) for i in range(N)]
ans=0
for i in range(N):
  ans = max(ans, A3[i]+A4[N]-A4[i])
print(ans)
src = [int(input()) for _ in range(6)]
t = min(src[1:])
	if src[0]%t:
		print(4 + src[0]//t + 1)
	else:
		print(4 + src[0]//t)

### FIXED CODE ###

N = int(input())
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())
 
min = min(A, B, C, D, E)
 
ans = 4 + int(N/min)
if N % min != 0:
    ans = ans + 1
print(ans)
n = int(input())  # nは入力回数
str_list = [input() for _ in range(n)]


a=0
b=0
c=0                
d=0                
                
for num in range(1, n):
    if str_list[num] == 'AC':
    	a += 1
    elif str_list[num] == 'WA':
    	b += 1
    elif str_list[num] == 'TLE':
    	c +=1
    else :
    	d +=1

print('AC x'+ a)
print('WA x'+ b)
print('TLE x'+ c)
print('RE x'+ d)


### FIXED CODE ###

n = int(input())
str_list = [input() for _ in range(n)]


a=0
b=0
c=0                
d=0                
                
for i in range(n):
    if str_list[i] == 'AC':
    	a += 1
    elif str_list[i] == 'WA':
    	b += 1
    elif str_list[i] == 'TLE':
    	c +=1
    else :
    	d +=1

print('AC x '+ str(a))
print('WA x '+ str(b))
print('TLE x '+ str(c))
print('RE x '+ str(d))
print(input(),count("1"))

### FIXED CODE ###

print(input().count("1"))
from operator import mul
from functools import reduce

n = int(input().rstrip())
nums = map(int,input().rstrip().split(' '))

print(reduce(mul, nums))

### FIXED CODE ###

list = input().rstrip().split(' ')
a = int(list[0])
b = int(list[1])
print(a*b)
[N,L] = [int(x) for x in input().split()]
A = {}
ANS = {}
count = 0
for i,x in enumerate(input().split()):
    i = int(i)
    x = int(x)
    if x in A : A[x] += [i]
    else : A[x] = [i]
for i in sorted(A) :
    for j in A[i] :
        for k in range(L) :
            if j + k not in ANS :
                ANS[j+k] = i
                count += 1
    if count > N + 30 : break
for i in range(L-1,N-1) : print(ANS[i], end=" ")
print(ANS[N-1])

### FIXED CODE ###

def ascend(a,b):
    mini = min(A[a:a+L])
    ans[a] = mini
    for i in range(a+1,b):
        if A[i+L-1] <= mini : mini = A[i+L-1]
        elif A[i-1] == mini : mini = min(A[i:i+L])
        ans[i] = mini

def descend(a,b):
    mini = min(A[b-1:b+L-1])
    ans[b-1] = mini
    for i in range(b-2,a-1,-1):
        if A[i] <= mini : mini = A[i]
        elif A[i+L] == mini: mini = min(A[i:i+L])
        ans[i] = mini
    
[N,L] = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = [None]*(N-L+1)
count = 0;
if N > 10 ** 4:
    for i in range(100):
        if A[i+1] > A[i] : count += 1
    if count > 80 : descend(0, N-L+1)
    else : ascend(0,N-L+1)
else :
    ascend(0, N-L+1)
for i in ans[:-1] : print(i, end=" ")
print(ans[-1])
import fractions
  
def lcm(a,b):
  return (a * b) // fractions.gcd(a, b)

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

if M > N:
  N,M = M,N
  S,T = T,S

X = lcm(N,M)

n = N // fractions.gcd(N,M)
m = M // fractions.gcd(N,M)

for i in range(0,X + 1,m):
  if S[0] != T[0]:
    print(-1)
    break
  elif i % n == 0:
    if S[n] != T[m]:
      print(-1)
      break
else:
  print(X)


### FIXED CODE ###

import fractions
  
def lcm(a,b):
  return (a * b) // fractions.gcd(a, b)

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

if M > N:
  N,M = M,N
  S,T = T,S

X = lcm(N,M)

n = N // fractions.gcd(N,M)
m = M // fractions.gcd(N,M)

for i in range(0,X,m):
  if S[0] != T[0]:
    print(-1)
    break
  elif i % n == 0:
    s = i // n
    t = i // m
    if S[t] != T[s]:
      print(-1)
      break
else:
  print(X)
# -*- coding: utf-8 -*-
import math

if __name__ == '__main__':
    N = int(input())
    T = []
    for i in range(N):
        T.append(int(input()))

    a = T[0]
    for i in range(1,N):
        A = math.gcd(a, T[i])
        a = a * T[i] // A

    print(a)


### FIXED CODE ###

import fractions
N = int(input())
T = list(int(input()) for _ in range(N))
ans = T[0]
for i in range(1, N):
    ans = ans * T[i] // fractions.gcd(ans, T[i])
print(ans)
import math
N = int(input())
t_lcm = int(input())
for i in range(N-1):
  t = int(input())
  t_lcm = (t_lcm*t)//fractions.gcd(t,t_lcm)
print(t_lcm)



### FIXED CODE ###

import fractions
n=int(input())
ans=int(input())
for i in range(n-1):
  t=int(input())
  ans=(ans*t)//fractions.gcd(t,ans)
print(ans)
import sys
n = int(input())
for i in range(26):
  if (n - 4 * i ) % 7 or n-4*a>=0:
    continue
  else:
    print("Yes")
    sys.exit()
print("No")


### FIXED CODE ###

import sys
n = int(input())
for i in range(26):
  if (n - 4 * i ) % 7 or n - 4 * i < 0:
    continue
  else:
    print("Yes")
    sys.exit()
print("No")
N = int(input())
c = [int(x) for x in input().split()]

a = N

for i in N - 2:
  if c[i]==c[i+1]:
    a -= 1

print(a)

### FIXED CODE ###

N = int(input())
c = [str(x) for x in input()]

a = N
for i in range(N-1):
  if c[i]==c[i+1]:
    a -= 1
print(a)
N, M, K = [int(x) for x in input().strip().split()]
A = [int(x) for x in input().strip().split()]
B = [int(x) for x in input().strip().split()]
i = j = 0
ct = 0
while k > 0: 
  if A[i] < B[j]: 
    k -= A[i]
    i +=1
  else: 
    k -= B[j]
    j +=1
  ct += 1
print(ct)
    

### FIXED CODE ###

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
a, b = [0], [0]
for i in range(N):
	a.append(a[i]+A[i])
for i in range(M):
	b.append(b[i]+B[i])
	
ans, j = 0, M
for i in range(N+1):
	if a[i] > K:
		break
	while b[j] > K - a[i]:
		j -= 1
	ans = max(ans, i+j)
	
print(ans)
m = list(map(int, input().split()))
A = m[0]
B = m[1]
K = m[2]

g = list(range(A, B+1))
if len(g) <= 2*K:
    for v in g:
        print(v)
else:
    for i in range(K):
        print(g[i])
    for i in range(len(g)-K, len(g)):
        print(g[i])



### FIXED CODE ###

m = list(input())
if len(set(m)) == 3:
    print("Yes")
else:
    print("No")
import sys
import numpy as np
input = sys.stdin.readline

MOD=10**9+7
N, K = map(int, input().split())
grid = np.empty((0,N), dtype=Object)
for i in range(N):
  array = list(map(int, input().strip().split()))
  array2 = np.array(array)
  grid = np.append(grid,[array2],axis=0)
#print(grid)
dp=[[grid]]
gridsave=grid
i=1
while 2**i<=K:
  #print(i)
  #dp.insert(1,grid*grid)
  gridsave=np.dot(dp[i-1][0],dp[i-1][0])
  gridsave=np.mod(gridsave,MOD)
  #print(gridsave)
  dp.append([gridsave])
  i+=1
i-=1
K-=2**i
i-=1
#print('dp',dp)
grid1=dp[len(dp)-1][0]
#print(grid1)
#print(i)
while K>0:
  if K>=2**i:
    #print('i',i)
    grid1=np.dot(grid1,dp[i][0])
    grid1=np.mod(grid1,MOD)
    K-=2**i
    #print('grid1',grid1)
  i-=1
  #print('K',K)
ans=0
#print(grid1)
for i in range(N):
  for j in range(N):
    ans=(ans+grid1[i][j])%MOD
print(ans)


### FIXED CODE ###

import sys
import numpy as np
input = sys.stdin.readline

MOD=10**9+7
N, K = map(int, input().split())
grid = np.empty((0,N), dtype='O')
for i in range(N):
  array = list(map(int, input().strip().split()))
  array2 = np.array(array)
  grid = np.append(grid,[array2],axis=0)
#print(grid)
dp=[[grid]]
gridsave=np.eye(N,dtype="O")
#print(gridsave)
i=1
while K:
  if K & 1:
    gridsave=np.dot(grid,gridsave) % MOD
  grid=np.dot(grid,grid) % MOD
  K>>=1
ans=np.sum(gridsave)%MOD
#print(gridsave)
print(ans)
import random

def calc(D, s, t):
  last = [0 for _ in range(26)]
 
  value = 0
  for i in range(D):
    day = i + 1
    value += s[i][t[i]-1]
    last[t[i]-1] = day
    for j in range(26):
      value -= c[j] * (day - last[j])
  return value

D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
t = random.sample(range(1, 27), k=D)

val = calc(D, s, t)
for _ in range(1000):
  d_rand = random.randrange(1, D+1)
  q_rand = random.randrange(1, 27)
  old = t[d_rand-1]
  t[d_rand-1] = q_rand
  if val > calc(D, s, t):
    t[d_rand-1] = old
  else:
    val = calc(D, s, t)
for i in range(D):
  print(t[i]+1)

### FIXED CODE ###

D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]

for i in range(D):
  print(s[i].index(max(s[i]))+1)
import sys

def bigsmall(x,y):
    return x,y if x>y else y,x

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

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    if a[0] < 0 and a[-1] > 0:
        first = a.pop(-1)
        a.insert(0,first)

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[1])
    if a[2] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    for i in range(2, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    if a[-1] <= 0:
        ops.append((curr, a[-1]))
        curr = curr - a[-1]
    else:
        ops.append((a[-1], curr))
        curr = a[-1] - curr

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

### FIXED CODE ###

import sys

def bigsmall(x,y):
    return (x,y) if x>y else (y,x)

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

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[-1])
    if a[1] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    del a[-1]
    a.append(-1) # to set answer > 0

    for i in range(1, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

homon = [-1] * n

current = 0
st_loop = -1
for i in range(n): 
    if homon[current] == -1:
        homon[current] = i
        current = a[current] - 1
    else:
        #print(current)
        break

#print(homon)
nitta = len([t for t in homon if t != -1])
nltn = current
nlty = nitta - nltn

if k <= nitta:
    print(current + 1)
else:
    k -= nltn
    print(homon.index(k%nlty) + 1)



### FIXED CODE ###

#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

ord_ = [-1] * (n + 1)
s = []
v = 1
while ord_[v] == -1:
    ord_[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - ord_[v]
l = ord_[v]

if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l+k])
while True:
    a = input()
    print('a')

### FIXED CODE ###

while True:
    try:
        n,m = list(map(int,input().split()))
        if n == 0 and m == 0:
            break
        amari = n % m
        yasai = list(map(int,input().split())) 
        yasai.sort()

        s = 0
        for i in range(n):
            if (i+1) % m > 0:
                s += yasai[-i-1]
        print(s)
    except:
        pass
# 初期入力
NN,K = (int(x) for x in input().split())

#K個の和、初項と修項
p =10**9 + 7
seq =[i for i in range(NN+1)] #0-Nまでの数列
ans =[0] *NN
st =[0]*NN
fi =[0]*NN
st[0] =sum(seq[  :K])
fi[0] =sum(seq[-K: ])
ans[0] =fi[0] -st[0] +1
if NN==1:
    print(ans[0])
else:
    for i in range(NN -K+1):
        st[i+1] =st[i] +seq[K+i]
        fi[i+1] =fi[i] +seq[-K-i-1]
        ans.append(fi[i+1] -st[i+1] +1)
    print(sum(ans) %p)

### FIXED CODE ###

# 初期入力
NN,K = (int(x) for x in input().split())

#K個の和、初項と修項
p =10**9 + 7
seq =[i for i in range(NN+1)] #0-Nまでの数列
ans =[0] *(NN+1)
st =[0]*(NN+1)
fi =[0]*(NN+1)
st[0] =sum(seq[  :K])
fi[0] =sum(seq[-K: ])
ans[0] =fi[0] -st[0] +1
if NN==1 and K==1:
    print(3)
else:
    for i in range(NN -K+1):
        st[i+1] =st[i] +seq[K+i]
        fi[i+1] =fi[i] +seq[-K-i-1]
        ans[i+1] =(fi[i+1] -st[i+1] +1)
    print(sum(ans) %p)