import numpy as np,sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))

N,K = LI()

h = np.array(LI())

dp = np.full(N,10**10,dtype=int)

dp[0] = 0

for i in range(1,N):

    dp[i:i+K] = np.fmin(dp[i:i+K],dp[i-1]+np.abs(h[i:i+K]-h[i-1]))

print((dp[-1]))

import itertools,sys

def I(): return int(sys.stdin.readline().rstrip())

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))

N,K = LI()

h = LI()

dp = [float('INF')]*N

dp[0] = 0

for i,j in itertools.product(list(range(N)),list(range(1,K+1))):

    if i+j<=N-1:

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

print((dp[-1]))

import numpy as np, sys


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))


N, K = LI()
h = np.array(LI())
dp = np.full(N, 10**10, dtype=int)
dp[0] = 0
for i in range(1, N):
    dp[i : i + K] = np.fmin(dp[i : i + K], dp[i - 1] + np.abs(h[i : i + K] - h[i - 1]))
print((dp[-1]))

import itertools, sys


def I():
    return int(sys.stdin.readline().rstrip())


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))


N, K = LI()
h = LI()
dp = [float("INF")] * N
dp[0] = 0
for i, j in itertools.product(list(range(N)), list(range(1, K + 1))):
    if i + j <= N - 1:
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))
print((dp[-1]))

-import numpy as np, sys +import itertools, sys + + +def I(): +    return int(sys.stdin.readline().rstrip()) -h = np.array(LI()) -dp = np.full(N, 10**10, dtype=int) +h = LI() +dp = [float("INF")] * N -for i in range(1, N): -    dp[i : i + K] = np.fmin(dp[i : i + K], dp[i - 1] + np.abs(h[i : i + K] - h[i - 1])) +for i, j in itertools.product(list(range(N)), list(range(1, K + 1))): +    if i + j <= N - 1: +        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))