CodeEff/ECCO huggingface dataset:
# Original solution
# 貰うDP
N,K=list(map(int,input().split()))
h=[int(_) for _ in input().split()]

INF=10**9
dp=[INF]*(N)
dp[0]=0

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

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

INF=10**9
dp=[0]*(N)

for n in range(1,N):
    dp[n]=min([dp[k] + abs(h[n] - h[k]) for k in range(max(0,n-K), n)])
print((dp[N-1]))
# Original solution
N,K = list(map(int,input().split()))
h = list(map(int,input().split()))
INF = 10**10
 
dp = [INF]*N
dp[0] = 0
 
for i,hi in enumerate(h):
    if i == 0:
        continue
    s = i-K if i-K >= 0 else 0
    dp[i] = min([dpk + abs(hi-hk) for dpk,hk in zip(dp[s:i],h[s:i])])
 
print((dp[-1]))

# Optimized solution
import sys
read = lambda: sys.stdin.readline().rstrip()
 
def main():
    INF = 10 ** 20
 
    n,k = list(map(int,input().split()))
    h = tuple(map(int,read().split()))
    dp = [INF]*(n+1)
    dp[0] = 0
 
    for i in range(1, n):
        for j in range(1, k + 1):
            if i - j < 0:
                pass
            else:
                dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
 
    print((dp[n-1]))
if __name__ == "__main__":
    main()
# Original solution
N, K = list(map(int, input().split(" ")))
H = list(map(int, input().split(" ")))

l = [0]*(N+10)
for i in range(1, N):
    l[i] = min(l[j]+abs(H[i]-H[j]) for j in range(max(0, i-K), i))

print((l[N-1]))

# Optimized solution
N, K = list(map(int, input().split(" ")))
H = list(map(int, input().split(" ")))

costs = [0]*(N+10)
for i in range(1, N):
    j = max(0, i-K)
    costs[i] = min(cost+abs(H[i]-h) for cost, h in zip(costs[j:i], H[j:i]))

print((costs[N-1]))
# Original solution
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)))

# Optimized solution
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]))
# Original solution
import sys
sys.setrecursionlimit(10**6)

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

def solve(idx):
    if dp[idx] is not None:
        return dp[idx]

    if idx == N - 1:
        dp[idx] = 0
    elif idx == N - 2:
        dp[idx] = abs(h[idx] - h[idx + 1])
    else:
        temp = 10**10
        for k in range(1, K + 1):
            if idx + k == N:
                break
            temp = min(temp, solve(idx + k) + abs(h[idx] - h[idx + k]))
        dp[idx] = temp
    return dp[idx]

print((solve(0)))


# Optimized solution
import sys
sys.setrecursionlimit(10**6)

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

def solve(idx):
    if dp[idx] is not None:
        return dp[idx]

    if idx == N - 1:
        dp[idx] = 0
    elif idx == N - 2:
        dp[idx] = abs(h[idx] - h[idx + 1])
    else:
        temp = 10**10
        for k in range(1, K + 1):
            if idx + k == N:
                break
            if dp[idx + k] is None:
                temp = min(temp, solve(idx + k) + abs(h[idx] - h[idx + k]))
            else:
                temp = min(temp, dp[idx + k] + abs(h[idx] - h[idx + k]))
        dp[idx] = temp
    return dp[idx]

print((solve(0)))
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [0]
INF = float('inf')
for i in range(1, n):
  act = INF
  for j in range(k):
    if i-j > 0:
      act = min(act, dp[-1-j]+abs(h[i]-h[i-j-1]))
    if act == 0:
      break
  dp.append(act)
print((dp[-1]))

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

dp = [0]
INF = float('inf')
for i in range(1, n):
  act = INF
  for j in range(k):
    if i-j > 0:
      act = min(act, dp[-1-j]+abs(h[i]-h[i-j-1]))
  dp.append(act)
