CodeEff/ECCO huggingface dataset:
# Original solution
# coding:utf-8

import sys


input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


def inpl(): return list(map(int, input().split()))


n, k = inpl()
H = inpl()

if k >= n: k = n - 1
H = [H[0]] + H
dp = [INF] * (n + 1)
dp[0] = 0

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

print((dp[n]))


# Optimized solution
# coding:utf-8

import sys


input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


def inpl(): return list(map(int, input().split()))


n, k = inpl()
H = inpl()

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], abs(H[i + j] - H[i]) + dp[i])

print((dp[n - 1]))
# Original solution
def solve():
    h, k = read()
    result = think(h, k)
    write(result)


def read():
    n, k = read_int(2)
    return read_int(n), k


def read_int(n):
    return list([int(x) for x in read_line().split(' ')])[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(h, k):
    cache = preprocess(h, k)
    cost = [0 for x in range(len(h))]
    # element of h <= 10 ** 4, elements <= 10 ** 5, +1 in case
    initial_value_of_cost = 10 ** (4 + 5) + 1
    for dst in range(1, len(h)):
        minimum_cost = initial_value_of_cost
        for src in range(max(0, dst - k), dst):
            minimum_cost = min(minimum_cost, cost[src] + cache[src][dst])
        cost[dst] = minimum_cost
    return cost[len(h) - 1]


def preprocess(h, k):
    cache = [[0 for x in range(len(h))] for y in range(len(h))]
    for src in range(len(h) - 1):
        for dst in range(src + 1, min(len(h), src + k + 1)):
            cost = abs(h[src] - h[dst])
            cache[src][dst] = cost
    return cache


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

# Optimized solution
def solve():
    h, k = read()
    result = think(h, k)
    write(result)


def read():
    n, k = read_int(2)
    return read_int(n), k


def read_int(n):
    return list([int(x) for x in read_line().split(' ')])[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(h, k):
    cost = [0 for x in range(len(h))]
    # element of h <= 10 ** 4, elements <= 10 ** 5, +1 in case
    initial_value_of_cost = 10 ** (4 + 5) + 1
    for dst in range(1, len(h)):
        minimum_cost = initial_value_of_cost
        for src in range(max(0, dst - k), dst):
            minimum_cost = min(minimum_cost, cost[src] + abs(h[src] - h[dst]))
        cost[dst] = minimum_cost
    return cost[len(h) - 1]


def write(result):
    print(result)


if __name__ == '__main__':
    solve()
# Original solution
def solve():
    h, k = read()
    result = think(h, k)
    write(result)


def read():
    n, k = read_int(2)
    return read_int(n), k


def read_int(n):
    return list([int(x) for x in read_line().split(' ')])[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(h, k):
    cost = [0 for x in range(len(h))]
    # element of h <= 10 ** 4, elements <= 10 ** 5, +1 in case
    initial_value_of_cost = 10 ** (4 + 5) + 1
    n = len(h)
    for dst in range(1, n):
        minimum_cost = initial_value_of_cost
        start_point = max(0, dst - k)
        for src in range(start_point, dst):
            minimum_cost = min(minimum_cost, cost[src] + abs(h[src] - h[dst]))
        cost[dst] = minimum_cost
    return cost[n - 1]


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

# Optimized solution
def solve():
    h, k = read()
    result = think(h, k)
    write(result)


def read():
    n, k = read_int(2)
    return read_int(n), k


def read_int(n):
    return list([int(x) for x in read_line().split(' ')])[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(h, k):
    cost = [0 for x in range(len(h))]
    for dst in range(1, len(h)):
        start_point = max(0, dst - k)
        cost[dst] = min([cost[src] + abs(h[src] - h[dst]) for src in range(start_point, dst)])
    return cost[len(h) - 1]


def write(result):
    print(result)


if __name__ == '__main__':
    solve()
# Original solution
n, k = list(map(int, input().split()))
*h, = list(map(int, input().split()))

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

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

print((dp[n-1]))


# Optimized 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):
    if i == 1:
        dp[1] = dp[0] + abs(h[0]-h[1])
        continue

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

print((dp[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):
  if i < k:
    dp[i] = min( { dp[j]+abs(h[j]-h[i]) for j in range(i) } )
  else:
    dp[i] = min( { dp[i-j-1]+abs(h[i-j-1]-h[i]) for j in range(k) } )
print((dp[n-1]))

# Optimized 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):
  if i < k:
    dp[i] = min([ dp[j]+abs(h[j]-h[i]) for j in range(i) ])
  else:
    dp[i] = min( [ dp[i-j-1]+abs(h[i-j-1]-h[i]) for j in range(k) ] )
print((dp[n-1]))
# Original solution
import sys

def main():
  n, k, *h = list(map(int, sys.stdin.read().split()))
  dp = [float("inf")] * n
  dp[0] = 0
  for i in range(1, n):
    if i < k:
      dp[i] = min([ dp[j]+abs(h[j]-h[i]) for j in range(i) ])
    else:
      dp[i] = min( [ dp[i-j-1]+abs(h[i-j-1]-h[i]) for j in range(k) ] )
  print((dp[n-1]))
  
if __name__ == "__main__":
  main()

# Optimized solution
import sys

def main():
  n, k, *h = list(map(int, sys.stdin.read().split()))
  dp = [float("inf")] * n
  dp[0] = 0
  for i in range(1, n):
    if i < k:
      dp[i] = min([ dpj+abs(hj-h[i]) for dpj, hj in zip(dp[:i], h[:i]) ])
    else:
      dp[i] = min( [ dpj+abs(hj-h[i]) for dpj, hj in zip(dp[i-k:i], h[i-k:i]) ] )
  print((dp[n-1]))
  
if __name__ == "__main__":
  main()
# Original solution
#!/usr/bin/env python3

#import
#import math
#import numpy as np
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

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

for i in range(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 + j] - H[i]))
        

print((dp[-1]))



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

#import
#import math
#import numpy as np
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

dp = [10 ** 9] * 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] - H[i - j]))
        

print((dp[-1]))
# Original solution
N,K = list(map(int,input().split()))
H = list(map(int,input().split()))

INF = float('inf')
dp = [INF] * N
dp[0] = 0
for i in range(1,N):
    for j in range(K):
        if i-j-1 < 0: break
        dp[i] = min(dp[i], dp[i-j-1] + abs(H[i] - H[i-j-1]))
print((dp[-1]))

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

    print((dp[N-1]))



if __name__ == "__main__":
    main()

# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    H = list(map(int, input().split()))
    dp = [float('inf')] * (N + 5)
    dp[0] = 0
    for i in range(1,N):
        for k in range(1,K+1):
            if i - k >= 0:
                dp[i] = min(dp[i], dp[i-k] + abs(H[i] - H[i-k]))

    print((dp[N-1]))

if __name__ == "__main__":
    main()
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = float("inf")
dp = [inf] * N
dp[0] = 0


def chmin(table: list, num: int, sbn: int):
    table[num + sbn] = min((table[num + sbn], table[num] + abs(h[i + sbn] - h[num])))


for i in range(N):
    for sbn in range(K):
        try:
            chmin(dp, i, sbn + 1)
        except IndexError:
            break
print((dp[-1]))


# Optimized solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = 10**9
dp = [inf] * N
dp[0] = 0


def chmin(num: int, sbn: int):
    dp[num + sbn] = min((dp[num + sbn], dp[num] + abs(h[i + sbn] - h[num])))


for i in range(N):
    for sbn in range(K):
        try:
            chmin(i, sbn + 1)
        except IndexError:
            break
print((dp[-1]))
# Original solution
N, K = list(map(int, input().split()))
hlist = list(map(int, input().split()))
inf = float("inf")
dp = [inf] * N


def abs_sbn(a, b):
    return abs(hlist[b] - hlist[a])


dp[0] = 0
dp[1] = abs_sbn(0, 1)
for i in range(2, N):
    for j in range(1, K + 1):
        if (i - j) >= 0:
            dp[i] = min(dp[i], dp[i - j] + abs_sbn(i, i - j))

print((dp[-1]))


# Optimized solution
N, K = list(map(int, input().split()))
hlist = list(map(int, input().split())) + [0] * (K + 2)
inf = float("inf")
dp = [inf] * (N * K + 2)


def abs_sbn(a, b):
    return abs(hlist[b] - hlist[a])


dp[0] = 0
for i in range(N):
    for j in range(1, K + 1):
        dp[i + j] = min(dp[i + j], dp[i] + abs_sbn(i, i + j))

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 in range(N):
    for k in range(1,K+1):
        if i-k>=0:
            dp[i] = min(dp[i],dp[i-k]+abs(h[i]-h[i-k]))

print((dp[-1]))

# Optimized 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]))
# Original solution
# -*- coding: utf-8 -*-
"""
B - Frog 2
https://atcoder.jp/contests/dp/tasks/dp_b

"""
import sys
from sys import stdin


def solve(ashiba, N, K):
    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, len(ashiba)):
        for j in range(max(0, i-K), i):
            dp[i] = min(dp[i], dp[j]+abs(ashiba[j]-ashiba[i]))
    return dp[-1]


