CodeEff/ECCO huggingface dataset:
# Original solution
N,K = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [0 for i in range(N)]
for i in range(1,N):
  s = 10**10
  for j in range(1,min(i,K)+1):
    s = min(s,dp[i-j]+abs(h[i]-h[i-j]))
  dp[i] = s
print((dp[N-1]))

# Optimized solution
N,K = list(map(int,input().split()))
H = list(map(int,input().split()))
DP = [0]
for i in range(1,N):
    a = DP[i-1] + abs(H[i]-H[i-1])
    for j in range(2,min(K,i)+1):
        b = DP[i-j] + abs(H[i]-H[i-j])
        a = min(a,b)
    DP.append(a)
print((DP[N-1]))
# Original solution
N, K = list(map(int, input().split()))
H = [0] + list(map(int, input().split()))

DP = [0] * (N+1)

for i in range(2, N+1):
  k = min(i, K+1)
  DP[i] = min(DP[i-j] + abs(H[i-j] - H[i]) for j in range(1, k))
print((DP[N]))


# Optimized solution
N, K = list(map(int, input().split()))
H = [0] + list(map(int, input().split()))

DP = [0] * (N+1)

for i in range(2, N+1):
  k = max(i-K, 1)
  DP[i] = min(dp + abs(h - H[i]) for dp, h in zip(DP[k:i], H[k:i]))
print((DP[N]))
# Original solution
n, k = list(map(int, input().split()))
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(k)]
for i, hi in enumerate(h):
    if i == 0: continue
    (cs, hs) = (k - i, 0) if k > i else (0, i - k)
    new_cost = min([ck + abs(hi - hk) for ck, hk in zip(cost[cs:], h[hs:])])
    cost = cost[1:] + [new_cost]
print((cost[k - 1]))


# Optimized solution
n, k = list(map(int, input().split()))
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print((cost[-1]))
# Original solution
# Problem A - Frog 2 - Algoritmos e Grafos - MATA52
def frog2(n, h, k_max):
    global stones
    stones[0] = 0
    stones[1] = abs(h[1] - h[0])
    for i in range(2, n):
        for j in range(1, min(i, k_max) + 1):
            w1 = stones[i]
            w2 = abs(h[i - j] - h[i])
            stones[i] = min(w1, stones[i - j] + w2)
    return stones[n - 1]

n, k_max = list(map(int, input().split()))
h = list(map(int, input().split()))

stones = [float("inf")] * n
print((frog2(n, h, k_max)))

# Optimized solution
def woo(x, y):
    res = [0]*x
    z = list(map(int, input().split()))
    for i in range(1, x):
        res[i] = min(abs(z[i] - z[j]) + res[j] for j in range(max(0, i - y), i))
    return res

if __name__ == '__main__':
    x, y = list(map(int, input().split()))
    r = woo(x, y)
    f = len(r)-1
    print((r[f]))
# Original solution
def woo(x, y):
    res = [0]*x
    z = list(map(int, input().split()))
    for i in range(1, x):
        res[i] = min(abs(z[i] - z[j]) + res[j] for j in range(max(0, i - y), i))
    return res

if __name__ == '__main__':
    x, y = list(map(int, input().split()))
    r = woo(x, y)
    f = len(r)-1
    print((r[f]))



# Optimized solution
def main():
    n, k = tuple([int(e) for e in input().split()])
    arr = [int(e) for e in input().split()]
    t = [float('inf')] * (n + 1)
    t[0] = 0
    t[1] = 0 
    for i in range(2, n + 1):
        for j in range(1, k + 1):
            if i - j <= 0:
                break
            l1 = t[i - j] + abs(arr[i - j - 1] - arr[i - 1])
            t[i] = min(t[i], l1)
    print((t[n]))
    return 0


if __name__ == '__main__':
    main()
# Original solution
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [0] * n
t = []
 
