CodeEff/ECCO huggingface dataset:
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [None]*n
dp[0], dp[1] = 0, abs(h[0]-h[1])
for i in range(2, n):
    di = 10**9
    hi = h[i]
    for j in range(max(0, i-k), i):
        dj = dp[j] + abs(h[j]-hi)
        if di > dj:
            di = dj
    dp[i] = di
print((dp[-1]))

# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [None]*n
    dp[0], dp[1] = 0, abs(h[0]-h[1])
    for i in range(2, n):
        di = 10**9
        hi = h[i]
        for j in range(max(0, i-k), i):
            dj = dp[j] + abs(h[j]-hi)
            if di > dj:
                di = dj
        dp[i] = di
    print((dp[-1]))

if __name__ == "__main__":
    main()
# Original solution
def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [None]*n
    dp[0], dp[1] = 0, abs(h[0]-h[1])
    for i in range(2, n):
        di = 10**9
        hi = h[i]
        for j in range(max(0, i-k), i):
            dj = dp[j] + abs(h[j]-hi)
            if di > dj:
                di = dj
        dp[i] = di
    print((dp[-1]))

if __name__ == "__main__":
    main()

# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    m = [0 if i<k else i-k for i in range(2, n)]
    dp = [None]*n
    dp[0], dp[1] = 0, abs(h[0]-h[1])
    for i, (hi, mi) in enumerate(zip(h[2:], m), 2):
        di = 10**9
        for dj, hj in zip(dp[mi:i], h[mi:i]):
            d = dj + abs(hj-hi)
            if di > d:
                di = d
        dp[i] = di
    print((dp[-1]))

if __name__ == "__main__":
    main()
# Original solution
def test():
    N, K = list(map(int, input().split()))
    H = list(map(int, input().split()))
    dp = [float('inf')]*N
    dp[0] = 0
 
    for i in range(1, N):
        dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(max(i-K, 0), i))
 
    print((dp[-1]))
 
if __name__ == '__main__':
    test()

# Optimized solution
n,k=list(map(int,input().split()))
H=list(map(int,input().split()))
 
dp=[float('inf') for _ in range(n)]
dp[0]=0
for i,hi in enumerate(H[1:],1):
  s=max(0,i-k)
  dp[i]=min([aj + abs(hi-hj) for hj,aj in zip(H[s:i],dp[s:i])])
print((dp[-1]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
INF = 1000000007
dp = [INF] * (N + K)
dp[0] = 0
for i in range(N):
    for j in range(1, K + 1):
        if i + j < N:
            dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))

print((dp[N - 1]))


# Optimized solution
def input_li():
    return list(map(int, input().split()))

def input_int():
    return int(eval(input()))

N, K = input_li()
h_li = input_li()
dp = [10 ** 9 + 7] * N
dp[0] = 0
for i in range(N):
    for j in range(1, K + 1):
        if i + j < N:
            dp[i + j] = min(dp[i + j], dp[i] + abs(h_li[i] - h_li[i + j]))
print((dp[-1]))
# Original solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
c=[0]*n
for i in range(1,n):
    c[i]=min([c[j]+abs(h[i]-h[j]) for j in range(max(0,i-k),i)])
print((c[-1]))

# Optimized solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
c=[0]*n
for i in range(1,n):
    c[i]=min([cj+abs(h[i]-hj) for cj,hj in zip(c[max(0,i-k):i],h[max(0,i-k):i])])
print((c[-1]))
# Original solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
dp=[0]*n
dp[0]=0
for i in range(1,n):
    x=l[i]
    dp[i]=min([dp[j]+abs(x-l[j]) for j in range(max(0,i-k),i)])
print((dp[n-1]))

# Optimized solution
n,k=list(map(int,input().split()))
h=list(map(int, input().split()))
v=[0]*n
for i in range(1,n):
	u = h[i]
	v[i] = min([v[j] + abs(h[j]-u) for j in range(max(0, i-k), i)])
print((v[n-1]))
# Original solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
d=[0]*n
d[0]=0
for i in range(1,n):
    x=l[i]
    d[i]=min([d[j]+abs(x-l[j]) for j in range(max(0,i-k),i)])
print((d[n-1]))