print((dp[-1]))
# Original solution
h,k=list(map(int,input().split()))
def bupdp(l,h,k):
    dp=[0]*(h+1)
    dp[0] = 0
    for i in range(0,h):
        if i>k:
            mini = 999999999
            for j in range(1,k+1):
                mini = min(mini,dp[i-j]+abs(l[i]-l[i-j]))                
            dp[i] = mini

        if i<=k:
            minim = 999999999
            for j in range(1,i+1):
                minim = min(minim,dp[i-j]+abs(l[i]-l[i-j])) 
            dp[i] = minim
        if i<1:
            dp[i] = 0
    return dp[-2]

l = list(map(int,input().split()))
print((bupdp(l,h,k)))

# Optimized solution
h,k=list(map(int,input().split()))
def bupdp(l,h,k):
    dp=[0]*(h+1)
    dp[0] = 0
    for i in range(0,h):
        if i>k:
            mini = min([dp[i-j]+abs(l[i]-l[i-j]) for j in range(1,k+1)])             
            dp[i] = mini
        if i<=k and i!=0:
            minim = min([dp[i-j]+abs(l[i]-l[i-j]) for j in range(1,i+1)])
            dp[i] = minim
        if i<1:
            dp[i] = 0
    return dp[-2]

l = list(map(int,input().split()))
print((bupdp(l,h,k)))
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10 ** 18

h = H + [0] * K
dp = [INF] * (N + K)
dp[0] = 0

for n in range(N):
	for k in range(1, K + 1):
		l, r = n, n + k
		dp[r] = min(dp[r], dp[l] + abs(h[l] - h[r]))
print((dp[N - 1]))


# Optimized solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10 ** 18

h = H
dp = [0] * N

for n in range(1, N):
	l, r = max(0, n - K), n
	dp[n] = min([dpl + abs(hl - h[r]) for dpl, hl in zip(dp[l:r], h[l:r])])
	#print(dp)
print((dp[N - 1]))
# Original solution
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
dp = [float('inf')] * n
dp[0] = 0

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

print((dp[n - 1]))


# Optimized solution
def main():
    n, k = [int(i) for i in input().split()]
    h = [int(i) for i in input().split()]
    dp = [float('inf')] * n
    dp[0] = 0

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

main()
# Original solution
n,k=[int(i) for i in input().split()]
h=[int(i) for i in input().split()]

#import numpy as np

#a=np.array(h)
b=[[0 for i in range(n)] for j in range(n)]
#b=np.array(b)

for j in range(n):
    for i in range(n):
        if j>i:
            b[j][i]=abs(h[j]-h[i])
#print(b)

dp=[int(0) for i in range(n)]
dp0=[[10000000000 for i in range(n)] for j in range(n)]

dp0[0]=0
dp[0]=0

dp0[1]=b[1][0]
dp[1]=dp0[1]

if k==1:
    for i in range(2,n):
        dp[i]=dp[i-1]+b[i][i-1]
    
if k > 1:
    for j in range(2,n):
        if j <=k:
            for i in range(j):
                dp0[j][i]=dp[i]+b[j][i]
            dp[j]=min(dp0[j])
        else:
            for i in range(k):
                dp0[j][i+j-k]=dp[i+j-k]+b[j][i+j-k]
            dp[j]=min(dp0[j])

#print(dp0)
print((dp[n-1]))

# Optimized solution
n,k=[int(i) for i in input().split()]
h=[int(i) for i in input().split()]
#print(n,k,h)
dp=[float('inf') for i in range(n)]
dp[0]=0
dp[1]=abs(h[1]-h[0])

if k > 1 and n > 2:
  for y in range(2,k):
    dp[y]=abs(h[y]-h[0])
    x=1
    while x < k:
      tmp=dp[x]+abs(h[y]-h[x])
      if tmp < dp[y]:
        dp[y]=tmp
      x+=1
  for y in range(k,n):    
    dp[y]=dp[y-1]+abs(h[y]-h[y-1])
    for x in range(2,k+1):
      tmp=dp[y-x]+abs(h[y]-h[y-x])
      if tmp < dp[y]:
        dp[y]=tmp
        
if k == 1 and n > 2:
  for y in range(2,n):
    dp[y]=dp[y-1]+abs(h[y]-h[y-1])

