CodeEff/ECCO huggingface dataset:
# Original solution
#atcoder https://atcoder.jp/contests/dp/tasks/dp_b

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):

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

print((dp[N-1]))


# Optimized solution
#atcoder https://atcoder.jp/contests/dp/tasks/dp_b

def main():
    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):

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

    print((dp[N-1]))

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

ans=[0]*N
ans[1]=abs(h[1]-h[0])


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

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

    ans=[0]*N
    ans[1]=abs(h[1]-h[0])


    for i in range(2,N):
        ans[i] = min(abs(h[i] - h[j]) + ans[j] for j in range(max(0,i-K),i))
    print((ans[N-1]))
    
main()
# Original solution
def main():
    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-1):
        t = min(K+1, N-i)
        for j in range(1, t):
            dp[i+j] = min(dp[i+j], dp[i]+abs(h[i+j]-h[i]))
    print((dp[-1]))

main()


# Optimized solution
def main():
    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-1):
        for j in range(1, min(K+1, N-i)):
            dp[i+j] = min(dp[i+j], dp[i]+abs(h[i+j]-h[i]))
    print((dp[-1]))

main()
# 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(n):
    for j in range(0, min(i, k)+1):
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))

print((dp[-1]))


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

# n個の枠を作成
dp = [float("inf")] * n
# 初期値を0に
dp[0] = 0

#dp[i] =i番目まで来る最安の方法
for i in range(n):
    for j in range(1, min(i, k)+1):  # 上限が負にならないようにmin(i,k)で調整。+1でちゃんとrangeの癖を修正。
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))

print((dp[-1]))
# Original solution
import sys
# import pdb
input = sys.stdin.readline

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

dp_list = [-1 for _ in range(N)]
dp_list[0] = 0
dp_list[1] = abs(hs[1] - hs[0])

import heapq

sys.setrecursionlimit(1000000)
def dp_memo(p):
  if dp_list[p] != -1:
    return dp_list[p]

  costs = []
  heapq.heapify(costs)
  if K <= p:
    for i in range(1,K+1):
        heapq.heappush(costs, dp_memo(p-i) + abs(hs[p] - hs[p-i]))
        # costs.append(dp_memo(p-i) + abs(hs[p] - hs[p-i]))
  else:
    for i in range(1,p+1):
        heapq.heappush(costs, dp_memo(p-i) + abs(hs[p] - hs[p-i]))
        # costs.append(dp_memo(p-i) + abs(hs[p] - hs[p-i]))
  # result = min(costs)
  result = heapq.heappop(costs)
  dp_list[p] = result
  return result


def dp_recur():
  costs = []
  costs[0] = 0

  if N == 2:
    print(cost2)
    exit()
  # pdb.set_trace()
  for i in range(2, N):
    cost3 = min(cost2 + abs(hs[i] - hs[i-1]), cost1 + abs(hs[i] - hs[i-2]))
    cost1 = cost2
    cost2 = cost3
  return cost3

# print(dp_recur())
print((dp_memo(N-1)))


# Optimized solution
import sys
# import pdb
input = sys.stdin.readline

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

inf = 1e10

dp_list = [inf for _ in range(N)]
dp_list[0] = 0

# import heapq

sys.setrecursionlimit(1000000)
def dp_memo(p):
  if dp_list[p] != -1:
    return dp_list[p]

  costs = []
  # heapq.heapify(costs)
  if K <= p:
    for i in range(1,K+1):
        # heapq.heappush(costs, dp_memo(p-i) + abs(hs[p] - hs[p-i]))
        costs.append(dp_memo(p-i) + abs(hs[p] - hs[p-i]))
  else:
    for i in range(1,p+1):
        # heapq.heappush(costs, dp_memo(p-i) + abs(hs[p] - hs[p-i]))
        costs.append(dp_memo(p-i) + abs(hs[p] - hs[p-i]))
  result = min(costs)
  # result = heapq.heappop(costs)
  dp_list[p] = result
  return result


