CodeEff/ECCO huggingface dataset:
# Original solution
#!/usr/bin/env python3

def main():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,K+1):
            if i+j > N:
                break
            else:
                dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print((dp[N]))

if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,K+1):
            if i+j > N:
                break
            else:
                dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print((dp[N]))

if __name__ == "__main__":
    main()
# Original solution
import sys

stdin = sys.stdin

mod = 1000000007
inf = 1 << 60

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, k = na()
h = na()

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

print((dp[n - 1]))


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

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

INF = 1e12

dp = [INF] * n

dp[0] = 0
dp[1] = abs(pedras[1] - pedras[0])

for i in range(2,n):
    pulo = 0
    for j in range(i-1,-1,-1):
        pulo += 1
        if pulo > k:
            break
        else:
            dp[i] = min(dp[i], dp[j] + abs(pedras[i] - pedras[j]))


print((dp[n-1]))
# Original solution
n,k = list(map(int, input().split()))
H = [0]+list(map(int, input().split()))
dp = [float("inf")]*(n+1)
dp[0] = 0
dp[1] = 0

for i in range(n):
  for k_i in range(1, k+1):
    if i+k_i<=n:
      dp[i+k_i] = min(dp[i+k_i], dp[i]+abs(H[i+k_i]-H[i]))
      
print((dp[-1]))

# Optimized solution
n,k = list(map(int, input().split()))
H = [0]+list(map(int, input().split()))
dp = [float("inf")]*(n+1)
dp[0] = 0
dp[1] = 0

for i in range(1, n):
  for k_i in range(1, k+1):
    if i+k_i<=n:
      dp[i+k_i] = min(dp[i+k_i], dp[i]+abs(H[i+k_i]-H[i]))
      
print((dp[-1]))
# Original solution
def resolve():
	# N: 足場の個数, K: 1回で右にジャンプできる足場の数
	N, K = list(map(int, input().split()))
	# h: 各足場の高さ
	# dpテーブル宣言: dp[今いる足場の番号-1]=[最小コスト, 足場の高さ]
	dp = [
		[float('inf'), int(h_)]
		for h_ in input().split()
	]
	dp[0][0] = 0
	dp[1][0] = abs(dp[0][1] - dp[1][1])

	# k個左の足場からジャンプして来た時の最小コストを調べる
	for n in range(2, N):
		index_0 = max(0, n-K)
		h_ = dp[n][1]
		dp[n][0] = min(
			[
				abs( h_ - dp_[1] ) + dp_[0]
				for dp_ in dp[index_0:n]
			]
		)
	print((dp[N-1][0]))

resolve()

# Optimized solution
def resolve():
	# N: 足場の個数, K: 1回で右にジャンプできる足場の数
	N, K = list(map(int, input().split()))
	# h: 各足場の高さ
	# dpテーブル宣言: dp[今いる足場の番号-1]=[最小コスト, 足場の高さ]
	dp = [
		[float('inf'), int(h_)]
		for h_ in input().split()
	]
	dp[0][0] = 0
	dp[1][0] = abs(dp[0][1] - dp[1][1])

	# k個左の足場からジャンプして来た時の最小コストを調べる
	for n in range(2, N):
		height_current = dp[n][1]
		dp[n][0] = min(
			[
				abs( height_current - height ) + cost
				for cost,height in dp[max(0, n-K):n]
			]
		)
	print((dp[N-1][0]))

resolve()
# Original solution
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, h: "List[int]"):
    dp=[2**60]*N
    dp[0]=0
    cost=[[0]*N for _ in range(N)]
    for i in range(N):
        for j in range(i):
            cost[i][j]=abs(h[i]-h[j])

    for i in range(1,N):
        for st in range(max(0,i-K), i):
            dp[i]=min(dp[i], dp[st]+cost[i][st])
    print((dp[N-1]))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, h: "List[int]"):
    dp=[2**60]*N
    dp[0]=0
    for i in range(1,N):
        for st in range(max(0,i-K), i):
            dp[i]=min(dp[i], dp[st]+abs(h[st]-h[i]))
    print((dp[N-1]))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)

if __name__ == '__main__':
    main()
# Original solution
import math
n,k=list(map(int,input().split()))
a=[int(i) for i in input().split()]
b=[0]*n
b[n-2]=abs(a[n-1]-a[n-2])
for i in range(n-3,-1,-1):
    c=[]
    for j in range(i+1,min(n,i+k+1)):
        c.append(abs(a[j]-a[i])+b[j])
    b[i]=min(c)