print((dp[-1]))
# Original solution
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(eval(input())) #整数読み込み
def ifn(): return float(eval(input())) #浮動小数点読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return list(map(int, input().split())) #整数map取得
def fmn(): return list(map(float, input().split())) #浮動小数点map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, input().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def add_mod(a,b): return (a+b) % MOD

N,K = imn()
h = iln()

dp = [10**10 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]-h[i+j]))
print((dp[N-1]))


# Optimized solution
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(eval(input())) #整数読み込み
def ifn(): return float(eval(input())) #浮動小数点読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return list(map(int, input().split())) #整数map取得
def fmn(): return list(map(float, input().split())) #浮動小数点map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, input().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD

N,K = imn()
h = iln()

dp = [10**10 for _ in range(N)]

dp[0] = 0

for i in range(N):
    for k in range(1,K+1):
        if i+k < N: dp[i+k] = min(dp[i+k], dp[i]+abs(h[i+k]-h[i]))
print((dp[N-1]))
# Original solution
n, K = list(map(int, input().split()))
h = list(map(int, input().split()))

INF = 1 << 40
dp = [INF] * n
dp[0] = 0
dp[1] = abs(h[1] - h[0])

for i in range(1, n - 1):
	minv = 1 << 40
	for k in range(1, K + 1):
		if i + 1 - k < 0:
			continue
		minv = min(minv, dp[i + 1 - k] + abs(h[i + 1] - h[i + 1 - k]))
	dp[i + 1] = minv

print((dp[-1]))

# Optimized solution
n, k = list(map(int, input().split()))
h = [0] + list(map(int, input().split()))

INF = 1 << 30
dp = [INF] * (n + 1)
dp[0] = 0
dp[1] = 0

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


print((dp[n]))
# Original solution
def LI(): return list(map(int, input().split()))
inf = float('inf')

n, k = LI()
h = LI()

# init
dp = [inf] * k + [0] * n
d = [[inf] * j + [abs(h[i+j] - h[i]) for i in range(n-j)] for j in range(1, k+1)]

# calc
for i in range(1, n):
    d_i = [dp[i-j+k] + d[j-1][i] for j in range(1, k+1)]
    dp[i+k] = min(d_i)

print((dp[-1]))


"""
"""


# Optimized solution
def LI(): return list(map(int, input().split()))
inf = float('inf')

n, k = LI()
h = LI()

# init
dp = [inf] * k + [0] * n

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

print((dp[-1]))


"""
"""
# Original solution
def main():
    n,k=list(map(int,input().split()))
    h=list(map(int,input().split()))
    dp=[10**5]*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]))
    
main()


# 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):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-k),i))
print((dp[n-1]))
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()
    r = [0] * n
    for i in range(1,n):
        ai = a[i]
        t = r[i-1] + abs(a[i-1]-ai)
        j = i - 2
        mj = i - k
        if mj < 0:
            mj = 0
        while j >= mj:
            u = r[j] + abs(a[j]-a[i])
            if t > u:
                t = u
            j -= 1

        r[i] = t

    return r[-1]


print(main())


# Optimized solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()
    r = [0] * n
    for i in range(1,n):
        ai = a[i]
        t = r[i-1] + abs(a[i-1]-ai)
        mj = i - k
        if mj < 0:
            mj = 0
        for j in range(mj,i-1):
            u = r[j] + abs(a[j]-ai)
            if t > u:
                t = u

        r[i] = t

    return r[-1]


print(main())
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [0] * N
for i in range(N):
  if i > 0:
    dp[i] = min([dp[i - j] + abs(h[i] - h[i - j]) if i - j >= 0 else 10**9 for j in range(1, K + 1)])
print((dp[N - 1]))


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

dp = [0] * N
for i, hi in enumerate(h):
  if i > 0:
    x = max(0, i - K)
    dp[i] = min([dpj + abs(hi - hj) for hj, dpj in zip(h[x:i], dp[x:i])])
print((dp[N - 1]))
# Original solution
#!/usr/bin/env python3

n, k = [int(i) for i in input().split(' ')]
k += 1

harr = [int(i) for i in input().split(' ')]

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


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

print((dp[n-1]))


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

n, k = [int(i) for i in input().split(' ')]

arr = [int(i) for i in input().split(' ')]

