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

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



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

for i, hi in enumerate(H):

    if i == 0:

        continue

    r = max(0, i - K)

    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))

print((dp[N-1]))
N, K = list(map(int, input().split()))

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

 

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

for i in range(1, N):

    min_v = 1050000000

    for j in range(1, K+1):

        if i - j >= 0:

            min_v = min(min_v, dp[i - j] + abs(H[i] - H[i - j]))

    dp[i] = min_v

print((dp[N-1]))
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
dp = [0 for _ in range(N)]
for i, hi in enumerate(H):
    if i == 0:
        continue
    r = max(0, i - K)
    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))
print((dp[N - 1]))

N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
dp = [0 for _ in range(N)]
for i in range(1, N):
    min_v = 1050000000
    for j in range(1, K + 1):
        if i - j >= 0:
            min_v = min(min_v, dp[i - j] + abs(H[i] - H[i - j]))
    dp[i] = min_v
print((dp[N - 1]))

-for i, hi in enumerate(H): -    if i == 0: -        continue -    r = max(0, i - K) -    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i])) +for i in range(1, N): +    min_v = 1050000000 +    for j in range(1, K + 1): +        if i - j >= 0: +            min_v = min(min_v, dp[i - j] + abs(H[i] - H[i - j])) +    dp[i] = min_v