print((b[0]))

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

# Optimized solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
INF=10**9
dp=[INF for i in range(n)]
dp[0]=0
if k>n-1:
    k=n-1
for i in range(1,n):
    for j in range(1,k+1):
        if i-j<0:
            break
        dp[i]=min(dp[i-j]+abs(h[i]-h[i-j]),dp[i])
print((dp[-1]))
# Original solution
n,k = list(map(int,input().split()))
h = [int(i) for i in input().split()]
dp = [float("inf")]*n
dp[0] = 0
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))
print((dp[-1]))

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

dp = [0] * n

for i in range(n):
  dp_temp = 0
  for j in range(1, k+1):
    if i-j > -1:
      temp = dp[i-j] + abs(h[i]-h[i-j])
      if dp_temp == 0:
        dp_temp = temp
      else:
        if dp_temp > temp:
          dp_temp = temp
    else:
      break
  dp[i] = dp_temp
print((dp[n-1]))
  

# Optimized solution
n, k = list(map(int, input().split()))
h = [int(i) for i in input().split()]

dp = [0] * n

for i in range(n):
  dp_temp = 0
  flag = 0
  for j in range(1, k+1):
    if i-j >= 0:
      temp = dp[i-j] + abs(h[i]-h[i-j])
      if flag == 0:
        dp_temp = temp
        flag = 1
      else:
        if dp_temp >= temp:
          dp_temp = temp
    else:
      break
  dp[i] = dp_temp
print((dp[n-1]))
# Original solution
N_K=list(map(int,input().split()))
N=N_K[0]
K=N_K[1]
cost=list(map(int,input().split()))
#cost=[int(input()) for i in range(N)]

answers={0:0,1:abs(cost[1]-cost[0])}
if K==1:
	S=0
	for i in range(N-1):
		S += abs(cost[i]-cost[i+1])
	print (S)
else:
	for i in range(2,N):
		comp=[]
		if i<K:
			comp=[answers[i-j]+abs(cost[i]-cost[i-j]) for j in range(1,i+1)]
			answers[i]=min(comp)
		else:
			comp=[answers[i-j]+abs(cost[i]-cost[i-j]) for j in range(1,K+1)]
			answers[i]=min(comp)			
	print((answers[N-1]))

# Optimized solution
N_K=list(map(int,input().split()))
N=N_K[0]
K=N_K[1]
cost=list(map(int,input().split()))
answers=[0,abs(cost[1]-cost[0])]
if K==1:
	S=0
	for i in range(N-1):
		S += abs(cost[i]-cost[i+1])
	print (S)
else:
	for i in range(2,N):
		comp=[]
		if i<K:
			comp=[answers[i-j]+abs(cost[i]-cost[i-j]) for j in range(1,i+1)]
			answers.append(min(comp))
		else:
			comp=[answers[i-j]+abs(cost[i]-cost[i-j]) for j in range(1,K+1)]
			answers.append(min(comp))			
	print((answers[-1]))
# Original solution
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[-1]))        


# Optimized solution
import sys
input=sys.stdin.readline
def main():
    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[-1]))        

main()
# Original solution
import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = list(map(int,input().split()))
    H = list(map(int,input().split()))

    dp = [0] * N
    dp[1] = abs(H[1] - H[0])
    for i in range(2,N):
        dp[i] = min(dp[i-j] + abs(H[i] - H[i-j]) for j in range(1,max(K,i)))
    print((dp[-1]))

if __name__ == "__main__":
    main()

# Optimized solution
import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = list(map(int,input().split()))
    H = list(map(int,input().split()))

    dp = [0] * N
    dp[1] = abs(H[1] - H[0])
    for i in range(2,N):
        dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(max(0,i-K),i))
    print((dp[-1]))

if __name__ == "__main__":
    main()
# Original 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):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K, 0), i))
    
print((dp[-1]))

# Optimized solution
def test():
    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):
        dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(max(i-K, 0), i))
 
    print((dp[-1]))
 
if __name__ == '__main__':
    test()
# Original solution
def test():
    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):
        dp[i] = min(dp[i-j]+abs(h[i] - h[i-j]) for j in range(1,min(K,i)+1))
    
    print((dp[N-1]))
    
if __name__ == '__main__':
    test()

# Optimized solution
def test():
    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):
        dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K, 0), i))
    
    print((dp[N-1]))
    
if __name__ == '__main__':
    test()
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = 10 ** 4 + 1
dp = [inf] * N
dp[0] = 0
for i in range(1,N):
    H = h[i]
    dp[i] = min( [dp[j] + abs(h[j] - H) for j in range(max(0, i-K),i)])