def main(args):
    N, K = list(map(int, input().split()))
    ashiba = [int(i) for i in input().split()]
    ans = solve(ashiba, N, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])


# Optimized solution
# -*- coding: utf-8 -*-
"""
B - Frog 2
https://atcoder.jp/contests/dp/tasks/dp_b

"""
import sys
from sys import stdin


def solve(ashiba, N, K):
    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, len(ashiba)):
        paths = [dp[j]+abs(ashiba[j]-ashiba[i]) for j in range(max(0, i-K), i)]
        dp[i] = min(paths)
    return dp[-1]


def main(args):
    N, K = list(map(int, input().split()))
    ashiba = [int(i) for i in input().split()]
    ans = solve(ashiba, N, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])
# Original solution
# -*- coding: utf-8 -*-
"""
B - Frog 2
https://atcoder.jp/contests/dp/tasks/dp_b
AC
"""
import sys
from sys import stdin


def solve(ashiba, N, K):
    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, len(ashiba)):
        paths = [dp[j]+abs(ashiba[j]-ashiba[i]) for j in range(max(0, i-K), i)]
        dp[i] = min(paths)
    return dp[-1]


def main(args):
    N, K = list(map(int, input().split()))
    ashiba = [int(i) for i in input().split()]
    ans = solve(ashiba, N, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])