for i in range(1,n):
    for l in range(1,k+1):
        if i - l >= 0:
            t.append(abs(h[i] - h[i-l]) + dp[i-l])
        else:
            break
 
    dp[i] = min(t)
    t.clear()

print((dp[-1]))

#実行制限時間2秒以内に間に合わない

# Optimized solution
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [0] * n
t = []
 
for i in range(1,n):
    for l in range(1,k+1):
        if i - l >= 0:
            t.append(abs(h[i] - h[i-l]) + dp[i-l])
        else:
            break
    dp[i] = min(t)
    t.clear()
 
print((dp[-1]))
# Original solution
def main2(N,h,K):
    inf = 2**31 -1
    dp = [inf] * N
    dp[0] = 0
    for i in range(1, N):
        for k in range(1, K+1):
            if i-k >= 0:
                dp[i] = min(dp[i], dp[i-k] + abs(h[i-k] - h[i]))
            else:
                break
    return dp[N-1]

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

print((main2(N,h,K)))

# Optimized solution
def main2(N,h,K):
    dp = [0] * N
    for i in range(1,N):
        dp[i] = min([dp[j] + abs(h[i] - h[j]) for j in range(max(0, i-K), i)])
    return dp[-1]

N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
print((main2(N,h,K)))
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [0]*n
for i in list(range(n))[1:]:
    dp[i] = min([dp[j]+abs(h[i]-h[j]) for j in [max(0, i-ki-1) for ki in range(k)]])
print((dp[-1]))

# Optimized solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [0]*n
for i in list(range(n))[1:]:
    tmp = max(0, i-k)
    dp[i] = min([dpk+abs(h[i]-hk) for dpk, hk in zip(dp[tmp:i], h[tmp:i])])
print((dp[-1]))
# Original solution
import sys
input = sys.stdin.readline
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))

dp = [float('inf')]*n
dp[0] = 0
for i in range(n):
    for j in range(1,k+1):
        if i-j >= 0:
            dp[i] = min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
print((dp[n-1]))


# Optimized solution
import sys
input = sys.stdin.readline
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))

dp = [10**9]*n
dp[0] = 0
for i in range(n):
    for j in range(1,k+1):
        if i-j >= 0:
            dp[i] = min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
print((dp[n-1]))
# Original solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))

dp=[0]*n
for i in range(1,n):
    dp[i]=min([dp[j] + abs(h[i]-h[j]) for j in range(max(i-k,0),i)])
print((dp[-1]))


# Optimized solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))

dp=[0]*n
def calc(n,k,h,dp):
    for i in range(1,n):
        dp[i]=min([dp[j] + abs(h[i]-h[j]) for j in range(max(i-k,0),i)])

calc(n,k,h,dp)
print((dp[-1]))
# Original solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
h+=[0]*k
inf=float('inf')
dp=[inf]*(n+k)
dp[0]=0
for i in range(n):
    for j in range(1,k+1):
        dp[i+j]=min(dp[i]+abs(h[i]-h[i+j]),dp[i+j])
print((dp[n-1]))


# Optimized solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
dp=[float("inf")]*n
dp[0]=0
for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i]=min(dp[i],dp[j]+abs(h[i]-h[j]))

print((dp[n-1]))
# Original solution
import sys
def mincost(n,h):
    dp=[-1 for i in range(n)]
    dp[n-1]=0
    dp[n-2]=abs(h[n-2]-h[n-1])
    for i in range(n-3,-1,-1):
        mn=min(i+k+1,n)
        mnm=sys.maxsize
        for j in range(i+1,mn):
            mnm=min(dp[j]+abs(h[j]-h[i]),mnm)
        dp[i]=mnm    
    return dp[0]

n,k=list(map(int,input().split()))
h=[int(x) for x in input().split()]
print((mincost(n,h)))

# Optimized solution
from sys import stdin, gettrace
import sys
 
if not gettrace():
    def input():
        return next(stdin)[:-1]
 
 
