import sys

sys.setrecursionlimit(10**6)



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

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

dp = [None] * N



def solve(n):

    if dp[n] is not None:

        return dp[n]



    if n == 0:

        dp[n] = 0

    elif n == 1:

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

    else:

        ans = 10**10

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

            if n - i < 0:

                break

            if dp[n - i] is not None:

                temp = dp[n - i]

            else:

                temp = solve(n - i)

            ans = min(ans, temp + abs(h[n - i] - h[n]))

        dp[n] = ans



    return dp[n]



print((solve(N-1)))
N, K = list(map(int, input().split()))

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

# dp = [None] * N

dp = [10**9] * N



for i in range(N):

    if i == 0:

        dp[0] = 0

    elif i == 1:

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

    else:

        # ans = 10**10

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

            if j > i:

                break

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

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

        # dp[i] = ans



print((dp[N-1]))
import sys

sys.setrecursionlimit(10**6)
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [None] * N


def solve(n):
    if dp[n] is not None:
        return dp[n]
    if n == 0:
        dp[n] = 0
    elif n == 1:
        dp[n] = abs(h[0] - h[1])
    else:
        ans = 10**10
        for i in range(1, K + 1):
            if n - i < 0:
                break
            if dp[n - i] is not None:
                temp = dp[n - i]
            else:
                temp = solve(n - i)
            ans = min(ans, temp + abs(h[n - i] - h[n]))
        dp[n] = ans
    return dp[n]


print((solve(N - 1)))

N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
# dp = [None] * N
dp = [10**9] * N
for i in range(N):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(h[0] - h[1])
    else:
        # ans = 10**10
        for j in range(1, K + 1):
            if j > i:
                break
            # ans = min(ans, dp[i-j] + abs(h[i-j]-h[i]))
            dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i]))
        # dp[i] = ans
print((dp[N - 1]))

-import sys - -sys.setrecursionlimit(10**6) -dp = [None] * N - - -def solve(n): -    if dp[n] is not None: -        return dp[n] -    if n == 0: -        dp[n] = 0 -    elif n == 1: -        dp[n] = abs(h[0] - h[1]) +# dp = [None] * N +dp = [10**9] * N +for i in range(N): +    if i == 0: +        dp[0] = 0 +    elif i == 1: +        dp[1] = abs(h[0] - h[1]) -        ans = 10**10 -        for i in range(1, K + 1): -            if n - i < 0: +        # ans = 10**10 +        for j in range(1, K + 1): +            if j > i: -            if dp[n - i] is not None: -                temp = dp[n - i] -            else: -                temp = solve(n - i) -            ans = min(ans, temp + abs(h[n - i] - h[n])) -        dp[n] = ans -    return dp[n] - - -print((solve(N - 1))) +            # ans = min(ans, dp[i-j] + abs(h[i-j]-h[i])) +            dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i])) +        # dp[i] = ans +print((dp[N - 1]))