# Optimized solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
d=[0]*n
d[0]=0
for i in range(1,n):
    x=l[i]
    d[i]=min([d[j]+abs(l[j]-x) for j in range(max(0,i-k),i)])
print((d[n-1]))
# Original solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
d=[0]*n
d[0]=0
for i in range(1,n):
    x=l[i]
    d[i]=min([d[j]+abs(l[j]-x) for j in range(max(0,i-k),i)])
print((d[n-1]))

# Optimized solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
d=[0]*n
for i in range(1,n):
    x=l[i]
    d[i]=min([d[j]+abs(l[j]-x) for j in range (max (0,i-k),i)])
print((d[n-1]))
# Original solution
N, K = list(map(int, input().split()))
h = [int(x) for x in input().split()]

dp = [float('inf')] * N
dp[0] = 0

Func = min
for i in range(N-1):
    for j in range(i+1, i+K+1):
        if j >= N:
            break
        dp[j] = Func(dp[j], dp[i] + abs(h[i] - h[j]))

print((dp[N-1]))

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(x) for x in input().split()]

dp = [float('inf')] * N
dp[0] = 0

Func = min
for i, hi in enumerate(h):
    if i == 0:
        continue
    x = (0 if i-K < 0 else i-K)
    dp[i] = min([dpj + abs(hi - hj) for dpj, hj in zip(dp[x:i], h[x:i])])

print((dp[-1]))
# Original solution
import sys


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


def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [0] * n
    dp[0] = 0
    dp[1] = abs(h[0] - h[1])
    if n == 2:
        print((dp[1]))
    else:
        for i in range(2, n):
            if i <= k:
                dp[i] = min(abs(h[i] - h[i - j]) + dp[i - j] for j in range(1, i + 1))
            else:
                dp[i] = min(abs(h[i] - h[i - j]) + dp[i - j] for j in range(1, k + 1))
        print((dp[n - 1]))


if __name__ == "__main__":
    main()


# Optimized solution
import sys


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


def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [0] * n
    dp[0] = 0
    dp[1] = abs(h[0] - h[1])

    for i in range(2, n):
        dp[i] = min(abs(h[i] - h[j]) + dp[j] for j in range(max(0, i - k), i))
    print((dp[n - 1]))


if __name__ == "__main__":
    main()
# Original solution
INF = 10**9

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

dp = [INF] * n
dp[0] = 0

for i in range(1, n):
    l = i if i < k else k
    a = INF
    hsi = hs[i]
    for m in range(1, l + 1):
        j = i - m
        v = dp[j] + abs(hs[j] - hsi)
        if a > v:
            a = v
    dp[i] = a
print((dp[-1]))


# Optimized solution
n, k = list(map(int, input().split(' ')))

hs = list(map(int, input().split(' ')))

dp = [float('inf')] * (n + k + 1)
dp[0] = 0

for i in range(n - 1):
    for j in range(1, k + 1):
        if i + j < n:
            dp[i + j] = min([
                dp[i + j],
                dp[i] + abs(hs[i] - hs[i + j])
            ])

print((dp[n - 1]))
# Original solution
n,k,*h=list(map(int,open(0).read().split()))
a=[0]*n
for i in range(1,n):
    j=max(0,i-k)
    a[i]=min(A+abs(h[i]-H) for A,H in zip(a[j:i],h[j:i]))
print((a[-1]))

# Optimized solution
n,k,*h=list(map(int,open(0).read().split()))
a=[0]*n
for i in range(1,n):
    j=max(0,i-k)
    HH=h[i]
    a[i]=min(A+abs(HH-H) for A,H in zip(a[j:i],h[j:i]))
print((a[-1]))
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())

def main():
  dp=[inf for _ in range(10**5+10)]
  dp[0]=0
  n,k=LI()
  l=LI()

  for i in range(n):
    for j in range(1,k+1):
      if i+j<n:
        # print(dp[i],abs(l[i]-l[i+j]))
        dp[i+j]=min(dp[i+j],dp[i]+abs(l[i]-l[i+j]))
    # print(dp[:n])

  return dp[n-1]

# main()
print((main()))


# Optimized solution
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())