# Optimized solution
# -*- coding: utf-8 -*-
"""


"""


def chmin(i, j):
    a, b = dp[i+j], dp[i]+abs(h[i]-h[i+j])
    if a > b:
        dp[i+j] = b


n, k = list(map(int, (input().split())))
h = list(input().split())
h = [int(i) for i in h]
dp = [10**10]*n
dp[0] = 0
for i in range(0, n):
    for j in range(1, k+1):
        if i+j < n:
            chmin(i, j)
print((dp[n-1]))
# Original solution
import sys

n,k = list(map(int, input().split()))
l = list(map(int, input().split()))
dp = [sys.maxsize]*n
dp[0]=0
# dp[1] = abs(l[1]-l[0])
for i in range(1,n):
    for j in range(1,k+1):
        if i-j>=0:
            dp[i]=min(dp[i],dp[i-j]+abs(l[i]-l[i-j]))
print((dp[n-1]))

# Optimized solution
import sys

n,k = list(map(int, input().split()))
l = list(map(int, input().split()))
dp = [sys.maxsize]*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],dp[i-j]+abs(l[i]-l[i-j]))   
print((dp[n-1]))
# Original solution
import sys
sys.setrecursionlimit(10 ** 7)

def solve(index: int, dp: list, footing: list, K: int) -> int:
    if dp[index] != -1:
        return dp[index]

    if index == 0:
        return 0
    elif index == 1:
        return abs(footing[1]-footing[0])

    tmp = 10 ** 10
    for k in range(1, min(K+1,index+1)):
        tmp = min(tmp, solve(index-k, dp, footing, K)+abs(footing[index]-footing[index-k]))

    #print(f'{tmp=}')
    dp[index] = tmp
    return tmp

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

dp = [-1] * N

ans = solve(N-1, dp, footing, K)

print(ans)


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


dp = [10 ** 10] * N
dp = dp + [10 ** 10] * 1000
dp[0] = 0

footing = footing + [0] * 1000
# give
'''
for i in range(N-1):
    for k in range(1, K+1):
        if dp[i+k] > dp[i] + abs(footing[i+k] - footing[i]):
            dp[i+k] = dp[i] + abs(footing[i+k] - footing[i])

print(dp[N-1])
'''
# get
for i in range(1,N):
    for j in range(1,min(K+1, i+1)): 
        dp[i] = min(dp[i],dp[i-j] + abs(footing[i]-footing[i-j])) 

print((dp[N-1]))
# Original solution
N,K = list(map(int,input().split()))
H = list(map(int,input().split()))
import sys
import functools
#indexi番目にいるときの最小コスト
sys.setrecursionlimit(pow(10,6))


@functools.lru_cache(None)
def func(i):
    if i==0:
        return 0  

    h = H[i] 
    return min(func(i-k)+abs(h-H[i-k]) for k in range(1,min(i,K)+1))
        