def dp_recur(N):
  for i in range(1, N):
    for j in range(1,K+1):
      if i-j < 0:
        break
      dp_list[i] = min(dp_list[i], dp_list[i-j] + abs(hs[i] - hs[i-j]))
  return dp_list[N-1]

print((dp_recur(N)))
# print(dp_memo(N-1))
# Original solution
import sys
# import pdb
input = sys.stdin.readline

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

inf = 1e10

dp_list = [inf for _ in range(N)]
dp_list[0] = 0

# import heapq

sys.setrecursionlimit(1000000)
def dp_memo(i):
  if dp_list[i] != inf:
    return dp_list[i]

  result = dp_list[i]

  for j in range(1, K+1):
    if i-j < 0:
      break
    result = min(result, dp_memo(i-j) + abs(hs[i] - hs[i-j]))

  dp_list[i] = result
  return result


def dp_recur(N):
  for i in range(1, N):
    for j in range(1,K+1):
      if i-j < 0:
        break
      dp_list[i] = min(dp_list[i], dp_list[i-j] + abs(hs[i] - hs[i-j]))
  return dp_list[N-1]

# print(dp_recur(N))
print((dp_memo(N-1)))


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

total_cost = [-1 for i in range(N)]

total_cost[0] = 0
total_cost[1] = abs(hs[1] - hs[0])

for i in range(2, N):
  for k in range(1, K+1):
    if total_cost[i] == -1:
      total_cost[i] = total_cost[i-k] + abs(hs[i] - hs[i-k])
    else:
      total_cost[i] = min(total_cost[i], total_cost[i-k] + abs(hs[i] - hs[i-k]))
    if i == k:
      break

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

costs = [1e10 for _ in range(N)]
costs[0] = 0

for i in range(N):
  for k in range(1, min(K+1, N-i)):
    costs[i+k] = min(costs[i+k], costs[i] + abs(hs[i+k] - hs[i]))

print((costs[N-1]))


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

cost = [1e30 for _ in range(N)]
cost[0] = 0

for i in range(N):
  for k in range(i+1, min(N, i+K+1)):
    cost[k] = min(cost[k], cost[i] + abs(H[k] - H[i]))

print((cost[N-1]))
# Original solution
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+1):
    for j in range(1, K+1):
        if i + j > N:
            continue
        dp[i+j] = min(dp[i+j], dp[i] + abs(H[i+j] - H[i]))

print((dp[N]))

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

dp = [10 ** 10] * (N+1)
dp[:2] = 0, 0

for i in range(1, N+1):
    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[N]))
# Original solution
from sys import stdin,stdout
INT_MAX= 100000000
n,k=list(map(int,stdin.readline().split()))
dp  = [INT_MAX]*(n)
h=[int(X) for X in stdin.readline().split()]
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(2,n):
    p=1
    while(p<k+1 or i-p>=0):
        dp[i]= min(dp[i-p+1]+abs(h[i-p+1]-h[i]),dp[i])
        p+=1
stdout.write(str(dp[n-1])+"\n")

# Optimized solution
from sys import stdin,stdout
INT_MAX= 10**18
n,k=list(map(int,stdin.readline().split()))
dp  = [INT_MAX]*(n)
h=[int(X) for X in stdin.readline().split()]
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(2,n):
    p=1
    while(p<k+1 and (i-p)>=0):
        dp[i]= min(dp[i-p]+abs(h[i-p]-h[i]),dp[i])
        p+=1