# def input():
#    return stdin.buffer.readline()
 
def mincost(n,h,k):
  dp=[-1 for i in range(n)]
  dp[n-1]=0
  dp[n-2]=abs(h[n-2]-h[n-1])
  for i in range(n-3,-1,-1):
    mn=min(i+k+1,n)
    mnm=sys.maxsize
    for j in range(i+1,mn):
      mnm=min(dp[j]+abs(h[j]-h[i]),mnm)
    dp[i]=mnm    
  return dp[0]
 
def main():
  n,k=list(map(int,input().split()))
  h=[int(x) for x in input().split()]
  print((mincost(n,h,k))) 
 
if __name__ == "__main__":
    main()
# Original solution
n,k = list(map(int,input().split()))
lst1 = list(map(int,input().split()))

dp = [float("inf") for _ in range(n)]

dp[0] = 0

for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i] = min(dp[i],dp[j]+abs(lst1[i]-lst1[j]))

print((dp[-1]))

# Optimized solution
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = list(map(int,readline().split()))
lst1 = list(map(int,readline().split()))

dp = [10**10 for _ in range(n)]
dp[0] = 0
for i in range(1,n):
	for j in range(1,k+1):
		if i-j < 0:
			break
		dp[i] = min(dp[i],dp[i-j]+abs(lst1[i-j]-lst1[i]))

print((dp[-1]))
# Original solution
n, k, *h = list(map(int, open(0).read().split()))
h += [0] * 200
dp = [0] + [float("inf")] * 100200
for i in range(1, n):
  dp[i] = min(dp[i-a] + abs(h[i] - h[i-a]) for a in range(1, k + 1))
print((dp[n - 1]))

# Optimized solution
n, k, *h = list(map(int, open(0).read().split()))
dp = [0] * n
for i in range(1, n):
  dp[i] = min(dp[i-a] + abs(h[i] - h[i-a]) for a in range(1, min(i, k) + 1))
print((dp[n - 1]))
# Original solution
n,x=list(map(int,input().split()))
a=list(map(int,input().split()))
dp=[-1]*n
dp[0]=0
if n<=x:
    x=n-1
for i in range(1,x+1):
    dp[i]=abs(a[i]-a[0])

for i in range(x+1,n):
    min1=10**9
    for j in range(i-(x),i):  
        if min1>dp[j]+abs(a[j]-a[i]):               
            min1=dp[j]+abs(a[j]-a[i])
    dp[i]=min1
print((dp[-1]))


# Optimized solution
n,x=list(map(int,input().split()))
a=list(map(int,input().split()))
if n<=x:#ｎこのDPを作りたいのでｘが超えたらそれを変える必要がある
    x=n-1#でもこれは実際甘え

dp=[-1]*n

dp[0]=0#さいしょのところは０かかる
for i in range(1,x+1):#飛べる距離までとべるところから最初の値を引く
    dp[i]=abs(a[i]-a[0])

for i in range(x+1,n):#さいしょのいちから飛べないところからDPを入れる
    min1=10**9
    for j in range(i-(x),i): # 最小を調べる繰り返し
        if min1>dp[j]+abs(a[j]-a[i]):               
            min1=dp[j]+abs(a[j]-a[i])#Xの数最小の値を調べる
    dp[i]=min1
print((dp[-1]))
# Original solution
import sys
input = sys.stdin.readline

N, K = list(map(int, input().split()))
h = [int(x) for x in input().split()]
dp = [0] * N

for i, hi in enumerate(h):
    if i == 0:
        continue
    if i - K < 0:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print((dp[-1]))

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(_) for _ in input().split()]
dp = [float('inf')]*(N)
dp[0] = 0
dp[1] = abs(h[1]-h[0])

for i in range(2, N):
    for j in range(1, K+1):
        if i >= j:
            dp[i] = min(dp[i-j]+abs(h[i]-h[i-j]), dp[i])