print((func(N-1)))

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

dp = [0]*N
dp[1]= abs(H[1]-H[0])
#dp[i] インデックスiにいるときの最小値
inf = float("inf")

for i in range(1,N-1):
    tmp = inf
    oppai = min(K,i+1)
    for jump in range(1,oppai+1):
        tmp = min(tmp,dp[i+1-jump]+abs(H[i+1]-H[i+1-jump]))
    dp[i+1]=tmp
print((dp[N-1]))
# Original solution
import sys
from functools import reduce
sys.setrecursionlimit(2000000001)

def solve(n,k,ar,dp,x):
	ab = abs; mn = min
	if x>=n-2: return dp[x]
	if not dp[x] is -1: return dp[x]
	t = [ab(ar[x]-ar[i])+solve(n,k,ar,dp,i) for i in range(x+1,mn(n,x+k+1))]
	dp[x] = mn(t)	#reduce(lambda a,b : a if a < b else b,t)
	return dp[x]

def main():
	I = lambda:list(map(int,sys.stdin.readline().split()))
	pr = lambda x:sys.stdout.write(f'{x}\n')
	n,k, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	pr(solve(n,k,ar,dp,0))

main()

# Optimized solution
import sys
sys.setrecursionlimit(2000000001)

def solve(n,k,ar,dp,x):
	ab = abs; mn = min
	if x>=n-2: return dp[x]
	if not dp[x] is -1: return dp[x]
	dp[x] = mn([ab(ar[x]-ar[i])+solve(n,k,ar,dp,i) for i in range(x+1,mn(n,x+k+1))])
	return dp[x]

def main():
	I = lambda:list(map(int,sys.stdin.readline().split()))
	pr = lambda x:sys.stdout.write(f'{x}\n')
	n,k, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	pr(solve(n,k,ar,dp,0))

main()
# Original solution
#python3

INF = int(1e9)

def main():

    n, k = list(map(int, input().split())) 
    h = [int(i) for i in input().split()] + [0]* 110


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

    print((dp[n-1]))

main() 

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    n, k = list(map(int, readline().split()))
    h = [int(i) for i in readline().split()]
    #dp[i]:=i番目の柱までの経路によって加算されたコストの総和の最小値
    INF = int(1e9)
    dp = [INF for _ in range(n)]
    def cost(i, j):
        return abs(h[i]-h[j])

    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]+cost(i, i+j)) 
    print((dp[n-1]))
if __name__ == '__main__':
    main()
