Buggy python submission (verdict: Time Limit Exceeded):
def examA():
    N = I()
    H = LI()
    dp = [inf]*(N)
    dp[0] = 0
    for i in range(N-1):
        dp[i+1] = min(dp[i+1],dp[i]+abs(H[i+1]-H[i]))
        if i<N-2:
            dp[i + 2] = min(dp[i + 2], dp[i]+abs(H[i + 2] - H[i]))
    ans = dp[-1]
    print(ans)
    return

def examB():
    N, K = LI()
    H = LI()
    dp = [inf]*N
    dp[0] = 0
    for i in range(N):
        for k in range(1,K+1):
            if i+k>=N:
                break
            if dp[i+k]>dp[i]+abs(H[i+k]-H[i]):
                dp[i+k] = dp[i]+abs(H[i+k]-H[i])
    ans = dp[-1]
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Fixed submission:
N,K = map(int, input().split())
h = list(map(int, input().split()))
dp = [0]*N
ans = int(0)
c = int(0)
if N < K:
    c = N-1
else:
    c = K
for i in range(c):
    dp[i+1] = abs(h[i+1]-h[0])

for i in range(2,N):
    if i<K:
        c = i
    else:
        c = K
    dpcandi = [0] * c
    for j in range(c):
        dpcandi[j] = dp[i-j-1] + abs(h[i]-h[i-j-1])
    dp[i] = min(dpcandi)

ans = dp[N-1]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def examA():
    N = I()
    H = LI()
    dp = [inf]*(N)
    dp[0] = 0
    for i in range(N-1):
        dp[i+1] = min(dp[i+1],dp[i]+abs(H[i+1]-H[i]))
        if i<N-2:
            dp[i + 2] = min(dp[i + 2], dp[i]+abs(H[i + 2] - H[i]))
    ans = dp[-1]
    print(ans)
    return

def examB():
    N, K = LI()
    H = LI()
    dp = [inf]*N
    dp[0] = 0
    for i in range(N):
        for k in range(1,K+1):
            if i+k>=N:
                break
            dp[i+k] = min(dp[i+k],dp[i]+abs(H[i+k]-H[i]))
    ans = dp[-1]
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Fixed submission:
N,K = map(int, input().split())
h = list(map(int, input().split()))
dp = [0]*N
ans = int(0)
c = int(0)
if N < K:
    c = N-1
else:
    c = K
for i in range(c):
    dp[i+1] = abs(h[i+1]-h[0])

for i in range(2,N):
    if i<K:
        c = i
    else:
        c = K
    dpcandi = [0] * c
    for j in range(c):
        dpcandi[j] = dp[i-j-1] + abs(h[i]-h[i-j-1])
    dp[i] = min(dpcandi)

ans = dp[N-1]

print(ans)

Buggy python submission (verdict: Runtime Error):
N,K = map(int, input().split())
h = list(map(int, input().split()))
dp = [0]*N
ans = int(0)
for i in range(K):
    dp[i+1] = abs(h[i+1]-h[0])

for i in range(2,N):
    if i<K:
        c = i
    else:
        c = K
    dpcandi = [0] * c
    for j in range(c):
        dpcandi[j] = dp[i-j-1] + abs(h[i]-h[i-j-1])
    dp[i] = min(dpcandi)

ans = dp[N-1]

print(ans)

Fixed submission:
N,K = map(int, input().split())
h = list(map(int, input().split()))
dp = [0]*N
ans = int(0)
c = int(0)
if N < K:
    c = N-1
else:
    c = K
for i in range(c):
    dp[i+1] = abs(h[i+1]-h[0])

for i in range(2,N):
    if i<K:
        c = i
    else:
        c = K
    dpcandi = [0] * c
    for j in range(c):
        dpcandi[j] = dp[i-j-1] + abs(h[i]-h[i-j-1])
    dp[i] = min(dpcandi)

ans = dp[N-1]

print(ans)