def main():
  n,k=LI()
  h=LI()
  h=[0]+h
  h+=[inf]*k

  dp=[inf]*114514
  dp[1]=0
  for i in range(1,n):
    for j in range(1,k+1):
      dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
  # print(dp[:10])
  return dp[n]

# main()
print((main()))
# Original solution
MAX = 10**9

n, k = list(map(int, input().split()))
cost = list(map(int, input().split()))
dp = [MAX] * (n)
dp[0] = 0

for i in range(1, n):
    for j in range(max(0, i-k), i+1):
                dp[i] = min(dp[i] ,dp[j] + abs(cost[i] - cost[j]))

print((dp[-1]))


# Optimized solution
#!/usr/bin/env pypy

import sys
inp = sys.stdin.readline
out = print
MAX = 10**9
 
n, k = map(int, inp().split())
cost = list(map(int, inp().split()))
dp = [MAX] * (n)
dp[0] = 0
 
for i in range(1, n):
    dp[i] = min(dp[j] + abs(cost[i] - cost[j]) for j in range(max(0, i - k), i))
 
out(dp[-1])
# Original solution
n, k = list(map(int, input().split()))
h_list = list(map(int, input().split()))
costs = [0] * n
costs[1] = abs(h_list[0] - h_list[1])
start = 0
for i in range(2, n):
    if h_list[i] == 0:
        costs[i] = costs[i-1]
        continue
    else:
        v = h_list[i]
    tmp = []
    start = max(0, i-k)
    for l in range(start, i):
        tmp.append(abs(v - h_list[l])+costs[l])
        if abs(v - h_list[l]) == 0:
            break
    costs[i] = min(tmp)
print((costs[-1]))


# Optimized solution
n, k = list(map(int, input().split()))
h_list = list(map(int, input().split()))
costs = [0] * n
costs[1] = abs(h_list[0] - h_list[1])
start = 0
for i in range(2, n):
    tmp = []
    v = h_list[i]
    start = max(0, i-k)
    costs[i] = min(abs(v - h_j) + c for h_j, c in zip(h_list[start:i], costs[start:i]))
    # for l in range(start, i):
    #     tmp.append(abs(v - h_list[l])+costs[l])
    #     if abs(v - h_list[l]) == 0:
    #         break
    # costs[i] = min(tmp)
print((costs[-1]))
# Original solution
import math
import operator as op
from functools import reduce
from fractions import Fraction as frac

def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, list(range(n, n-r, -1)), 1)
    denom = reduce(op.mul, list(range(1, r+1)), 1)
    return numer / denom
"""
int(input())
map(int,input().split())
list(map(int,input().split()))
"""
# t=int(input())
t=1
for term in range(t):
    nk=list(map(int,input().split()))
    h=list(map(int,input().split()))
    n=nk[0]
    k=nk[1]
    dp=[10**9]*n
    dp[0]=0
    dp[1]=abs(h[1]-h[0])
    for i in range(2,n):
        dp[i]=min(dp[j]+abs(h[i]-h[j]) for j in range(max(0,i-k),i))
    print((dp[n-1]))

# Optimized solution
import sys
def input():
    return sys.stdin.readline().strip()
 
def main():
    nk=list(map(int,input().split()))
    h=list(map(int,input().split()))
    n=nk[0]
    k=nk[1]
    dp=[10**9]*n
    dp[0]=0
    dp[1]=abs(h[1]-h[0])
    for i in range(2,n):
        dp[i]=min(dp[j]+abs(h[i]-h[j]) for j in range(max(0,i-k),i))
        # for j in range(max(0,i-k),i):
        #     dp[i]=min(dp[j]+abs(h[i]-h[j]),dp[i])
 
    print((dp[-1]))
 
if __name__ == "__main__":
    main()
# Original solution
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
# import numpy as np
starttime = time.time()
# import numpy as np
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return list(map(int, data().split()))
def ssp(): return list(map(str, data().split()))
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

try:
    # sys.setrecursionlimit(int(pow(10,6)))
    sys.stdin = open("input.txt", "r")
    # sys.stdout = open("../output.txt", "w")
except:
    pass

n,k=L()
H=L()
dp=[10**10 for i in range(n)]
dp[0]=0
dp[1]=abs(H[1]-H[0])
for i in range(2,n):
    for j in range(1,k+1):
        dp[i]=min(dp[i-j]+abs(H[i]-H[i-j]),dp[i])