stdout.write(str(dp[n-1])+"\n")
# Original 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 in range(n-1):
    for j in range(1, min(k+1, n-i)):
        dp[i+j] = min(dp[i+j], dp[i] + abs(h[i+j] - h[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):
    for j in range(1, k+1):
        if i-j >= 0:
            dp[i] = min(dp[i], dp[i-j] + abs(h[i]-h[i-j]))
print((dp[n-1]))
# Original solution
mod=10**9+7
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return list(map(int,sys.stdin.readline().split()))
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a,b=mi()
l=list(mi())
dp=[0]*a
dp[0]=0
for i in range(1,a):
    dp[i]=min(abs(l[i]-l[i-j-1])+dp[i-j-1] for j in range(min(i,b)))
print((dp[-1]))

# Optimized solution
mod=10**9+7
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return list(map(int,sys.stdin.readline().split()))
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
def main():
  a,b=mi()
  l=list(mi())
  dp=[0]*a
  dp[0]=0
  for i in range(1,a):
    dp[i]=min(abs(l[i]-l[i-j-1])+dp[i-j-1] for j in range(min(i,b)))
  print((dp[-1]))
main()
# Original solution
# @oj:atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-04-20 10:31
# @url:https://atcoder.jp/contests/dp/tasks/dp_b
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):
        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()

# Optimized solution
import sys


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


def main():
    size, jumps = list(map(int, input().split()))
    array = list(map(int, input().split()))
    dp = [999999999] * size
    dp[0]= 0
    dp[1] = abs(array[0]- array[1])
    for i in range(2,size):
        dp[i] = min(abs(array[i] - array[j]) + dp[j] for j in range(max(0, i - jumps), i))
    print((dp[size-1]))

if __name__ == "__main__":
    main()
# Original solution
# @oj:atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-04-20 10:31
# @url:https://atcoder.jp/contests/dp/tasks/dp_b
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,x) in enumerate(dp[2:],2):
    #     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()

# Optimized solution
# @oj:atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-04-20 10:31
# @url:https://atcoder.jp/contests/dp/tasks/dp_b
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
def main():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))

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

    print((dp[-1]))
    return


if __name__ == '__main__':
    main()


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

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

    print((dp[-1]))
    return


if __name__ == '__main__':
    main()
# Original solution
n, k = list(map(int,input().split()))
l = list(map(int,input().split()))
dp = [10**10]*n
dp[0] = 0
dp[1] = abs(l[0] - l[1])
for i in range(2,n):
    for j in range(i-1,max(0,i-k)-1,-1):
        tem = dp[j] + abs(l[i] - l[j])
        if tem<dp[i]:
            dp[i] = tem
    
print((dp[n-1]))
#print(dp)    
    

# Optimized solution
n, k = list(map(int,input().split()))
l = list(map(int,input().split()))
dp = [10**9]*n
dp[0] = 0
dp[1] = abs(l[0] - l[1])
for i in range(2,n):
    for j in range(i-1,max(0,i-k)-1,-1):
        tem = dp[j] + abs(l[i] - l[j])
        if tem<dp[i]:
            dp[i] = tem
    
print((dp[n-1]))
# Original solution
N,K=list(map(int,input().split()))
H=list(map(int,input().split()))
dp=[0]#足場iまでの最小コスト
for i in range(1,N):
    l=max(0,i-K)
    #m=min([d+abs(H[i]-h) for d,h in zip(dp[l:i],H[l:i])])
    m=min(list(map(lambda d,h:d+abs(H[i]-h),dp[l:i],H[l:i])))
    dp.append(m)
print((dp[-1]))

# Optimized solution
N,K=list(map(int,input().split()))
H=list(map(int,input().split()))
dp=[0]#足場iまでの最小コスト
for i in range(1,N):
    l=max(0,i-K)
    m=min([d+abs(H[i]-h) for d,h in zip(dp[l:i],H[l:i])])
    #m=min(map(lambda d,h:d+abs(H[i]-h),dp[l:i],H[l:i]))
    dp.append(m)
print((dp[-1]))
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))

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

a = min(n, k)
for i in range(n):
    for j in range(1, min(k + 1, n - i)):
        dp[i+j] = min(dp[i+j], dp[i] + 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 ** 9] * n
dp[0] = 0

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

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