# Original solution
def solve():
  n,k=[int(i) for i in input().split()]
  h=[int(i) for i in input().split()]
  dp=[0]*n
  for i, hi in enumerate(h):
    if i==0: continue
    dp[i]=min([dp[j] + abs(hi-h[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print((solve()))


# Optimized solution
def solve():
  n,k=[int(i) for i in input().split()]
  h=[int(i) for i in input().split()]
  dp=[0]*n
  for i, hi in enumerate(h):
    if i==0: continue
    x = max(0, i - k)
    dp[i]=min([dpj + abs(hi-hj) for dpj, hj in zip(dp[x:i], h[x:i])])
    #スライサー[x:i]で[x]～[i-1]の部分リストを取得
  return dp[n-1]
print((solve()))
# Original solution
N,K=list(map(int,input().split()))
dp=[1000000000000000]*N
h=[int(x) for x in input().split()]
dp[0]=0
for i in range(N):
    dp=dp[:i] + ([x if x<dp[i]+abs(h[i+j]-h[i]) else dp[i]+abs(h[i+j]-h[i]) for j,x in enumerate(dp[i:(i+K+1 if i+K+1<N else N)])]) + (dp[i+K+1:] if i+K+1<N else [])
#    for j in range(i,i+K+1 if i+K+1 < N else N):
#        dp[j] = dp[j] if dp[j]<dp[i]+abs(h[j]-h[i]) else dp[i]+abs(h[j]-h[i])
print((dp[-1]))

# Optimized solution
N,K=list(map(int,input().split()))
dp=[1000000000000000 for i in range(N)]
h=[int(x) for x in input().split()]
dp[0]=0
for i in range(N):
    for j in range(i,i+K+1 if i+K+1 < N else N):
        dp[j] = dp[j] if dp[j]<dp[i]+abs(h[j]-h[i]) else dp[i]+abs(h[j]-h[i])
print((dp[-1]))
# Original solution
n,h=list(map(int,input().split()))
l=list(map(int,input().split()))
c=[10**9]*n
c[0]=0
c[1]=abs(l[0]-l[1])
for i in range(2,n):
    c[i]=min( c[j]+abs(l[i]-l[j]) for j in range(max(0,i-h),i) )
print((c[-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
n,h=list(map(int,input().split()))
l=list(map(int,input().split()))
c=[10**9]*n
c[0]=0
c[1]=abs(l[0]-l[1])
for i in range(2,n):
    for j in range(max(0,i-h),i):
        c[i]=min(c[j]+abs(l[i]-l[j]),c[i])
print((c[-1]))

# Optimized solution
n,h=list(map(int,input().split()))
l=list(map(int,input().split()))
c=[10**9]*n
c[0]=0
c[1]=abs(l[0]-l[1])
for i in range(2,n):
    c[i]=min( c[j]+abs(l[i]-l[j]) for j in range(max(0,i-h),i) )
print((c[-1]))
# Original solution
a,b = list(map(int, input().split()))
b+=1
x = list(map(int, input().split()))
dp = [0]+[10**10]*(a-1)
dp = [0]+[float("inf")]*(a-1)
dp[1] = abs(x[1]-x[0])
for i,h in enumerate(x):
    for j in range(i+1,min(i+b,a)):
        dp[j] = min(dp[j],dp[i]+abs(h-x[j]))
#print(dp)
print((dp[a-1]))

# Optimized solution
a,b = list(map(int, input().split()))
x = list(map(int, input().split()))
#dp = [0]+[10**10]*(a-1)
dp = [0]+[float("inf")]*(a-1)
dp[1] = abs(x[1]-x[0])
for i,h in enumerate(x):
    for j in range(max(0,i-b),i):
        dp[i] = min(dp[i],dp[j]+abs(h-x[j]))
        #print(dp)
#print(dp)
print((dp[a-1]))
# Original solution
n,k = list(map(int,input().split()))
H = list(map(int,input().split()))
inf = float('INF')
dp = [inf]*n
dp[0] = 0
for i in range(1,n):
    for j in range(1,min(i,k)+1):
        # print(j)
        dp[i]=min(dp[i],dp[i-j]+abs(H[i]-H[i-j]))
    # print(dp)

    # dp[i]=min(dp[i],dp[i-1]+abs(H[i]-H[i-1]))
# print(dp)
print((dp[-1]))

# Optimized solution
n,k = list(map(int,input().split()))
H = list(map(int,input().split()))
inf = float('INF')
dp = [inf]*n
dp[0] = 0
for i in range(0,n-1):
    for j in range(1,min(n-i-1,k)+1):
        # print(j)
        dp[i+j]=min(dp[i+j],dp[i]+abs(H[i]-H[i+j]))
    # print(dp)

    # dp[i]=min(dp[i],dp[i-1]+abs(H[i]-H[i-1]))
# print(dp)
print((dp[-1]))
# Original solution
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

n, k = list(map(int, input().split()))
h = [0] + list(map(int, input().split()))+[0]*(k+10)

INF = 10**10
dp = [INF]*(n+k+10)
dp[1] = 0

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


print((dp[n]))



# Optimized solution
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

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

INF = 10**10
dp = [INF]*(n+5)
dp[1] = 0

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

print((dp[n]))
# Original solution
from collections import deque
INF = float('inf')
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))

H = deque(list(map(int, input().split())))
max_K = 100
for _ in range(max_K):
  H.append(INF)

dp = [INF] * (N + max_K)
dp[0] = 0

for n in range(N):
  for offset in range(1, K + 1):
    new_index = n + offset
    cost = dp[n] + abs(H[n] - H[new_index])
    if (dp[new_index] > cost):
      dp[new_index] = cost

print((dp[N - 1]))


# Optimized solution
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))

# H = deque(list(map(int, input().split())))
max_K = 100
H = list(map(int, input().split()))
H += [0] * max_K
# for _ in range(max_K):
#   H.append(0)

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

for n in range(N):
  for offset in range(1, K + 1):
    new_index = n + offset
    cost = dp[n] + abs(H[n] - H[new_index])
    if (dp[new_index] > cost):
      dp[new_index] = cost

print((dp[N - 1]))
# 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(dp[i-j]+abs(h[i]-h[i-j]) for j in range(1, min(i, k)+1))
print((dp[-1]))


# Optimized solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
INF = float('inf')
dp = [INF]*n
dp[0] = 0

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