print((dp[-1]))



endtime = time.time()
# print(f"Runtime of the program is {endtime - starttime}")



# Optimized solution
import sys
def input():
    return sys.stdin.readline().strip()
 
def main():
    nk=list(map(int,input().split()))
    h=list(map(int,input().split()))
    n=nk[0]
    k=nk[1]
    dp=[10**9]*n
    dp[0]=0
    dp[1]=abs(h[1]-h[0])
    for i in range(2,n):
        dp[i]=min(dp[j]+abs(h[i]-h[j]) for j in range(max(0,i-k),i))
        # for j in range(max(0,i-k),i):
        #     dp[i]=min(dp[j]+abs(h[i]-h[j]),dp[i])
 
    print((dp[-1]))
 
if __name__ == "__main__":
    main()
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [0] * n
dp[0] = 0
for i in range(1, n):
    pre = min(k,i)
    costs_pre = [0] * pre
    for j in range(1,pre+1):
        costs_pre[j-1] = dp[i-j] + abs(h[i-j]-h[i])
    dp[i] = min(costs_pre)
print((dp[n-1]))

# Optimized solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [0] * n
for i in range(1, n):
    mincost = 10 ** 9
    for j in range(1, min(i+1, k+1)):
        mincost = min(mincost, dp[i-j] + abs(h[i]-h[i-j]))
    dp[i] = mincost
print((dp[n-1]))
# Original solution
from sys import stdin, setrecursionlimit as srl
from threading import stack_size, Thread
from math import *
from collections import *
from heapq import *

srl(int(1e9)+7)
stack_size(int(1e8))

'''
deque
1) append
2) appendleft
3) pop
4) popleft
5) insert, remove, index, count, extend, extendleft, reverse
6) rotate           # negative value rotates to the left , positive to the right
'''

'''
heapq

minheap by default but can be used as maxheap by pushing elements after multiplying them with -1

1) heapify ( iterable )
2) heappush ( heap, element)
3) heappop (heap)                   # smallest is removed
4) heappushpop (heap, element)
5) heapreplace (heap, element)      # same as heappushpop but first popped then pushed
'''

def solve():
    #start here
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [0 for i in range(n)]
    for i in range(1, n):
        x = float('inf')
        for j in range(1, 1 + min(k, i)):
            x = min(x, dp[i-j] + abs(h[i]-h[i-j]))
        dp[i] = x
    print((dp[n-1]))


Thread(target=solve).start()

# Optimized solution
def solve():
    #start here
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [0 for i in range(n)]
    for i in range(1, n):
        x = float('inf')
        for j in range(1, 1 + min(k, i)):
            x = min(x, dp[i-j] + abs(h[i]-h[i-j]))
        dp[i] = x
    print((dp[n-1]))

solve()
# Original solution
N, K = list(map(int, input().split()))
H = [int(i) for i in input().split()]

dp = [0 for _ in range(N)]
dp[0] = 0
for i in range(1, N):
    dp[i] = min([dp[j] + abs(H[j] - H[i]) for j in range(max(0, i-K), i)])
print((dp[N-1]))

# Optimized solution
def main():    
    N, K = list(map(int, input().split()))
    H = [int(i) for i in input().split()]

    dp = [0 for _ in range(N)]
    dp[0] = 0
    for i in range(1, N):
        dp[i] = min([dp[j] + abs(H[j] - H[i]) for j in range(max(0, i-K), i)])
    print((dp[N-1]))

if __name__ == '__main__':
    main()
# Original solution
ni = lambda: int(eval(input()))
nm = lambda: list(map(int, input().split()))
nl = lambda: list(map(int, input().split()))

n,k=nm()
h=nl()

dp = [10**10] * n
dp[0] = 0

for i in range(n):
  for j in range(max(0,i-k),i):
    dp[i] = min(dp[i], dp[j] + abs(h[j]-h[i]))

print((dp[n-1]))


# Optimized solution
ni = lambda: int(eval(input()))
nm = lambda: list(map(int, input().split()))
nl = lambda: list(map(int, input().split()))

n,k=nm()
h=nl()