print((dp[-1]))


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

if N-1<=K:
    print((abs(H[N-1]-H[0])))
    exit()

inf=10**4+1
dp=[inf]*N
dp[0]=0

for i,h in enumerate(H):
    if i == 0:continue
    m=inf
    M=max(0,i-K)
    dp[i]=min([dpt+abs(h-ht) for ht,dpt in zip(H[M:i],dp[M: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(0,i-K), i))
print((dp[-1]))

# 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()))

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


li_cost = [float("inf")] * n

li_cost[0] = 0

# i = 現在の足場
for i in range(n):
    #for a in range(1, k + 1):
    w = min(k + 1, n - i)
    for a in range(1, w):
        if i + a < n:
            li_cost[i + a] = min(abs(li_h[i] - li_h[i + a]) + li_cost[i], li_cost[i + a])

print((li_cost[-1]))



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

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


dp = [float("inf")] * n

dp[0] = 0

for i in range(1, n):
    w = i - k if i - k > 0 else 0
    dp[i] = min([cost + abs(height - li_h[i]) for cost, height in zip(dp[w:i], li_h[w:i])])

print((dp[-1]))
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [1e9] * n
dp[0] = 0

for i in range(n):
    for j in range(i + 1, i + k + 1, 1):
        if j < n:
            dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]))

print((dp[n - 1]))

# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
dp = [0] * n

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

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


# Optimized solution
n,k,*h=list(map(int,open(0).read().split()))

INF=float("inf")
dp=[INF]*(n)
dp[0]=0

for i in range(n):
    for j in range(1,k+1):
        try:
            dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
        except:
            pass

    # if not i-1<0:
    #     dp[i]=min(dp[i],dp[i-1]+abs(h[i]-h[i-1]))
    # if not i-2<0:
    #     dp[i]=min(dp[i],dp[i-2]+abs(h[i]-h[i-2]))


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

# Optimized solution
import sys
input = sys.stdin.readline

def Main(N, K, h):
    dp =[float('inf')]*N
    dp[0] = 0
    for i in range(N):
        for j in range(1, min(N-i,K+1)):
            dp[i+j] = min(dp[i+j], dp[i] + abs(h[i] - h[i+j]))
    return dp[N-1]

def main():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    print((Main(N, K, h)))

if __name__ == '__main__':
    main()
# Original solution
# https://atcoder.jp/contests/dp/tasks/dp_b

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

dp = [0] * (N)
dp[0] = 0

# 参考：https://atcoder.jp/contests/dp/submissions/10181844
for i in range(1, N):
    tmp = [dp[k] + abs(h[i] - h[k]) for k in range(max(0, i - K), i)]
    dp[i] = min(tmp)
print((dp[-1]))


# Optimized solution
# https://atcoder.jp/contests/dp/tasks/dp_b

def main():
    N, K = list(map(int, input().split()))
    h = [int(x) for x in input().split()]

    dp = [0] * (N)
    dp[0] = 0

    # 参考：https://atcoder.jp/contests/dp/submissions/10181844
    for i in range(1, N):
        tmp = [dp[k] + abs(h[i] - h[k]) for k in range(max(0, i - K), i)]
        dp[i] = min(tmp)
    print((dp[-1]))


if __name__ == '__main__':
    main()
# Original solution
N,K = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [0]+[None]*(N-1) 
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]+[None]*(N-1) 
def func(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)) 

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

# Optimized solution
N,K=list(map(int,input().split()))
H=list(map(int,input().split()))
cost =10**10
DP=[0]*(N+10)
DP[0]=0
DP[1]=abs(H[1]-H[0])
for i in range(2,N):
  cost=10**10
  for k in range(max(i-K,0),i):
    cost=min(cost,(abs(H[i]-H[k])+DP[k]))
  DP[i]=cost
print((DP[N-1]))
# Original solution
n,k = list(map(int,input().split()))
h = list(map(int,input().split())) + [0] * (n*k)
dp = [float('inf')] * (n*k + 1)
dp[0] = 0
for i in range(n):
    for j in range(1,k+1):
        dp[i+j] = min(dp[i+j],dp[i] + abs(h[i] - h[i+j]))
print((dp[n-1]))


# Optimized solution
n,k = list(map(int,input().split()))
h = list(map(int,input().split())) + [0] * (k)
dp = [float('inf')] * (n*k + 1)
dp[0] = 0
for i in range(n):
    for j in range(1,k+1):
        dp[i+j] = min(dp[i+j],dp[i] + abs(h[i] - h[i+j]))