dp = [10**10]*n

k += 1
dp[0] = 0

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

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


def solve():
    N, K, *H = list(map(int, sys.stdin.read().split()))
    dp = [10**10] * N
    dp[0] = 0
    for i, h in enumerate(H):
        dp_i = dp[i]
        for k, dp_i_k in enumerate(dp[i+1:i+K+1], start=1):
            if i+k > N-1:
                break
            tmp_dp = dp_i + abs(h - H[i+k])
            if dp_i_k > tmp_dp:
                dp[i+k] = tmp_dp
    return dp[-1]


if __name__ == "__main__":
    answer = solve()
    print(answer)


# Optimized solution
import sys


def solve():
    N, K, *H = list(map(int, sys.stdin.read().split()))
    dp = [0] * N
    for i in range(1, N):
        s = i-K if i-K > 0 else 0
        dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(s, i))
    return dp[-1]


if __name__ == "__main__":
    answer = solve()
    print(answer)
# Original solution
import sys


def solve():
    N, K, *H = list(map(int, sys.stdin.read().split()))
    dp = [0] * N
    for i in range(1, N):
        s = i-K if i-K > 0 else 0
        dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(s, i))
    return dp[-1]


if __name__ == "__main__":
    answer = solve()
    print(answer)


# Optimized solution
import sys


def solve():
    N, K, *H = list(map(int, sys.stdin.read().split()))
    dp = [0] * N
    for i in range(1, N):
        h_i = H[i]
        s = i-K if i-K > 0 else 0
        dp[i] = min(d_j + abs(h_i - h_j) for d_j, h_j in zip(dp[s:i], H[s:i]))
    return dp[-1]


if __name__ == "__main__":
    answer = solve()
    print(answer)
# Original solution
def main():
  import sys
  input = sys.stdin.readline
  n,k=list(map(int,input().split()))
  h=tuple(map(int,input().split()))
  dp=[0]*(n+1)
  for i in range(2,n+1):
    a=min(i-1,k)+1
    dp[i]+=min([dp[i-j]+abs(h[i-1]-h[i-j-1]) for j in range(1,a)])
  print((dp[-1]))
if __name__=='__main__':
  main()

# Optimized solution
def main():
  import sys
  input = sys.stdin.readline
  n,k=list(map(int,input().split()))
  h=tuple(map(int,input().split()))
  dp=[0]*(n+1)
  for i in range(2,n+1):
    a=min(i-1,k)+1
    m=float('inf')
    for j in range(1,a):
      x=dp[i-j]+abs(h[i-1]-h[i-j-1])
      m=x if m>=x else m
    dp[i]+=m
  print((dp[-1]))
if __name__=='__main__':
  main()
# Original solution
# given an array of Heights, jump from 1 to N with at most K sized jump to N
# each jump incurs abs(H[j] - H[i]) cost
# minimize cost of jumps from 1 to N
#
import collections

def frog(H, k):
    if k > len(H):
        k = len(H)
    K = collections.deque([0] * (k + 1))
    n = len(H)
    klen = len(K)
    for i in range(n):
        cost = 0
        if i > 0:
            maxjump = min(i, k)
            cost = float("inf")
            for j in range(1, maxjump + 1):
                jcost = abs(H[i] - H[i - j]) + K[klen - j]
                if jcost < cost:
                    cost = jcost
        K.popleft()
        K.append(cost)
    return cost

assert frog([40, 10, 20, 70, 80, 10, 20, 70, 80, 60], 4) == 40
assert frog([10, 10], 100) == 0
assert frog([10, 20, 10], 1) == 20

if __name__ == "__main__":
    import sys
    s = sys.stdin.read()
    N, k, *H = list(map(int, s.split()))
    ans = frog(H, k)
    print(ans)


# Optimized solution
# given an array of Heights, jump from 1 to N with at most K sized jump to N
# each jump incurs abs(H[j] - H[i]) cost
# minimize cost of jumps from 1 to N
#
import collections

