Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

n = int(input().strip())
A = input().strip().split(' ')
ans = ''
for i,a in enumerate(A):
  if i == 0:
    ans = a
  elif i%2 == 0:
    ans += ','+a
  else:
    ans = a+','+ans
if n%2 == 0:
  print(*ans.split(','))
else:
  print(*ans.split(',')[::-1])



### FIXED CODE ###

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

odd = A[::2]
even = A[1::2]

if N % 2 == 0:
  ans = even[::-1]+odd
else:
  ans = odd[::-1]+even

print(*ans)
### BUGGY CODE ###

n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]
for i in range(n):
    a.append(a[i] + A[i])
for i in range(m):
    b.append(b[i] + B[i])
ans = 0

for i in range(n+1):
    for j in range(m+1):
        if a[i] + b[j] <= k:
            ans = max(ans, i+j)
        
print(ans)
            

### FIXED CODE ###

n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]
for i in range(n):
    a.append(a[i] + A[i])
for i in range(m):
    b.append(b[i] + B[i])

ans = 0

for i in range(n + 1):
    if a[i] > k:
        break
    while  a[i] + b[m] > k:
        m -= 1
    ans = max(ans, i + m)

print(ans)
### BUGGY CODE ###

n, k = map(int, input().split())
A = list(map(int, input().split()))

root = [A[0]]

if k<= n:
    for i in range(k-1):
        root.append(A[root[-1]-1])
    print(root[-1])
    exit()
else:
    for i in range(n-1):
        next = A[root[-1]-1]
        if not next in root:
            root.append(next)
        else:
            index = root.index(next)
            f = root[:index]
            loop = root[index:]
    len_f = len(f)
    len_loop = len(loop)
    print(loop[(k-len_f)%len_loop-1])

### FIXED CODE ###

n, k = map(int, input().split())
A = list(map(int, input().split()))

now = A[0]
root_set = set([now])
root_array = [now]
if k<= n:
    for i in range(k-1):
        now = A[now-1]
        root_set.add(now)
        root_array.append(now)
    print(root_array[-1])
else:
    for i in range(n-1):
        now = A[now-1]
        if not now in root_set:
            root_set.add(now)
            root_array.append(now)
        else:
            index = root_array.index(now)
            f = root_array[:index]
            loop = root_array[index:]
            break
    len_f = len(f)
    len_loop = len(loop)
    print(loop[(k-len_f)%len_loop-1])
### BUGGY CODE ###

# -*- coding: utf-8 -*-
n = int(input())
a = [int(input()) for _ in range(n)]
a_unique = list(sorted(set(a), reverse=True))
max_value = max(a_unique)
for i in range(n):
    current_value = a.pop(i)
    if max_value in a:
        print(max_value)
    else:
        print(a_unique[1])
    a.insert(i, current_value)


### FIXED CODE ###

# -*- coding: utf-8 -*-
n = int(input())
a = [int(input()) for _ in range(n)]
a_unique = list(sorted(set(a), reverse=True))
max_value = a_unique[0]
max_value_count = a.count(max_value)

if max_value_count == 1:
    for i in range(n):
        if a[i] == max_value:
            print(a_unique[1])
        else:
            print(max_value)
else:
    for i in range(n):
        print(max_value)
### BUGGY CODE ###

def isort(A, n, g):
    global cnt
    for i in range(g, n):
        v = A[i]
        j = i - g
        while j >= 0 and A[j] > v:
            A[j + g] = A[j]
            j -= g
            cnt += 1
        A[j + g] = v

def ssort(A, n):
    global cnt
    cnt = 0
    m = max(min(int(n / 3), 100), 1)
    G = [m * 3 - 3 * (i + 1) + 1 for i in range(m - 1)]
    G.append(1)
    print(m)
    print(" ".join([str(g) for g in G]))
    for g in G:
        isort(A, n, g)

n = int(input())
A = [int(input()) for _ in range(n)]
ssort(A, n)
print(cnt)
print("\n".join([str(a) for a in A]))

### FIXED CODE ###

import sys

