import heapq

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

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



dp = [0] * N

dp[1] = dp[0] + abs(h[1]-h[0])



for i in range(2, N):

    compare = []

    heapq.heapify(compare)

    cnt = min(K, i)

    for j in range(cnt):

        heapq.heappush(compare, dp[i-j-1] + abs(h[i]-h[i-j-1]))

    dp[i] = heapq.heappop(compare)



print((dp[-1]))

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

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



dp = [0] * N

dp[1] = dp[0] + abs(h[1]-h[0])



for i in range(2, N):

    compare = []

    cnt = min(K, i)

    for j in range(cnt):

        compare.append(dp[i-j-1] + abs(h[i]-h[i-j-1]))

    dp[i] = min(compare)



print((dp[-1]))

import heapq

N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [0] * N
dp[1] = dp[0] + abs(h[1] - h[0])
for i in range(2, N):
    compare = []
    heapq.heapify(compare)
    cnt = min(K, i)
    for j in range(cnt):
        heapq.heappush(compare, dp[i - j - 1] + abs(h[i] - h[i - j - 1]))
    dp[i] = heapq.heappop(compare)
print((dp[-1]))

N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [0] * N
dp[1] = dp[0] + abs(h[1] - h[0])
for i in range(2, N):
    compare = []
    cnt = min(K, i)
    for j in range(cnt):
        compare.append(dp[i - j - 1] + abs(h[i] - h[i - j - 1]))
    dp[i] = min(compare)
print((dp[-1]))

-import heapq - -    heapq.heapify(compare) -        heapq.heappush(compare, dp[i - j - 1] + abs(h[i] - h[i - j - 1])) -    dp[i] = heapq.heappop(compare) +        compare.append(dp[i - j - 1] + abs(h[i] - h[i - j - 1])) +    dp[i] = min(compare)