dp = [10**10] * n
dp[0] = 0

for i in range(n):
  for j in range(i+1,min(i+k+1,n)):
    dp[j] = min(dp[j], dp[i] + abs(h[j]-h[i]))

print((dp[n-1]))
# Original solution
N,K = list(map(int,input().split()))
H = list(map(int,input().split()))
 
 
 
# DPテーブル
DP = [float('inf')]*N
 
# 初期条件
DP[0] = 0
 
for i in range(0,N):
  for j in range(1,K+1):
    if i+j >= N:
      break
    DP[i+j] = min(DP[i+j],DP[i]+abs(H[i]-H[i+j]))
print((DP[N-1]))

# Optimized solution
N,K = list(map(int,input().split()))
H = list(map(int,input().split()))
 
 
 
# DPテーブル
DP = [float('inf')]*N
 
# 初期条件
DP[0] = 0
DP[1] = abs(H[1]-H[0])

for i in range(2,N):
    DP[i] = min(DP[i-j]+abs(H[i]-H[i-j]) for j in range(1,min(i,K)+1))
print((DP[N-1]))
# Original solution
INF = float('inf')
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [INF for _ in range(N)]
dp[0] = 0
for i in range(N):
    for j in range(1, K+1):
        if i + j < N:
            dp[i+j] = min(dp[i+j], dp[i] + abs(h[i+j] - h[i]))

print((dp[N-1]))


# Optimized solution
INF = float('inf')
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [INF for _ in range(N)]
dp[0] = 0
for i in range(N):
    for j in range(K):
        j += 1
        if i + j < N:
            dp[i+j] = min(dp[i+j], dp[i] + abs(h[i+j] - h[i]))

print((dp[N-1]))
# Original solution
n,k=list(map(int,input().split()))
h=[int(l) for l in input().split()]
dp=[0,abs(h[0]-h[1])]
for i in range(2,n):
    ans=10**15
    num=i-k
    if num<0:
        num=-1
    else:
        num-=1
    for j in range(i-1,num,-1):
        ans=min(abs(h[i]-h[j])+dp[j],ans)
    dp.append(ans)
print((dp[n-1]))


# Optimized solution
from sys import stdin,stdout
n,k=list(map(int,stdin.readline().split()))
h=[int(l) for l in stdin.readline().split()]
dp=[0,abs(h[0]-h[1])]
for i in range(2,n):
    ans=10**15
    num=i-k
    if num<0:
        num=-1
    else:
        num-=1
    for j in range(i-1,num,-1):
        ans=min(abs(h[i]-h[j])+dp[j],ans)
    dp.append(ans)
print((dp[n-1]))
# Original solution
def a():
    N, K = [int(n) for n in input().split()]
    h = [int(n) for n in input().split()]
    # dp[i]はi番目までたどり着くのに必要な最小コスト
    # dp[0] = 0
    # dp[1] = |h[1] - h[0]|
    # dp[i] = min(dp[0]+h[i]-h[0], dp[1]+h[i]-h[1], ...)
    dp = [0] * N
    dp[0] = 0
    dp[1] = abs(h[1] - h[0])
    for i in range(2, N):
        dp[i] = min([dp[k] + abs(h[k] - h[i]) for k in range(max(i - K, 0), i)])
    print((dp[N-1]))

if __name__ == "__main__":
    a()

# Optimized solution
def main():
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    dp = [-1]*N
    dp[0] = 0
    for i in range(1,N):
        res = -1
        for j in range(1, K+1):
            if i - j < 0:
                break
            new = dp[i-j] + abs(A[i] - A[i-j])
            if res == -1:
                res = new
                continue
            res = min(res, new)
        dp[i] = res
    print((dp[N-1]))
main()
# Original solution
import sys

sys.setrecursionlimit(100000)

N, K = list(map(int, input().split()))
H = list(map(int ,input().split()))
cost = [-1]*N

def sumOfCost(i):
  if cost[i] >= 0:
    return cost[i]
  elif i == 0:
    soc = 0
  else:
    soc = 1000000000
    for j in range(1, min(i+1,K+1)):
      if i-j < 0:
        break
      soc = min(soc, sumOfCost(i-j)+abs(H[i]-H[i-j]))
  cost[i] = soc
  return soc