print((dp[N-1]))
# Original solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
dp=[0]*n
for i in range(1,n):
  dp[i]=min(dp[j]+abs(h[i]-h[j]) for j in range(max(i-k,0),i))
print((dp[n-1]))

# Optimized solution
def main():
	N, K = list(map(int, input().split()))
	h = list(map(int, input().split()))
	dp=[0]*N
	for i in range(1,N):
		dp[i] = min(dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i))
	print((dp[N-1]))
main()
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))

c = [0 for i in range(n)]

for i in range(1, n):
    l = max(0, i-k)
    c[i] = min([c[x] + abs(h[x] - h[i]) for x in range(l, i)])

print((c[n-1]))


# Optimized solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))

c = [0 for i in range(n)]

for i in range(1, n):
    l = max(0, i-k)
    c[i] = min(cj + abs(hj - h[i]) for hj, cj in zip(h[l:i], c[l:i]))

print((c[n-1]))
# Original solution
import sys
import math
from collections import deque

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

dp = [[10**9 for _ in range(K+1)] for _ in range(N)]
dp[0] = [0 for _ in range(K+1)]

for i in range(N):
    for l in range(K):
        if i-(l+1) >= 0:
            dp[i][l] = dp[i - (l + 1)][K] + abs(h[i] - h[i - (l + 1)])
        else:
            dp[i][l] = min(dp[i])
        if l == K-1:
            dp[i][K] = min(dp[i])
print((min(dp[N-1])))

# Optimized solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [[10**9 for _ in range(K+1)] for _ in range(N)]
dp[0] = [0 for _ in range(K+1)]

for i in range(N):
    for l in range(K):
        if i-(l+1) >= 0:
            dp[i][l] = dp[i - (l + 1)][K] + abs(h[i] - h[i - (l + 1)])
        else:
            dp[i][l] = min(dp[i])
        if l == K-1:
            dp[i][K] = min(dp[i])
print((min(dp[N-1])))
# Original solution
#

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

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

if n<3:
    print((abs(a[0]-a[-1])))
    exit()

c=[0]*n
c[-1]=0
mini=1000000

for i in reversed(list(range(n-k, n-1))):
    # print(i)
    c[i]=abs(a[i]-a[n-1])
    mini=min(mini, c[i])


for i in reversed(list(range(n-k))):
    c[i]=min([c[t]+abs(a[i]-a[t]) for t in range(i+1, i+k+1)])

# print("?", c)

print((c[0]))

# Optimized solution
n, k=list(map(int, input().split()))
 
a=list(map(int, input().split()))
 
if n<3:
    print((abs(a[0]-a[-1])))
    exit()
 
c=[0]*n
c[-1]=0
mini=1000000
 
for i in reversed(list(range(n-k, n-1))):
    # print(i)
    c[i]=abs(a[i]-a[n-1])
    mini=min(mini, c[i])
 
 
for i in reversed(list(range(n-k))):
    c[i]=min([c[t]+abs(a[i]-a[t]) for t in range(i+1, i+k+1)])
 
# print("?", c)
 
