import heapq



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

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

dp=[0]*N

for i in range(N):

    he=[]

    if i == 0:

        dp[i] = 0

    else:

        for j in range(K):

            heapq.heappush(he, dp[i-(j+1)]+abs(h[i]-h[i-(j+1)]) if i-(j+1) >= 0 else float('inf'))

        dp[i] = heapq.heappop(he)

print((dp[N-1]))

import sys

sys.setrecursionlimit(10**9)

INF=10**18

MOD=10**9+7

def input(): return sys.stdin.readline().rstrip()



def main():

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

    h=list(map(int,input().split()))+[INF]*K

    dp=[INF]*(2*10**5)

    dp[0]=0

    for i in range(N):

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

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

    print((dp[N-1]))



if __name__ == '__main__':

    main()

import heapq

N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [0] * N
for i in range(N):
    he = []
    if i == 0:
        dp[i] = 0
    else:
        for j in range(K):
            heapq.heappush(
                he,
                dp[i - (j + 1)] + abs(h[i] - h[i - (j + 1)])
                if i - (j + 1) >= 0
                else float("inf"),
            )
        dp[i] = heapq.heappop(he)
print((dp[N - 1]))

import sys

sys.setrecursionlimit(10**9)
INF = 10**18
MOD = 10**9 + 7


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split())) + [INF] * K
    dp = [INF] * (2 * 10**5)
    dp[0] = 0
    for i in range(N):
        for k in range(1, K + 1):
            dp[i + k] = min(dp[i + k], dp[i] + abs(h[i + k] - h[i]))
    print((dp[N - 1]))


if __name__ == "__main__":
    main()

-import heapq +import sys -N, K = list(map(int, input().split())) -h = list(map(int, input().split())) -dp = [0] * N -for i in range(N): -    he = [] -    if i == 0: -        dp[i] = 0 -    else: -        for j in range(K): -            heapq.heappush( -                he, -                dp[i - (j + 1)] + abs(h[i] - h[i - (j + 1)]) -                if i - (j + 1) >= 0 -                else float("inf"), -            ) -        dp[i] = heapq.heappop(he) -print((dp[N - 1])) +sys.setrecursionlimit(10**9) +INF = 10**18 +MOD = 10**9 + 7 + + +def input(): +    return sys.stdin.readline().rstrip() + + +def main(): +    N, K = list(map(int, input().split())) +    h = list(map(int, input().split())) + [INF] * K +    dp = [INF] * (2 * 10**5) +    dp[0] = 0 +    for i in range(N): +        for k in range(1, K + 1): +            dp[i + k] = min(dp[i + k], dp[i] + abs(h[i + k] - h[i])) +    print((dp[N - 1])) + + +if __name__ == "__main__": +    main()