print((dp[n-1]))
# Original solution
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 21 16:49:00 2020

@author: matsui
"""

def cost(i,j):
    return abs(h[i]-h[j])

N,K=list(map(int,input().split()))
h=[int(num) for num in input().split()]

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

# Optimized solution
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 21 16:49:00 2020

@author: matsui
"""



N,K=list(map(int,input().split()))
h=[int(num) for num in input().split()]

dp=[0]*N
dp[1]=abs(h[0]-h[1])
   
def cost(h,N,K,dp):
    for i in range(2,N):
        dp[i]=min([abs(h[i]-h[j])+dp[j] for j in range(max(0, i-K), i)])
    return dp[-1]
    
print((cost(h,N,K,dp)))
# Original solution
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 21 16:49:00 2020

@author: matsui
"""

   
def cost(h,N,K):
    dp=[0]*N
    for i in range(1,N):
        tmp=[]
        for j in range(1,K+1):
            if i-j < 0 : break
            tmp.append(abs(h[i]-h[i-j])+dp[i-j])
        dp[i]=min(tmp)
    return dp[-1]

N,K=list(map(int,input().split()))
h=[int(num) for num in input().split()]

print((cost(h,N,K)))

# Optimized solution
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 21 16:49:00 2020

@author: matsui
"""

   
def cost(h,N,K):
    dp=[0]*N
    dp[1]=abs(h[0]-h[1])
    for i in range(2,N):
        tmp=[abs(h[i]-h[j])+dp[j] for j in range(max(0,i-K),i)]
        dp[i]=min(tmp)
    return dp[-1]

N,K=list(map(int,input().split()))
h=[int(num) for num in input().split()]

print((cost(h,N,K)))
# Original solution
N, K = list(map(int, input().split()))
h = [int(x) for x in input().split()]

inf = 10 ** 5

Cost = [inf] * N
Cost[0] = 0

for i in range(1, N):
    tmp = [Cost[i - k] + abs(h[i] - h[i - k]) if i - k >= 0 else inf for k in range(1,K + 1)]
    Cost[i] = min(tmp)

print((Cost[-1]))

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(x) for x in input().split()]

Cost = [0] * N
Cost[0] = 0

for i in range(1, N):
    tmp = [Cost[k] + abs(h[i] - h[k]) for k in range(max(0, i - K),i)]
    Cost[i] = min(tmp)

print((Cost[-1]))
# Original solution
N, K = list(map(int, input().split()))
h = [int(i) for i in input().split()]
dp = [0] * N


for i in range(1, N):
  c = 10 ** 4 * N
  j=0 if i<K else i-K
  H = h[i]
  for k,l in zip(dp[j:i], h[j:i]):
    c1 = k + abs(l-H)
    c = min(c1, c)
  dp[i] = c
print((dp[-1]))

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

for i,H in enumerate(h):
  if i != 0:
    j=0 if i<K else i-K
    dp[i] = min(k + abs(l-H) for k,l in zip(dp[j:i], h[j:i]))
print((dp[-1]))
# Original solution
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))

dp = [[100000 for i in range(n)]for j in range(n)]

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

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

dp = [1000000000 for i in range(n)]
dp[0] = 0

for i in range(n):
    for j in range(k+1):
        if i+j<n:
            dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j])
print((dp[n-1]))
# Original solution
N,K = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [float('inf')]*(N+10000)
dp[0] = 0
for i in range(N):
    for j in range(K+1):
        try:
            dp[i+j] = min(dp[i]+abs(h[i]-h[i+j]),dp[i+j])
        except Exception:
            continue
print((dp[N-1]))

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

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

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

dp = [float('inf')]*n
for i in range(n):
    if i == 0:
        dp[i] = 0
        continue    
    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):
  L = [dp[j] +abs(h[i] - h[j]) for j in range(max(0,i-K),i)]
  dp[i] = min(L)
print((dp[-1]))

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

# Optimized solution
N,K = list(map(int,input().split()))
H = list(map(int,input().split()))
 
def main(N,K,H):
    dp = [0]*N
    for i in range(1,N):
        L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
        dp[i] = min(L)
    print((dp[-1]))
main(N,K,H)
# Original 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):
        a = (dp[j] + abs(h[i] - h[j]) for j in range(max(0,i-k),i))        
        dp[i] = min(a)

    print((dp[-1]))

main()


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

dp = [0] * n


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

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