print((c[0]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

inf = 10000000000

dp = [inf] * N
dp[0] = 0

for i in range(1, N):
    #mindp_i = inf

    for jump in range(1, K + 1):
        if i - jump >= 0:
            dp[i] = min(dp[i], dp[i - jump] + abs(h[i] - h[i - jump]))

            #dpcand = dp[i - jump] + abs(h[i] - h[i - jump])
            #print(dpcand)
            #if dpcand < mindp_i:
            #    mindp_i = dpcand

    #dp[i] = mindp_i

print((dp[N-1]))

# Optimized solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

inf = float('inf')

dp = [inf] * N
dp[0] = 0

for i in range(1, N):
    mindp_i = inf

    for jump in range(1, K + 1):
        if i - jump >= 0:

            dpcand = dp[i - jump] + abs(h[i] - h[i - jump])
            #print(dpcand)
            if dpcand < mindp_i:
                mindp_i = dpcand

    dp[i] = mindp_i

print((dp[N-1]))
# Original solution
def main(): #グローバル名前空間で大きな処理を書かない
    n,k=list(map(int,input().split()))
    H=["dammy"]+list(map(int,input().split()))
    dp=[0]*(n+1)
    for i in range(2,min(k+2,n+1)):
        dp[i]=min(dp[j]+abs(H[i]-H[j]) for j in range(1,i))
    for i in range(k+2,n+1):
        dp[i] = min(dp[i-j]+abs(H[i]-H[i-j]) for j in range(1,k+1)) #TLEになる.
    print((dp[n]))
if __name__ == '__main__':
    main()

# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    H = list(map(int, input().split()))
    dp = [0] * n
    for i, Hi in enumerate(H):
        if i == 0:
            continue
        s = max(0, i-k) # iがk以下かどうかで処理が変わる.
        dp[i] = min([dl + abs(hl - Hi) for dl, hl in zip(dp[s:i], H[s:i])])
    print((dp[-1]))
main()
# Original solution
n,k= list(map(int,input().split()))
h = [int(i) for i in input().split()]
inf = 10**7
dp = [inf]*100005
dp[0] = 0
for i in range(1,n):
    dp[i] = min(dp[i-j] + abs(h[i-j] - h[i]) for j in range(1,min(k,i)+1))
print((dp[n-1]))

# Optimized solution
def main():
    n,k= list(map(int,input().split()))
    h = [int(i) for i in input().split()]
    inf = 10**7
    dp = [inf]*100005
    dp[0] = 0
    for i in range(1,n):
        dp[i] = min(dp[i-j] + abs(h[i-j] - h[i]) for j in range(1,min(k,i)+1))
    print((dp[n-1]))
main()
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print((DP[-1]))

# Optimized solution
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [0]
for i in range(n-1):
    cnt = float("inf")
    for j in range(k):
        if i-j < 0: continue
        cnt = min(dp[-1-j] + abs(h[i-j]-h[i+1]), cnt)
        #print("i =",i,"j =",j,"i-j =",i-j,"i+j =",i+j,"cnt =",cnt)
    dp.append(cnt)
print((dp[-1]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [float("inf")]*(N + 2)
dp[0] = 0
for i in range(N):
    for k in range(1, K + 1):
        if i < N - k:
            dp[i + k] = min(dp[i + k], abs(h[i] - h[i + k]) + dp[i])

print((dp[N - 1]))


# Optimized solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [float("inf")]*(N + 2)
dp[0] = 0
for i in range(N):
    for k in range(1, min(K + 1, N - i)):
        dp[i + k] = min(dp[i + k], abs(h[i] - h[i + k]) + dp[i])

print((dp[N - 1]))
# Original solution
import sys
sys.setrecursionlimit(10**7)
n,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
INF = 10 ** 9
memo = [-1]*n
memo[0] = 0

def dp(i):
    if (memo[i] != -1):
        return memo[i]
    ans = INF
    for j in range (max(0, i-k),i):
        ans = min(ans, dp(j) + abs(h[i] - h[j]))
    memo[i] = ans
    return memo[i]

print((dp(n-1)))


# Optimized solution
import sys
sys.setrecursionlimit(10**7)
n,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
memo = [0]*n
for i in range(1, n):
    min_k = float('inf') 
    min_k=min([memo[j] +abs(h[i]-h[j]) for j in range(max(0, i-k), i)])
    memo[i] = min_k

print((memo[-1]))
# Original solution
import sys
n, K = list(map(int, sys.stdin.readline().split()))
H = list(map(int, sys.stdin.readline().split()))

p = [float('inf') for _ in range(n)]
p[0] = 0
for i in range(1, n):
    for k in range(1, K+1):
        if i-k >=0:
            tmp = abs(H[i]-H[i-k]) + p[i-k]
            if p[i] > tmp:
                p[i] = tmp

print((p[-1]))

# Optimized solution
#EDP-B
import sys
n, K = list(map(int, sys.stdin.readline().split()))
H = list(map(int, sys.stdin.readline().split()))
dp = [0 for i in range(n)]
#初期条件
for i in range(1, n):
    dp[i] = abs(H[i]-H[i-1]) + dp[i-1]
#print(dp)
for i in range(2, n):
    for k in range(1, K+1):
        if i-k>=0:
            dp[i] = min(dp[i], abs(H[i]-H[i-k])  + dp[i-k])
            #print(dp, i, k)

print((dp[-1]))
# Original solution
#DP B
import sys
N, K = list(map(int, sys.stdin.readline().split()))
H = list(map(int, sys.stdin.readline().split()))

dp = [0 for _ in range(N)]
#print(dp)
#print(H)
dp[1] = abs(H[1]-H[0])
#print(dp)

for i in range(2, N):
    for k in range(1, K+1):
        if i-k>=0:
            tmp = abs(H[i]-H[i-k]) + dp[i-k]
            if dp[i] ==0 or tmp < dp[i]:
                dp[i] = tmp
    #print(dp)
print((dp[-1]))

# Optimized solution
#DP B
import sys
N, K = list(map(int, sys.stdin.readline().split()))
H = list(map(int, sys.stdin.readline().split()))

dp = [-1 for _ in range(N)]
#print(dp)
#print(H)
dp[0] = 0
#print(dp)

for i in range(1, N):
    for k in range(1, K+1):
        if i-k>=0:
            tmp = abs(H[i]-H[i-k]) + dp[i-k]
            if dp[i] ==-1 or tmp < dp[i]:
                dp[i] = tmp
    #print(dp)
print((dp[-1]))
# Original solution
# https://atcoder.jp/contests/dp/tasks/dp_b
# B - Frog 2

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


dp = [0] * N

for i in range(1, N):
    MIN = None
    for j in range(1, K + 1):
        if i - j < 0:
            continue
        if MIN == None:
            MIN = dp[i - j] + abs(H[i] - H[i - j])
        else:
            MIN = min(dp[i - j] + abs(H[i] - H[i - j]), MIN)
    dp[i] = MIN

print((dp[-1]))


# Optimized solution
N,K=list(map(int,input().strip().split(" ")))
H=list(map(int,input().strip().split(" ")))
dp=[0]+[10**10]*(N-1)
for i in range(1,N):
    temp=0
    for j in range(i-K,i):
        if j>=0:
            dp[i]=min(dp[i],dp[j]+abs(H[i]-H[j]))
print((dp[-1]))
# Original solution
import sys
#import numpy as np
#from collections import defaultdict
import math
#from collections import deque

input = sys.stdin.readline



def main():


    N ,K= list(map(int,input().split()))
    H = list(map(int,input().split()))
    H = [0] + H
    dp= [0] *(N+1)
    dp = [10**5] * (N + 1)
    dp[1] = 0

    for i in range(2, N + 1):
        way =[10**10]*(K+2)
        for j in range(1, K+1):
            if j<i:
                way[j]=(dp[i-j]+abs(H[i]-H[i-j]))
        dp[i] = min(way)


    print((dp[N]))





if __name__ == "__main__":
    main()

# Optimized solution
import sys
#import numpy as np
#from collections import defaultdict
#import math
#from collections import deque

input = sys.stdin.readline



def main():


    N ,K= list(map(int,input().split()))
    H = list(map(int,input().split()))
    H = [0] + H
    dp= [0] *(N+1)
    dp = [10**5] * (N + 1)
    dp[1] = 0

    for i in range(2, N + 1):
        way =[10**10]*(K+2)
        for j in range(1, K+1):
            if j<i:
                way[j]=(dp[i-j]+abs(H[i]-H[i-j]))
        dp[i] = min(way)


    print((dp[N]))





if __name__ == "__main__":
    main()
# Original solution
N, K = list(map(int, input().split()))
h = [0] + list(map(int, input().split()))
dp = [10**10 for i in range(N+1)]
dp[0] = dp[1] = 0
for i in range(1, N+1):
    for k in range(1, K+1):
        if i + k <= N:
            dp[i+k] = min(dp[i+k], dp[i]+abs(h[i] - h[i+k]))
print((dp[N]))

# Optimized solution
N, K = list(map(int, input().split()))
h = [0] + list(map(int, input().split()))
dp = [10**10] * (N+1)
dp[0] = dp[1] = 0
for i in range(1, N+1):
    for k in range(1, K+1):
        if i + k <= N:
            dp[i+k] = min(dp[i+k], dp[i]+abs(h[i] - h[i+k]))
print((dp[N]))
# Original solution
# B - Frog 2
import sys
sys.setrecursionlimit(10**6)

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

# 無限大の値
INF = 10**10

# DP テーブル
dp = [0]*(100010)

# DP テーブル全体を初期化
for i in range(100010):
    dp[i] = INF 

# 初期条件
def rec(v):
    # DP の値が既に更新されていたらそのままリターン 
    if dp[v] < INF:
        return dp[v]
    if v == 0:
        dp[0] = 0
        return 0
    
    res = INF
    for k in range(1,K+1):
        # 遷移元の足場がないとき

        if v-k < 0:
            continue
        # 足場 v-k から足場 v に移動する
        res = min(res, rec(v-k) + abs(h[v]-h[v-k]))
        # 結果をメモして返す
        dp[v] = res
    return res
print((rec(N-1)))

# Optimized solution
# B - Frog 2
N,K = list(map(int,input().split()))
h = list(map(int,input().split()))

# 無限大の値
INF = 10**10

# DP テーブル
dp = [0]*(100010)

# DP テーブル全体を初期化
for i in range(100010):
    dp[i] = INF 

# 初期条件
dp[0] = 0

for v in range(1,N):
    for k in range(1,K+1):
        # 遷移元の足場がないとき
        if v-k < 0:
            continue
        # 足場 v-k から足場 v に移動する
        dp[v] = min(dp[v], dp[v-k] + abs(h[v]-h[v-k]))
print((dp[N-1]))
# Original solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
final=[0]*n
if k>=n:
  print((abs(l[n-1]-l[0])))
else:   
   for i in range(1,k+1):
      final[i]=abs(l[i]-l[0])
  
   for i in range(k+1,n):

      mini=10**9+1



  
      x=k
      for j in range(1,x+1):

           mini=min(mini,final[i-j]+abs(l[i]-l[i-j]))
      final[i]=mini
   print((final[-1]))


# Optimized solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
final=[0]*n
if k>=n:
  print((abs(l[n-1]-l[0])))
else:   
   for i in range(1,k+1):
      final[i]=abs(l[i]-l[0])
  
   for i in range(k+1,n):

      li=[abs(l[i]-l[ik])+final[ik] for ik in range(i-1,i-k-1,-1) ]
      final[i]=min(li)
   print((final[-1]))
# Original solution
import sys
input = sys.stdin.readline

N,K=(int(i) for i in input().split())
h = [int(i) for i in input().split()]
c = [0]*N
for i in range(1,N):
    jumpmax = min(i,K)
    c[i] = min([abs(h[i]-h[i-j])+c[i-j] for j in range(1,jumpmax+1)])
print((c[N-1]))

# Optimized solution
N,K=(int(i) for i in input().split())
h = [int(i) for i in input().split()]
c = [0]*N
for i in range(1,N):
    jumpmax = min(i,K)
    c[i] = min([abs(h[i]-h[i-j])+c[i-j] for j in range(1,jumpmax+1)])
print((c[N-1]))