dp = [0]
for i in range(1, N):
    min_v = 1050000000
    for j in range(1, K+1):
        if i - j >= 0:
            min_v = min(min_v, dp[i - j] + abs(H[i] - H[i - j]))
    dp.append(min_v)
print((dp[N-1]))

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

dp = [0 for _ in range(N)]
for i in range(1, N):
    r = max(0, i - K)
    dp[i] = min(d + abs(H[i] - h) for h, d in zip(H[r:i], dp[r:i]))
print((dp[N-1]))
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

dp = [0 for _ in range(N)]
for i, hi in enumerate(H):
    if i == 0:
        continue
    r = max(0, i - K)
    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))
print((dp[N-1]))

# Optimized solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
 
dp = [0 for _ in range(N)]
for i in range(1, N):
    min_v = 1050000000
    for j in range(1, K+1):
        if i - j >= 0:
            min_v = min(min_v, dp[i - j] + abs(H[i] - H[i - j]))
    dp[i] = min_v
print((dp[N-1]))
# Original solution
def main():
    n,k = list(map(int,input().split()))
    h = [int(i) for i in input().split()]
    INF =  float('inf')
    dp = [INF for i 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]+abs(h[i]-h[i+j]),dp[i+j])
            else:
                break
                
    print((dp[n-1]))

main()

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

main()
# Original solution
from collections import Counter
from functools import reduce
import fractions
import math
import statistics
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]  # LIST INT
def LF(): return [float(x) for x in sys.stdin.readline().split()]  # LIST FLOAT
def LS(): return sys.stdin.readline().split()  # LIST STRING
def MI(): return map(int, sys.stdin.readline().split())  # MAP INT
def II(): return int(sys.stdin.readline())  # INPUT INT
def IS(): return input()  # INPUT STRING
def P(x): return print(x)
def C(x): return Counter(x)
def GCD_LIST(numbers):  # greatest common divisor
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):  # least common multiple
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))


n, k = MI()
dp = [INF] * n
h = LI()
dp[0] = 0
for i in range(0, n-1):
    for j in range(1, k+1):
        if n <= i+j:
            continue
        dp[i+j] = min(dp[i] + abs(h[i]-h[i+j]), dp[i+j])
P(dp[n-1])


# Optimized solution
from collections import Counter
from functools import reduce
import fractions
import math
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]  # LIST INT
def LF(): return [float(x) for x in sys.stdin.readline().split()]  # LIST FLOAT
def LS(): return sys.stdin.readline().split()  # LIST STRING
def MI(): return map(int, sys.stdin.readline().split())  # MAP INT
def II(): return int(sys.stdin.readline())  # INPUT INT
def IS(): return input()  # INPUT STRING
def P(x): return print(x)
def C(x): return Counter(x)
def GCD_LIST(numbers):  # greatest common divisor
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):  # least common multiple
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))


n, k = MI()
dp = [INF] * n
h = LI()
dp[0] = 0
for i in range(0, n-1):
    for j in range(1, k+1):
        if n <= i+j:
            continue
        dp[i+j] = min(dp[i] + abs(h[i]-h[i+j]), dp[i+j])
P(dp[n-1])
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

INF = 10 ** 12
dp = [INF] * 100010

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(h[i] - h[i-j]))

print((dp[N-1]))


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

INF = 10 ** 10
dp = [INF] * 100010

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] - h[i-j]))

print((dp[N-1]))
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = 1001001001
dp = [inf] * (n+1)

dp[0] = 0
dp[1] = abs(h[0]-h[1])

for i in range(n):
    for j in range(i + k):
        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()))
inf = 1001001001
dp = [inf] * (n+1)

dp[0] = 0
dp[1] = abs(h[0]-h[1])

for i in range(n):
    for j in range(k):
        if i + j >= n:
            break
        dp[i + j] = min(dp[i + j], dp[i-1] + abs(h[i+j] - h[i-1]))
