import numpy as np

from numba import njit



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

h = np.array(list(map(int, input().split())), np.int64)

INF = 10 ** 18





@njit

def f():

    dp = np.full(n, INF, np.int64)

    dp[0] = 0



    for i, e in enumerate(h[1:], 1):

        lb = np.maximum(0, i - k)

        dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - e))



    ans = dp[-1]

    return ans





ans = f()

print(ans)

import numpy as np

from numba import njit



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

h = np.array(list(map(int, input().split())), np.int64)

INF = 10 ** 18





@njit

def f(n, k, h):

    dp = np.full(n, INF, np.int64)

    dp[0] = 0



    for i in range(1, n):

        lb = np.maximum(0, i - k)

        dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - h[i]))



    ans = dp[-1]

    return ans





ans = f(n, k, h)

print(ans)

import numpy as np
from numba import njit

n, k = list(map(int, input().split()))
h = np.array(list(map(int, input().split())), np.int64)
INF = 10**18


@njit
def f():
    dp = np.full(n, INF, np.int64)
    dp[0] = 0
    for i, e in enumerate(h[1:], 1):
        lb = np.maximum(0, i - k)
        dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - e))
    ans = dp[-1]
    return ans


ans = f()
print(ans)

import numpy as np
from numba import njit

n, k = list(map(int, input().split()))
h = np.array(list(map(int, input().split())), np.int64)
INF = 10**18


@njit
def f(n, k, h):
    dp = np.full(n, INF, np.int64)
    dp[0] = 0
    for i in range(1, n):
        lb = np.maximum(0, i - k)
        dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - h[i]))
    ans = dp[-1]
    return ans


ans = f(n, k, h)
print(ans)

-def f(): +def f(n, k, h): -    for i, e in enumerate(h[1:], 1): +    for i in range(1, n): -        dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - e)) +        dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - h[i])) -ans = f() +ans = f(n, k, h)