def frog(H, k):
    if k > len(H):
        k = len(H)
    K = collections.deque([0] * (k + 1))
    n = len(H)
    klen = len(K)
    inf = float("inf")
    for i in range(n):
        cost = 0
        if i > 0:
            maxjump = i if i < k else k # min(i, k)
            cost = inf
            hi = H[i]
            for j in range(1, maxjump + 1):
                jcost = abs(hi - H[i - j]) + K[klen - j]
                if jcost < cost:
                    cost = jcost
        K.popleft()
        K.append(cost)
    return cost

assert frog([40, 10, 20, 70, 80, 10, 20, 70, 80, 60], 4) == 40
assert frog([10, 10], 100) == 0
assert frog([10, 20, 10], 1) == 20

if __name__ == "__main__":
    import sys
    s = sys.stdin.read()
    N, k, *H = list(map(int, s.split()))
    ans = frog(H, k)
    print(ans)
# Original solution
# given an array of Heights, jump from 1 to N with at most K sized jump to N
# each jump incurs abs(H[j] - H[i]) cost
# minimize cost of jumps from 1 to N
#
import collections

def frog(H, k):
    if k > len(H):
        k = len(H)
    K = collections.deque([0] * (k + 1))
    n = len(H)
    klen = len(K)
    inf = float("inf")
    for i in range(n):
        cost = 0
        if i > 0:
            maxjump = i if i < k else k # min(i, k)
            cost = inf
            hi = H[i]
            for j in range(1, maxjump + 1):
                jcost = abs(hi - H[i - j]) + K[klen - j]
                if jcost < cost:
                    cost = jcost
        K.popleft()
        K.append(cost)
    return cost

assert frog([40, 10, 20, 70, 80, 10, 20, 70, 80, 60], 4) == 40
assert frog([10, 10], 100) == 0
assert frog([10, 20, 10], 1) == 20

if __name__ == "__main__":
    import sys
    s = sys.stdin.read()
    N, k, *H = list(map(int, s.split()))
    ans = frog(H, k)
    print(ans)


# Optimized solution
# given an array of Heights, jump from 1 to N with at most K sized jump to N
# each jump incurs abs(H[j] - H[i]) cost
# minimize cost of jumps from 1 to N
#
def frog(H, k):
    if k > len(H):
        k = len(H)
    n = len(H)
    K = [0] * n
    inf = float("inf")
    for i in range(n):
        cost = 0
        if i > 0:
            maxjump = i if i < k else k # min(i, k)
            cost = inf
            hi = H[i]
            # jump source going backwards from i-1 (smallest 1 sized jump) to i - maxjumps
            for j in range(i - 1, i - maxjump - 1, -1):
                jcost = abs(hi - H[j]) + K[j]
                if jcost < cost:
                    cost = jcost
        K[i] = cost
    return cost

assert frog([40, 10, 20, 70, 80, 10, 20, 70, 80, 60], 4) == 40
assert frog([10, 10], 100) == 0
assert frog([10, 20, 10], 1) == 20

if __name__ == "__main__":
    import sys
    s = sys.stdin.read()
    N, k, *H = list(map(int, s.split()))
    ans = frog(H, k)
    print(ans)
# Original solution
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: list(map(int, sys.stdin.readline().split()))
nms = lambda: list(map(str, sys.stdin.readline().split()))
nl = lambda: list(nm())

n, k = nm()
h = nl()
h.insert(0, h[0])
INF = 10 ** 9
dp = [INF for _ in range(n + 1)]
dp[1] = 0

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


# Optimized solution
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: list(map(int, sys.stdin.readline().split()))
nms = lambda: list(map(str, sys.stdin.readline().split()))
nl = lambda: list(nm())

n, k = nm()
h = nl()
h.insert(0, h[0])
INF = 10 ** 9
dp = [INF for _ in range(n + 1)]
dp[1] = 0

for i in range(2, n + 1):
    x = max(i - k, 1)
    dp[i] = min([d + abs(h[i] - hd) for d, hd in zip(dp[x:i], h[x:i])])
print((dp[-1]))
# Original solution
n,k,*h=list(map(int,open(0).read().split()));
dp=[0];
for i in range(1,n):dp+=[min(10**9*(i-j>k)+dp[j]+abs(h[j]-h[i])for j in range(i))];
print((dp[-1]))

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