print((dp[n-1]))
# Original solution
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    hs = list(map(int, input().split()))
    if n <= k+1:
        print((abs(hs[0]-hs[-1])))
        exit()
    ans_list = [0]*n
    last_hs = hs[-1]
    for i in range(1,k+1):
        ans_list[-i] = abs(last_hs - hs[-i])
    for i in range(k+1,n+1):
        hs_i = hs[-i]
        ans_list[-i] = min([abs(hs_i-hs[x])+ans_list[x] for x in range(1-i,k+1-i)])
    print((ans_list[0]))


# Optimized solution
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    hs = list(map(int, input().split()))
    if n <= k+1:
        print((abs(hs[0]-hs[-1])))
        exit()
    ans_list = [0]*n
    last_hs = hs[-1]
    for i in range(1,k+2):
        ans_list[-i] = abs(last_hs - hs[-i])
    for i in range(k+2,n+1):
        hs_i = hs[-i]
        ans_list[-i] = min([abs(hs_i-x)+y for x,y in zip(hs[1-i:k+1-i],ans_list[1-i:k+1-i])])
    print((ans_list[0]))
# Original solution
if __name__ == '__main__':

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

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



# Optimized solution
if __name__ == '__main__':

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

    #TLE pypy
    INF = 10**9
    dp = [INF] * (n+1)
    dp[0] = 0
    k = min(k,n)
    for i in range(1,n):
        for j in range(1,k+1):
            dp[i] = min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
    print((dp[n-1]))
# Original solution
n, k = list(map(int, input().split()))
# 2237
H = list(map(int, input().split()))


dp = [float("inf")]*(n+2)
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]-H[i+j]))

print((dp[n-1]))


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


dp = [float("inf")]*(n+2)
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]-H[i+j]))

print((dp[n-1]))
# Original solution
# Educational DP Contest B-Frog 2

def frog(N, K, h):
    # initialize
    inf = float('inf')
    dp = [inf] * N
    
    minKN = min(K, N)
    for i in range(minKN):
        if i == 0:
            dp[i] = 0
        else: 
            dp[i] = abs(h[0] - h[i])
            for j in range(minKN):
                if j == i:
                    continue
                elif j < i:
                    dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))
    # DP
    for i in range(N):
        if i == 0:
            continue
        elif i < K:
            continue
        else:
            dp[i] = min([abs(h[k] - h[i]) + dp[k] for k in range(i-K, i)])
    return dp[N-1]
    
if __name__ == "__main__":
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    print((frog(N, K, h)))

# Optimized solution
# ducational DP Contest B-Frog 2
    
N, K = list(map(int, input().split()))
h = list(map(int, input().split())) + [0]*K

dp = [float('inf') for _ in range(N+K)]
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(h[i+j] - h[i]))
        
print((dp[N-1]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

# DP
inf = float("inf")
dp = [inf for _ in range(N)]
dp[0] = 0
for i in range(1, N):
    for k in range(1, K+1):
        if i - k < 0:
            break
        dp[i] = min(dp[i], dp[i-k] + abs(h[i] - h[i-k]))
print((dp[-1]))

# Optimized solution
# Frog2
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = float("inf")
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:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i+j]))
print((dp[-1]))
# Original solution
import heapq
N, K  = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [0] * N
dp[1] = dp[0] + abs(h[1]-h[0])

for i in range(2, N):
    compare = []
    heapq.heapify(compare)
    cnt = min(K, i)
    for j in range(cnt):
        heapq.heappush(compare, dp[i-j-1] + abs(h[i]-h[i-j-1]))
    dp[i] = heapq.heappop(compare)

print((dp[-1]))


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

dp = [0] * N
dp[1] = dp[0] + abs(h[1]-h[0])

for i in range(2, N):
    compare = []
    cnt = min(K, i)
    for j in range(cnt):
        compare.append(dp[i-j-1] + abs(h[i]-h[i-j-1]))
    dp[i] = min(compare)

print((dp[-1]))