def isort(A, n, g):
    cnt = 0
    for i in range(g, n):
        v = A[i]
        j = i - g
        while j >= 0 and A[j] > v:
            A[j + g] = A[j]
            j -= g
            cnt += 1
        A[j + g] = v
    return cnt

def ssort(A, n):
    cnt = 0
    G = []
    h = 1
    while h <= n:
        G.append(h)
        h = 3 * h + 1
    m = len(G)
    G.reverse()
    print(m)
    print(*G, sep = " ")
    for g in G:
        cnt += isort(A, n, g)
    return cnt

n = int(input())
A = list(map(int, sys.stdin.readlines()))
cnt = ssort(A, n)
print(cnt)
print(*A, sep = "\n")
### BUGGY CODE ###

H,W=map(int,input().split())

def g(j):
    if j==".":
        return 1 #白
    else:
        return 0 #黒
    
S=[]
for j in range(H):
    S.append(list(map(g,input())))

def bools(b):
    if b==True:
        return 1
    else:
        return 0

def m(x,y):
    if (x,y)==(W-1,H-1):
        return 0
    elif x==W-1:
        return m(x,y+1)+bools(S[y][x]!=S[y+1][x])
    
    elif y==H-1:
        return m(x+1,y)+bools(S[y][x]!=S[y][x+1])

    else:
        return min(m(x+1,y)+bools(S[y][x]!=S[y][x+1]),m(x,y+1)+bools(S[y][x]!=S[y+1][x]))