print(sumOfCost(N-1))
    



# Optimized solution
import sys

sys.setrecursionlimit(100000)

N, K = list(map(int, input().split()))
H = list(map(int ,input().split()))
cost = [-1]*N

def sumOfCost(i):
  if cost[i] >= 0:
    return cost[i]
  elif i == 0:
    soc = 0
  else:
    soc = 1000000000
    for j in reversed(list(range(1, min(i+1,K+1)))):
      soc = min(soc, sumOfCost(i-j)+abs(H[i]-H[i-j]))
  cost[i] = soc
  return soc

print(sumOfCost(N-1))
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
dp = [float('inf')] * N
dp[0] = 0
for i in range(1, N):
    for j in range(1, K + 1):
        if i - j < 0:
            break
        dp[i] = min(dp[i], dp[i - j] + abs(H[i - j] - H[i]))
print((dp[-1]))

# Optimized solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
dp = [10 ** 10] * N
dp[0] = 0
for i in range(1, N):
    for j in range(1, K + 1):
        if i - j < 0:
            break
        dp[i] = min(dp[i], dp[i - j] + abs(H[i - j] - H[i]))
print((dp[-1]))
# Original solution
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [10**100]*n
dp[0] = 0
for i in range(1,n):
    for j in range(1,k+1):
        if i-j >= 0:
            dp[i] = min(dp[i-j]+abs(h[i]-h[i-j]),dp[i])
        else:
            break
print((dp[-1]))

# Optimized solution
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))
inf = 10**12
dp = [inf]*n
dp[0] = 0
for i in range(n):
    for j in range(1,k+1):
        if i+j < n:
            dp[i+j] = min(dp[i+j], dp[i]+abs(h[i]-h[i+j]))
print((dp[-1]))
# Original solution
n, k = [int(x) for x in input().split()]

hs = [int(x) for x in input().split()]

dp = [0] * n
inf = 1 << 30

for i in range(1, min(k,n)):
	smallest = inf
	for j in range(i):
		smallest = min(smallest, dp[j] + abs(hs[i] - hs[j]))
	dp[i] = smallest

if k < n:
	for i in range(k, n):
		smallest = inf
		for j in range(i - k, i):
			smallest = min(smallest, dp[j] + abs(hs[i] - hs[j]))
		dp[i] = smallest

print((dp[n-1]))

# Optimized solution
n, k = [int(x) for x in input().split()]

hs = [int(x) for x in input().split()]

dp = [0] * n

for i in range(1, min(k,n)):
	dp[i] = min([dpj + abs(hs[i] - hj) for dpj, hj in zip(dp[:i], hs[:i])])

if k < n:
	for i in range(k, n):
		dp[i] = min([dpj + abs(hs[i] - hj) for dpj, hj in zip(dp[i-k:i], hs[i-k:i])])

print((dp[-1]))
# Original solution
# 配るDP
# Hは1-index
import sys

input = sys.stdin.readline
n, k = list(map(int, input().split()))
H = [0] + list(map(int, input().split()))
dp = [10 ** 10] * (n + 1)
dp[1] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if i + j <= n:
            dp[i + j] = min(dp[i + j], dp[i] + abs(H[i + j] - H[i]))
print((dp[n]))


# Optimized solution
# 配るDP
# Hは1-index
# PyPyじゃないと厳しい。
# 二重for loop
import sys

input = sys.stdin.readline
n, k = list(map(int, input().split()))
H = [0] + list(map(int, input().split()))
dp = [10 ** 10] * (n + 1)
dp[1] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if i + j <= n:
            dp[i + j] = min(dp[i + j], dp[i] + abs(H[i + j] - H[i]))
print((dp[n]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
DP = [0]*N
DP[1] = abs(h[1]-h[0])

for i in range(2, N):
    DP[i] = min(abs(h[i]-h[j]) + DP[j] for j in range(max(0, i-K), i))
print((DP[-1]))

# Optimized solution
def main():

    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    DP = [0]*N
    DP[1] = abs(h[1]-h[0])

    for i in range(2, N):
        DP[i] = min(abs(h[i]-h[j]) + DP[j] for j in range(max(0, i-K), i))
    print((DP[-1]))
    
main()