print((m(0,0)+bools(S[0][0]==0)+1)//2)


### FIXED CODE ###

H,W=map(int,input().split())

def g(j):
    if j==".":
        return 1 #白
    else:
        return 0 #黒
    
S=[]
for j in range(H):
    S.append(list(map(g,input())))


Z=[[0]*W for i in range(H)]

for y in range(H):
    if y==0:
        for x in range(W):
            if x==0:
                Z[0][0]=1-S[0][0]
            else:
                Z[y][x]=Z[y][x-1]+(S[y][x-1]==1 and S[y][x]==0)
    else:
        for x in range(W):
            if x==0:
                Z[y][x]=Z[y-1][x]+(S[y-1][x]==1 and S[y][x]==0)
            else:
                Z[y][x]=min(Z[y][x-1]+(S[y][x-1]==1 and S[y][x]==0),Z[y-1][x]+(S[y-1][x]==1 and S[y][x]==0))

print(Z[H-1][W-1])
### BUGGY CODE ###

number = int(input())
a = list(map(int,input().split()))
count = 0
for i in range(number):
    for j in range(number):
        if a[i] ==j+1 and a[j] == i+1:
            count += 1
print(int(count/2))

### FIXED CODE ###

number = int(input())
a = list(map(int,input().split()))
count = 0
for i in range(number):
    j = a[i]-1
    if a[j] == i + 1:
        count += 1
print(int(count/2))
### BUGGY CODE ###

#16Bmissing
def mn(li):
    if li[0]== "S":
        c[0]=0
    elif li[0]== "H":
        li[0]=1
    elif li[0]== "C":
        li[0]=2
    elif li[0]== "D":
        li[0]=3
    li[1]=int(li[1])
    return li
def nm(nu):
    if nu==0:
        co="S"
    elif nu==1:
        co="H"
    elif nu==2:
        co="C"
    elif nu==3:
        co="D"
#    co+=??? ???+str(li[1])
    return co
N=int(input())
m=[[]]
for i in range(N-1):
    c=[j for j in input().split()]
    c=mn(c)
    for j in range(2):
        m[i].append(c[j])
    m.append([])
c=[j for j in input().split()]
c=mn(c)
for j in range(2):
    m[N-1].append(c[j])
m.sort()
i=0
j=0
l=1
#print(m)
while i < N:
    while m[i][0]!=j:
        for k in range(1,14):
            print(nm(m[i][0])+" "+str(k))
        j+=1
  #  print(i)
    while m[i][1]!=l:
        print(nm(m[i][0])+" "+str(l))
        l+=1
    l+=1
    if l>13:
        l=1
        j+=1
    i+=1

### FIXED CODE ###

#16Bmissing
def mn(li):
    if li[0]== "S":
        c[0]=0
    elif li[0]== "H":
        li[0]=1
    elif li[0]== "C":
        li[0]=2
    elif li[0]== "D":
        li[0]=3
    li[1]=int(li[1])
    return li
def nm(nu):
    if nu==0:
        co="S"
    elif nu==1:
        co="H"
    elif nu==2:
        co="C"
    elif nu==3:
        co="D"
#    co+=??? ???+str(li[1])
    return co
T=[]
for i in range(4):
    T.append([])
    for j in range(14):
        T[i].append(0)
N=int(input())
for i in range(N):
    c=[j for j in input().split()]
    c=mn(c)
  #  print(c)
    T[c[0]][c[1]]=1
#print(T)
for i in range(4):
    for j in range(1,14):
        if T[i][j]==0:
            print(nm(i)+" "+str(j))
### BUGGY CODE ###

n = int(input())

i = 1
power = 1
while i <= n:
    power *= i
    i += 1
print(power % (10**9 + 7))


### FIXED CODE ###

n = int(input())

i = 1
power = 1
while i <= n:
    if power >= 10**9 + 7:
        power %= (10**9 + 7)
    power *= i
    i += 1
print(power % (10**9 + 7))
### BUGGY CODE ###

S = input()
 
INF = 10 ** 9 + 7
 
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1 # 後ろからi文字目までで、13で割った余りがjであるものの個数 0 → 3 → 43 → 543
# 実際にはjの部分に0 → 3 → 43 → 543が入るイメージだが、13で割って入れている
mul = 1 # 桁の部分を示す
 
for i in range(len(S)):
    x = S[-(i+1)] 
    if x == "?":
        for k in range(10): # ?の場合は、?の部分を0から9まで埋めた上で、余りが0から12のケースの個数を数える
            for j in range(13):
                dp[i+1][(mul * k + j) % 13] += dp[i][j] # 例えば43の時の個数は、3の時の個数に40の時の個数を加えたもの
                dp[i+1][(mul * k + j) % 13] %= INF
                
    else:
        k = int(x)
        for j in range(13):
            dp[i+1][(mul * k + j) % 13] += dp[i][j]
            dp[i+1][(mul * k + j) % 13] %= INF
    mul = mul * 10 % 13
    
print(dp[len(S)][5])

### FIXED CODE ###

INF = 10 ** 9 + 7

S = input()
# dp[i][j]はうしろからi桁を13で割った余りがjであるものの個数
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
mul = 1

for i in range(len(S)):
    x = S[-(i + 1)]
    if x == "?":
        for k in range(10):
            for j in range(13):
                dp[i + 1][(mul * k + j) % 13] += dp[i][j]
                dp[i + 1][(mul * k + j) % 13] %= INF
    else:
        k = int(x)
        for j in range(13):
            dp[i + 1][(mul * k + j) % 13] += dp[i][j]
            dp[i + 1][(mul * k + j) % 13] %= INF
    mul = mul * 10 % 13  # 計算量削減のためここで13で割っている

print(dp[len(S)][5])
### BUGGY CODE ###

import networkx as nx

G = nx.DiGraph()

N, M = map(int, input().split())
G.add_nodes_from(range(1, N + 1))
for _ in range(M):
    a, b, c = map(int, input().split())
    G.add_edge(a, b, weight=-c)

cG = G.copy()
cG.add_edge(N, 1)
for c in nx.strongly_connected_components(cG):
    if 1 in c:
        sG = G.subgraph(c)
        break
try:
    print(-nx.bellman_ford_path_length(sG, 1, N))
except nx.NetworkXUnbounded:
    print('inf')


### FIXED CODE ###

from scipy.sparse.csgraph import connected_components, bellman_ford, NegativeCycleError
from scipy.sparse import csr_matrix

N, M = map(int, input().split())
frm, to, length = [], [], []
for _ in range(M):
    a, b, c = map(int, input().split())
    frm.append(a - 1)
    to.append(b - 1)
    length.append(-c)

# N-1→0の辺を追加してから卿連結判定⇒ゴールにたどり着けない閉路を除外
connect_check = csr_matrix(([1] * (M + 1), (frm + [N - 1], to + [0])), shape=(N, N))
_, labels = connected_components(connect_check, connection='strong')
label_num = labels[0]
frm_c, to_c, length_c = [], [], []
for i in range(M):
    if labels[frm[i]] == labels[to[i]] == label_num:
        frm_c.append(frm[i])
        to_c.append(to[i])
        length_c.append(length[i])

matr = csr_matrix((length_c, (frm_c, to_c)), shape=(N, N))

try:
    ans = -int(bellman_ford(matr, indices=0)[N - 1])
    print(ans)
except NegativeCycleError:
    print('inf')
### BUGGY CODE ###

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

ma = max(A)
for i in range(N):
    if A[i] == ma:
        A[i] = 0
        print(max(A))
        A[i] = ma
    else:
        print(ma)

### FIXED CODE ###

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

newA = sorted(A)
ma = newA[N-1]
for i in range(N):
    if A[i] == ma:
        print(newA[N-2])
    else:
        print(ma)
### BUGGY CODE ###

import sys
from math import factorial
input = sys.stdin.readline
N, M, K = map(int, input().split())
MOD = 998244353
inv = [1] * N
FAC = factorial(N - 1) % MOD
if M == 1:
    print(1 if K == N - 1 else 0)
else:
    res = 0
    for i in range(1, N):
        inv[i] = pow(factorial(i), MOD - 2, MOD)
    for k in range(K + 1):
        # k個の○とN-k-1個の|を並べる順列
        # punc = (N-1)!/k!/(N-k-1)!
        # punc = factorial(
        #     k + (N - k - 1)) // factorial(k) // factorial(N - k - 1)
        punc = FAC
        # punc *= pow(factorial(k), MOD - 2, MOD)
        punc *= inv[k]
        punc = punc % MOD
        # punc *= pow(factorial(N - k - 1), MOD - 2, MOD)
        punc *= inv[N - k - 1]
        punc = punc % MOD
        color = M * pow(M - 1, N - k - 1, MOD)
        res += ((punc % MOD) * color) % MOD
        res = res % MOD
    print(res % MOD)


### FIXED CODE ###

import sys
from math import factorial
input = sys.stdin.readline
N, M, K = map(int, input().split())
MOD = 998244353
inv = [1] * N
FAC = factorial(N - 1) % MOD
if M == 1:
    print(1 if K == N - 1 else 0)
else:
    res = 0
    for i in range(1, N):
        inv[i] = (inv[i - 1] * pow(i, MOD - 2, MOD)) % MOD
    for k in range(K + 1):
        # k個の○とN-k-1個の|を並べる順列
        # punc = (N-1)!/k!/(N-k-1)!
        # punc = factorial(
        #     k + (N - k - 1)) // factorial(k) // factorial(N - k - 1)
        punc = FAC
        # punc *= pow(factorial(k), MOD - 2, MOD)
        punc *= inv[k]
        punc = punc % MOD
        # punc *= pow(factorial(N - k - 1), MOD - 2, MOD)
        punc *= inv[N - k - 1]
        punc = punc % MOD
        color = M * pow(M - 1, N - k - 1, MOD)
        res += ((punc % MOD) * color) % MOD
        res = res % MOD
    print(res % MOD)
### BUGGY CODE ###

K, S = map(int, input().split())
cnt = 0
for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if 0<= i <= K and 0<= j <= K and 0<=k<=K and i+j+k == S:
                cnt += 1
print(cnt)


### FIXED CODE ###

K, S = map(int, input().split())
cnt = 0
for i in range(K+1):
    for j in range(K+1):
        if 0 <= S-i-j <= K:
            cnt += 1
print(cnt)
### BUGGY CODE ###

n = int(input())
#a = [x for x in range(1, n+1)]
#print(sum(a) - n)

wa = 0
for i in range(1, n):
    wa+=i

print(wa)

### FIXED CODE ###

n = int(input())

wa = (n+1)*(n//2)
if n % 2 == 1:
    wa+=(n+1)//2

print(wa-n)
### BUGGY CODE ###

N = int(input())

S = ["0"]*N

for i in range(N):
  S[i] = str(input())

cnt_ar = list(set(S))
cnt_ar.sort()

length = len(cnt_ar)

ar = [0]*length

for i in range(length):
  ar[i] = S.count(cnt_ar[i])

max_num = max(ar)

for i in range(length):
  if ar[i] == max_num:
    print(cnt_ar[i])


### FIXED CODE ###

N = int(input())

d = {}
for _ in range(N):
    S = input()
    if S in d:
        d[S] += 1
    else:
        d[S] = 1

m = max(d.values())
for s in sorted(k for k in d if d[k] == m):
    print(s)
### BUGGY CODE ###

import sys
import numpy as np
from numba import njit

input = sys.stdin.buffer.readline

#'''
N = int(input())
vws = [tuple(map(int, input().split())) for _ in range(N)]
'''
N = 2 ** 18
vws = [(1, i%10 + 1) for i in range(N)]
#'''

n = min(2**10, N)
vws_arr = np.array(vws)

dp = np.zeros((n, 10**5+1), np.int32)
dp[0, vws[0][1]:] = vws[0][0]
for i in range(1, n):
    v, w = vws[i]
    dp[i] = dp[(i-1)//2]
    dp[i, w:] = np.maximum(dp[(i-1)//2, :-w]+v, dp[i, w:])


@njit("i4(i4[:], i4[:], i4, i4[:], i4[:])", cache = True)
def func(vs, ws, L, dp_, bits):
    values = np.zeros_like(bits, np.int32)
    weights = np.zeros_like(bits, np.int32)
    for i in range(len(vs)):
        indices = np.nonzero(np.bitwise_and(bits, 1<<i))
        values[indices] += vs[i]
        weights[indices] += ws[i]

    mask = (weights <= L)     
    return np.amax(dp_[L - weights[mask]] + values[mask])


bits = np.arange(1 << 8).astype(np.int32)

Q = int(input())
for _ in range(Q):
    V, L = map(int, input().split())
    V -= 1
    if V < n:
        print(dp[V, L])
    else:
        Vs = []
        while V >= n:
            Vs.append(V)
            V = (V-1) // 2
        vs = vws_arr[Vs, 0]
        ws = vws_arr[Vs, 1]
        print(func(np.array(vs, np.int32), np.array(ws, np.int32), L, dp[V], bits[:1 << len(Vs)]))


### FIXED CODE ###

import sys
import numpy as np
from numba import njit

input = sys.stdin.buffer.readline

N = int(input())
vws = [tuple(map(int, input().split())) for _ in range(N)]

n = min(2**11, N)
vws_arr = np.array(vws, np.int32)

dp = np.zeros((n, 10**5+1), np.int32)
for i in range(n):
    v, w = vws[i]
    dp[i] = dp[(i-1)//2]
    dp[i, w:] = np.maximum(dp[(i-1)//2, :-w]+v, dp[i, w:])


@njit("i4(i4[::1], i4[::1], i4, i4[::1])", cache = True)
def func(vs, ws, L, dp_):
    values = np.zeros(1 << len(vs), np.int32)
    weights = np.zeros(1 << len(vs), np.int32)
    for i in range(len(vs)):
        for bits in range(1 << len(vs)):
            if bits & (1 << i):
                values[bits] += vs[i]
                weights[bits] += ws[i]
    ret = 0
    for v, w in zip(values, weights):
        if w <= L:
            ret = max(dp_[L - w] + v, ret)
    return ret

Q = int(input())
for _ in range(Q):
    V, L = map(int, input().split())
    V -= 1
    if V < n:
        print(dp[V, L])
    else:
        Vs = []
        while V >= n:
            Vs.append(V)
            V = (V-1) // 2
        vs = vws_arr[Vs, 0]
        ws = vws_arr[Vs, 1]
        print(func(vs, ws, L, dp[V]))
### BUGGY CODE ###

N, M = map(int, input().split())
value = 0
A_list = []
for i in range(N):
    A, B = map(int, input().split())
    A_list += [A for i in range(B)]
A_list.sort()
for i in range(M):
    value += A_list[i]
print(value)

### FIXED CODE ###

N, M = map(int, input().split())
AB_list = [list(map(int, input().split())) for i in range(N)]
AB_list.sort()
ans, cnt = 0, 0
for a, b in AB_list:
    ans += a*b
    cnt += b
    if cnt > M:
        ans = ans-(a*(cnt-M))
        break
print(ans)
### BUGGY CODE ###

N = int(input())

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

for i in range(1,N+1):
  c = A.count(i)
  print(c)

### FIXED CODE ###

N = int(input())
 
A = list(map(int, input().split()))
 
result = [0]*N
for i in A:
  result[i-1] +=1
  
for j in result:
  print(j)
### BUGGY CODE ###

import numpy as np

N, M=map(int, input().split())
A=np.sort(list(map(int, input().split())))
for i in range(M):
  B, C=map(int, input().split())
  
  n_cand=np.sum(A<C)
  if n_cand<=B:
    A[:n_cand]=C
  else:
    A=np.r_[A[B:n_cand], np.ones(B)*C, A[n_cand:]]
    
print(int(np.sum(A)))

### FIXED CODE ###

from collections import defaultdict

cnt=defaultdict(int)

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

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

tmp=[0]*M
for i in range(M):
  B, C=map(int, input().split())
  cnt[C]+=B
  tmp[i]=C
A+=tmp

tmp=list(cnt.keys())
tmp.sort()
j=-1
out=0
while N>0:
  if cnt[tmp[j]]<=N:
    out+=tmp[j]*cnt[tmp[j]]
    N-=cnt[tmp[j]]
  else:
    out+=tmp[j]*N
    N=0
    
  j-=1
print(out)
### BUGGY CODE ###

import sys

N, M = map(int, input().split())
li = [list(map(int, input().split())) for n in range(M)]

a = []

for i in li:
  if i[0] == N:
    a.append(i[1])

  if i[1] == N:
    a.append(i[0])

for i in li:
  if i[0] == 1 and i[1] in a:
    print("POSSIBLE")
    sys.exit()

print("IMPOSSIBLE")

### FIXED CODE ###

import sys
from collections import defaultdict
d = defaultdict(int)

N, M = map(int, input().split())
li = [list(map(int, input().split())) for n in range(M)]

for i in li:
  if i[1] == N:
    d[i[0]] += 1

for i in li:
  if i[0] == 1 and d[i[1]] != 0:
    print("POSSIBLE")
    sys.exit()
    
print("IMPOSSIBLE")
### BUGGY CODE ###

s = input()
K = int(input())

sss = set()
for i in range(len(s)):
    for j in range(i+1,len(s)+1):
        sss.add(s[i:j])

ans = sorted(sss)
print(ans[K-1])


### FIXED CODE ###

s = input()
K = int(input())

sss = set()
M = len(s)
for end in range(1, K+1):
    for i in range(M):
        sss.add(s[i:(i+end)])

ans = sorted(sss)
print(ans[K-1])
### BUGGY CODE ###

a,b,c=map(int,input().split())
count=0
while a<=b:
    if c%a==0:
        count+=1

print(count)


### FIXED CODE ###

a,b,c=map(int,input().split())
count=0
while a<=b:
    if c%a==0:
        count+=1
    a+=1
print(count)
### BUGGY CODE ###

def solve():
    h, k = read()
    result = think(h, k)
    write(result)


def read():
    n, k = read_int(2)
    return read_int(n), k


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(h, k):
    cost = [0 for x in range(len(h))]
    # element of h <= 10 ** 4, elements <= 10 ** 5, +1 in case
    initial_value_of_cost = 10 ** (4 + 5) + 1
    for dst in range(1, len(h)):
        minimum_cost = initial_value_of_cost
        for src in range(max(0, dst - k), dst):
            minimum_cost = min(minimum_cost, cost[src] + abs(h[src] - h[dst]))
        cost[dst] = minimum_cost
    return cost[len(h) - 1]


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

### FIXED CODE ###

def solve():
    h, k = read()
    result = think(h, k)
    write(result)


def read():
    n, k = read_int(2)
    return read_int(n), k


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(h, k):
    cost = [0 for x in range(len(h))]
    for dst in range(1, len(h)):
        start_point = max(0, dst - k)
        cost[dst] = min([cost[src] + abs(h[src] - h[dst]) for src in range(start_point, dst)])
    return cost[len(h) - 1]


def write(result):
    print(result)


if __name__ == '__main__':
    solve()
### BUGGY CODE ###

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

l=N
for i in range(0,N):
    if A[i]>=K:
        l=i
        break
A=A[:l]
N=l
#A[num]が必要な数か判定
def condition(num):
    if N-1>num:
        test=A[:num]+A[num+1:]
    else:
        test=A[:num]
    if A[num]>=K:
        return True
    else:
        dp=[False for i in range(0,K)]
        for i in range(K-A[num],K):
            dp[i]=True

        for i in range(1,N):
            for j in range(0,K-test[i-1]):
                dp[j]=dp[j] or dp[j+test[i-1]]
        return dp[0]

#必要な数のうち最小のもの
if N==0:
    print(0)
elif N==1:
    if A[0]>=K:
        print(0)
    else:
        print(1)
else:
    start=0
    end=N-1
    while end-start>1:
        t=(end+start)//2
        if condition(t):
            end=t
        else:
            start=t

    if condition(start):
        print(start)
    elif condition(end):
        print(end)
    else:
        print(N)

### FIXED CODE ###

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

l=N
for i in range(0,N):
    if A[i]>=K:
        l=i
        break
A=A[:l]
N=l
#A[num]が必要な数か判定
def condition(num):
    if N-1>num:
        test=A[:num]+A[num+1:]
    else:
        test=A[:num]
    if A[num]>=K:
        return True
    else:
        check=0
        for i in range(K-A[num],K):
            check+=pow(2,i)

        for i in range(1,N):
            check=check|(check>>test[i-1])

        return check%2==1
        

#必要な数のうち最小のもの
if N==0:
    print(0)
elif N==1:
    if A[0]>=K:
        print(0)
    else:
        print(1)
else:
    start=0
    end=N-1
    while end-start>1:
        t=(end+start)//2
        if condition(t):
            end=t
        else:
            start=t

    if condition(start):
        print(start)
    elif condition(end):
        print(end)
    else:
        print(N)
### BUGGY CODE ###

n = input().split()
n = [int(i) for i in n]
num = n[-1]
count = 0
for i in range(num):
  num -= 1
  if n[0] is not 0:
    count+=1
    n[0] -= 1
  elif n[0] is 0 and n[1] is not 0:
    n[1] -= 1
  else:
    count-=1
    n[-2] -= 1

print(count)

### FIXED CODE ###

n = input().split()
n = [int(i) for i in n]
count = 0

a = n[0]*1
b = n[1] 
c = n[2]*-1
while n[-1] > 0:
    if n[0] >= n[-1] and n[0] != 0:
        count = n[-1]*1
        n[-1] = 0
    elif n[0] < n[-1] and n[0] != 0:
        count += n[0]
        tmp = n[0]+n[1]
        n[-1] -= tmp
        n[0] = 0
    elif n[2] >= n[-1]:
        count += n[-1]*-1
        n[-1] = 0
    elif n[2] < n[-1]:
        count += c
        n[-1] = 0

print(count)
### BUGGY CODE ###

N = int(input())
A = []
m = 0
for i in range(N):
    m = int(input())
    A.append(m)
    #A[i] = int(A[i])
task = 0
m = 0
for i in range(N):
    task = A.pop(i)
    m = max(A)
    print(m)
    A.insert(i,task)

### FIXED CODE ###

N = int(input())
A = []
m = 0
for i in range(N):
    A.append(int(input()))
task = 0
m = max(A)
m2 = sorted(A)[-2]
maxindex = []
for i in range(N):
    if A[i] == m:
        print(m2)
    else:
        print(m)