peterverebics/CodeNet_Python_118 huggingface dataset:
n,k = map(int,input().split())
h = list(map(int,input().split()))
k = min(k,n-1)
ans = [0]*(k+1)
temp_min = 10**10
for i in range(1,k+1):
    ans[i] = abs(h[0]-h[i])
for i in range(k+1,n):
    temp_min = 10**10
    for j in range(1,k+1):
        temp_min = min(temp_min,ans[i-j]+abs(h[i]-h[i-j]))
    ans.append(temp_min)
print(ans[-1])
def main():
    INF = 2 ** 60
    n, k = map(int, input().split())
    hs = 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:
                break
            dp[i + j] = min(dp[i + j], dp[i] + abs(hs[i] - hs[i + j]))

    print(dp[-1])


if __name__ == '__main__':
    main()
n, k = map(int, input().split())
h = [int(i) for i in input().split()]
dp = [0]
for i in range(1, n):
    dp += [min([dp[-j] + abs(h[i] - h[i - j]) for j in range(1, min(i, k) + 1)])]
print(dp[-1])
from sys import stdin as I
from sys import stdout as O
N,K = map(int,I.readline().split())
H = [None] + list(map(int,I.readline().split()))
DP = [1000000000000 for i in range(N+1)]
DP[1] = 0
for i in range(2,N+1): 
	for j in range(1,min(i-1,K)+1):DP[i] = min(DP[i],DP[i-j]+abs(H[i]-H[i-j]))
print(DP[N])
N, K = map(int, input().split())
h = list(map(int, input().split()))

INF = 10 ** 10
dp = [INF] * N
dp[0] = 0
dp[1] = min(dp[1], dp[0] + abs(h[0] - h[1]))

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

print(dp[N - 1])
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,k=map(int,input().split())
H=list(map(int,input().split()))
dp=[inf]*n; dp[0]=0
for i in range(n):
  for ki in range(1,k+1):
    if not (0<=i+ki<n): continue
    dp[i+ki]=min(dp[i+ki],dp[i]+abs(H[i]-H[i+ki]))
print(dp[n-1])
n,k = map(int, input().split())
h   = list(map(int, input().split()))

dp = [10**18] * (10**5+10)
dp[0] = 0

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

print(dp[n-1])
n, k = map(int, input().strip().split(" "))
h = list(map(int, input().split(" ")))

dp = [0] * n
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])
import sys
INF = 10**18

if __name__ == "__main__":
    n,k = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [INF]*n
    dp[0] = 0
    for i in range(1,n):
        c = INF
        for j in range(max(i-k,0),i):
            d = dp[j] + abs(h[j]-h[i])
            c = min(c,d)
        dp[i] = c
    print(dp[-1])
N, K = map(int, input().split())
h_list = list(map(int, input().split()))

dp_list = [0]
for n in range(1, N):
    tmp_h = h_list[n]
    if n < K:
        lst = [abs(tmp_h - h_list[i]) + dp_list[i] for i in range(n)]
    else:
        lst = [abs(tmp_h - h_list[n-k]) + dp_list[n-k] for k in range(1, K + 1)]
    dp_list.append(min(lst))
print(dp_list[-1])
# 貰うDP
N,K=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])
n, k = map(int, input().split())
h = [int(i) for i in input().split()]

def chmin(a, b):
    if a > b:
        return b
    else:
        return a
f_inf = pow(10,9)+99

dp = [f_inf] * (10**5+10)
dp[0] = 0
for i in range(1, n):
    for j in range(1, k+1):
        if i - j >= 0:
            dp[i] = chmin(dp[i], dp[i-j]+ abs(h[i] - h[i - j]))
print(dp[n-1])
def main():
    n, k = 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()
N,K=map(int, input().split())
*h,=map(int,input().split())
INF=10**12

dp = [INF]*N
dp[0]=0

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

dp = [10**10] * n
dp[0] = 0
for i in range(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])
n, k = map(int, input().split())
h = list(map(int, input().split()))

a = [10**10] * n

a[0] = 0
for i in range(0,n):
    for x in range(1,k+1):
        j = i+x
        if j >= n:
            continue
        a[j] = min(a[j], a[i] + abs(h[i] - h[j]))

print(a[n-1])
import sys

N, K = map(int, input().split())
H = list(map(int, sys.stdin.readline().rsplit()))

dp = [10 ** 9] * (N + K)
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] - H[i + k])
            )

print(dp[N - 1])
N,K = map(int,input().split())
H = list(map(int,input().split()))
dp = [float('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+j],dp[i]+abs(H[i]-H[i+j]))
print(dp[N-1])
N, K = 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(1, K+1):
        if N > i+j:
            dp[i+j] = min(dp[i+j], dp[i]+abs(h[i]-h[i+j]))

print(dp[N-1])
import numpy as np
N,K= map(int,input().split())
H = np.array(list(map(int, input().split())))
 
dp = np.zeros(N, dtype=int)
 
for i in range(1,N):
  s=max(i-K,0)
  dp[i]=np.min(dp[s:i]+np.abs(H[s:i]-H[i]))
 
print(dp[-1])
def main():
    N,K=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()
n, k = map(int, input().split())
arr = list(map(int, input().split()))

# dp初期化
dp = [float("inf")] * n
dp[0] = 0

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

print(dp[n - 1])
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [float('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]-h[i+j]))
print(dp[-1])
import numpy as np
n, k = list(map(int, input().split()))
H = np.array(list(map(int, input().split())), dtype = np.int)
dp = np.zeros((n, ), dtype = np.int)
for i in range(1, n):
    s, h = max(i - k, 0), H[i]
    dp[i] = np.min(dp[s:i] + abs(h - H[s:i]))
print(dp[n - 1])
N , M = map(int, input().split())
import numpy as np
list1=np.array(list(map(int, input().split())))
dp = np.array([float('inf') for i in range(N)])
dp[0] = 0
#print(np.abs(list1[1]-list1[1:1+1]))
for i in range(N):
    if i - M < 0:
        dp[i] = np.min(dp[:i+1]+np.abs(list1[i]-list1[:i+1]) )
    else:
        dp[i] = np.min(dp[i-M:i+1]+np.abs(list1[i]-list1[i-M:i+1]) )
    #print(dp)
print(int(dp[N-1]))
times, jump = [int(i) for i in input().split()]
line = input()
list1 = []
if times <= 1:
  print("%s" % 0)
else: 
  for i in line.split():
    if i:
      list1.append(int(i))
  mem = [0] * times
  mem[-2] = abs(list1[-1] - list1[-2])
  for i in range(len(list1)-3, -1, -1):
    ans = float("inf")
    end = min(len(list1), i+jump+1)
    for k in range(i+1, end):
      ans = min(ans, abs(list1[i] - list1[k]) + mem[k])
    mem[i] = ans
  print("%s" % mem[0])
import math
 
def solve(a,n,k):
  dp = [0]*(n)
  dp[0]=0
  inf = 10**9
  for i in range(1,n):
    dp[i]=inf
    r=max(0,i-k)
    dp[i]=min([abs(a[i]-a[j])+dp[j] for j in range(r,i)])
 
  #print(dp)
  return dp[-1]
n,k=map(int,input().split())
a=list(map(int,input().split()))
print(solve(a,n,k))
N, K=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[i]=min(dp[i], abs(h[i]-h[i-1]))
	else:
		start=max(0, i-K)
		dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(start, i))

print(dp[-1])
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [0 for n in range(N)]
inf = 10**10
for i in range(1, N):
	dp[i] = inf
	for k in range(max(0, i-K), i):
		dp[i] = min(dp[i], dp[k] + abs(H[i] - H[k]))
# print(dp)
print(dp[N-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))

c = [0 for i in range(n)]

for i in range(1, n):
    l = max(0, i-k)
    c[i] = min(cj + abs(hj - h[i]) for hj, cj in zip(h[l:i], c[l:i]))

print(c[n-1])
N, K = map(int, input().split())
hs = list(map(int, input().split()))

total_cost = [1e15 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])
def main():
    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))
    inf = float('inf')
    dp = [inf] * n
    dp[0] = 0
    #dp = [0] * n

    for i1, each_h in enumerate(h):
        if i1 == 0:
            continue
        start = max(0, i1 - k)
        dp[i1] = min([abs(each_h - each_h2) + each_dp for each_h2, each_dp in zip(h[start:i1], dp[start:i1])])

    print(dp[n - 1])

if __name__ == '__main__':
    main()
def main():
    n, k = 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()
# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(input())

N, K = geta(int)
h = geta(int)

INF = 1 << 60
dp = [INF] * N
dp[0], dp[1] = 0, abs(h[0] - h[1])

for i in range(2, N):
    lb = i - K if i > K else 0
    for j in range(lb, i):
        tmp = dp[j] + abs(h[j] - h[i])
        if tmp < dp[i]:
            dp[i] = tmp

print(dp[N - 1])
n, k = 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(0, i-k), i))

    
print(dp[n-1])
n,k = map(int,input().split())
h_L = list(map(int,input().split()))


#dp = [float("inf")] * (20)
dp = [float("inf")] * (10**5 + 1)
dp[0] = 0

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

print(dp[n-1])
#!/usr/bin/env python3

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

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

inf = float("inf")
dp = [inf for _ in range(N)]

dp[0] = 0

# 配るDP
for i in range(N):
    for k in range(1, K + 1):
        if i + k < N:
            k_step = dp[i] + abs(h[i] - h[i + k])
            dp[i + k] = min(dp[i + k], k_step)

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

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

for i in range(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])
N, K = map(int, input().split())
h = list(map(int, input().split()))

ans = [float("inf")] * N
ans[0] = 0
for i in range(N-1):
    for j in range(1, K+1):
        if i + j > N - 1:
            continue
        ans[i+j] = min(ans[i+j], ans[i] + abs(h[i] - h[i+j]))
print(ans[-1])
import sys
n,k=map(int,input().split())
# k=2
l=list(map(int,input().split()))
ans=[0,abs(l[1]-l[0])]
for i in range(2,n):
    m=sys.maxsize
    c=1
    while c<min(k+1,i+1):
        
        m=min(abs(l[i]-l[i-c])+ans[-c],m)
        c+=1
    ans.append(m)
# print(ans)
print(ans[-1])
def solve(a, n, k):
    f = [0] * n
    f[1] = abs(a[1] - a[0])

    for i in range(2, n):
        f[i] = float('inf')
        for j in range(max(0, i - k), i):
            f[i] = min(f[i], f[j] + abs(a[i] - a[j]))

    return f[n-1]


def main():
    inp = lambda: [int(x) for x in input().split()]
    n, k = inp()
    a = inp()
    print(solve(a, n, k))


if __name__ == '__main__':
    main()
N, K = 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(i+1,min(i+K+1, N)):
        dp[j] = min(dp[j], dp[i] + abs(H[i] - H[j]))

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

dp = [0]*N
for i in range(1,N):
    dp[i] = min(abs(H[i]-H[i-n]) + dp[i-n] for n in range(1,min(K+1,i+1)))

print(dp[-1])
N, K = map(int, input().split())
H = list(map(int, input().split()))
for _ in range(K+2):
    H.append(10**9)
dp = [10**9] * (N+K+1)
dp[0] = 0
# ひっぱる
for i in range(1, N):
    tmp = 10 ** 9
    for j in range(1, K+1):
        # 最小の移動コストを計算
        tmp = min(tmp, dp[i-j] + abs(H[i]-H[i-j]))
    dp[i] = tmp
print(dp[N-1])
import sys
input = sys.stdin.readline

def main():
    N, K = 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:
                dp[i] = min(dp[i], dp[i-j] + abs(H[i] - H[i-j]))
    print(dp[N-1])

if __name__ == '__main__':
    main()
INF = 10000
n,k = map(int,input().split())
stair = list(map(int,input().split()))
dp = [0] *n
for i in range(1,n):
    dp[i] = dp[i-1]+abs(stair[i]-stair[i-1])
    for j in range(1,k+1):
        if(i - j >= 0):
            # print(i,i-j)
            dp[i] = min(dp[i], dp[i-j]+abs(stair[i]-stair[i-j]))
        # print(dp)



print(dp[-1])
import numpy as np
import sys
input = sys.stdin.readline

N,K=map(int,input().split())
h=np.array(list(map(int,input().split())))

cost=np.array([-1 for _ in range(N)])


for i in range(N):
    if i==0:
        cost[i]=0
    else:
        siten=max(i-K,0)
        min_cost=np.min(cost[siten:i]+abs(h[siten:i]-h[i]))
        cost[i]=min_cost

print(cost[N-1])
#!/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])
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[0]*n
for i in range(1,min(k,n)):
	dp[i]=abs(l[i]-l[0])
#print(dp)
for i in range(k,n):
  dp[i]=dp[i-1]+abs(l[i-1]-l[i])
  for j in range(k+1):
  	dp[i]=min(dp[i],dp[i-j]+abs(l[i-j]-l[i]))
#print(dp)
print(dp[n-1])
N, K = map(int, input().split())
h = list(map(int, input().split()))

dp = [10 ** 9 for i in range(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])
am,k= list(map(int,input().split()))
arr = list(map(int,input().split()))
if am == 1:
    print(0)
else:
    dp = [0]
    for i in range(1,am):
        m = 9999999999999
        for g in range(max(i-k,0),i):
            m = min(dp[g] + abs(arr[g]-arr[i]),m)
        dp.append(m)
    print(dp[-1])
n,k=map(int,input().split())
A=list(map(int,input().split()))
#dpテーブルの初期化
#最小化問題
dp=[10*18]*n
dp[0]=0

for i in range(1,n):
    tmpmin=9999999999999999999999
    for j in range(k): 
        if i-j-1<0:
            break
        ans=dp[i-j-1]+abs(A[i]-A[i-j-1])
        if tmpmin>ans:
            tmpmin=ans

    dp[i]=tmpmin
print(dp[-1])
# dp array
import sys
input = sys.stdin.readline
def chmin(dp, idx, b):
    if dp[idx] > b:
        dp[idx] = b
        return True
    return False
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10 ** 9
    dp = [INF] * N
    dp[0] = 0
    for i in range(N):
        for j in range(1, K+1):
            if i+j < N:chmin(dp, i+j, dp[i]+abs(h[i]-h[i+j]))
    print(dp[-1])
if __name__ == "__main__":
    main()
n,k=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*n
for i in range(1,n):
  dp[i]=float('inf')
  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])
print(dp[n-1])
n, k = 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(max(0,i-k),i):
        # 今の足場へのコストと、他の足場から来た場合のコストを比較
        dp[i] = min(dp[i], dp[j] + abs(h[i]-h[j]))
print(dp[-1])
n,k = 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])
n,k=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]*n 
for i in range(1,n):
    if i==1:
        dp[i]=abs(a[i]-a[i-1]) 
    else:
        dp[i]=dp[i-1]+abs(a[i]-a[i-1])
        for j in range(2,k+1):
            if j<=i:
                dp[i]=min(dp[i],dp[i-j]+abs(a[i]-a[i-j]))
            else:
                break
print(dp[n-1])
import sys
def solve():
    N, K = map(int, input().split(" "))
    heights = [int(c) for c in 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(heights[i] - heights[i-j]))
            else:
                break
    print(dp[N-1])
solve()
import sys
input = sys.stdin.readline

import numpy as np

n, k = map(int, input().split())
h = np.array([int(i) for i in input().split()])

dp = np.array([0]*n)
dp[0] = 0

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

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

def frog_2():
    N, K = map(int,input().split())
    H = tuple(map(int,input().split()))

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

    for n in range(1, N):
        k = max(0, n - K) # k: 'n' ループ目の最大ジャンプ幅 'K' 
        h = H[n]          # h: 'n' ループ目の足場の高さ'H'
        dp[n] = min(abs(h - H[i]) + dp[i] for i in range(k, n))
        
    print(dp[-1])

frog_2()
from sys import stdin, stdout
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	n, k = rli()
	H = list(rli())
	dp = [INF for _ in range(n)]
	dp[0] = 0
	for i in range(1, n):
		for j in range(max(0, i-k), i):
			cost = abs(H[j] - H[i])
			dp[i] = min(dp[i], dp[j] + cost)
	print(dp[-1])
	stdout.close()

if __name__ == "__main__":
	main()
import sys
n,k = map(int,input().split())
a = list(map(int,input().split()))
cost = [0] * n
cost[1] = abs(a[1]-a[0])
mi = sys.maxsize
for i in range(1,n):
    for j in range(max(0,i-k),i):
        mi = min(mi,cost[j]+abs(a[i]-a[j]))
        #print(j,mi)
    
    cost[i] = mi
    mi = sys.maxsize
#print(cost)
print(cost[n-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp0 = [[0] * (k + 1)] * n
dp1 = [0] * n
for i in range(1, n):
    for j in range(k + 1):
        if j <= i and j != 0:
            dp0[i][j] = dp1[i - j] + abs(h[i] - h[i - j])
        else:
            dp0[i][j] = 10 ** 9
    dp1[i] = min(dp0[i])
print(dp1[n - 1])
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10**5

# DP = [INF] * ((10**5)+10)
DP = [None] * ((10**5)+10)
# DP = [None] * (N+1)
DP[0] = 0

for i in range(1, N):
    a = []
    for j in range(1, K+1):
        if 0 <= i-j < N:
            v = DP[i-j] + abs(H[i-j] - H[i])
            a.append(v)
    DP[i] = min(a)


# print(DP)
print(DP[N-1])
[N,K]=list(map(int, input().split()))
h=list(map(int, input().split()))
K=min(N,K)

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

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

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

print(dp[N-1][K])
def main():
    n, k = tuple([int(e) for e in input().split()])
    arr = [int(e) for e in input().split()]
    t = [float('inf')] * (n + 1)
    t[0] = 0
    t[1] = 0 
    for i in range(2, n + 1):
        for j in range(1, k + 1):
            if i - j <= 0:
                break
            l1 = t[i - j] + abs(arr[i - j - 1] - arr[i - 1])
            t[i] = min(t[i], l1)
    print(t[n])
    return 0


if __name__ == '__main__':
    main()
N,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
DP = [1000000000]*N
DP[0] = 0
def handan(i,x):
    if DP[i] > x:
        DP[i] = x
for i in range(1,N):
    for j in range(min(i,k)):
        x = DP[i-j-1] + abs(h[i]-h[i-j-1])
        handan(i,x)
print(DP[N-1])
import numpy as np
def resolve():
    n, k = map(int, input().split())
    h = np.array(list(map(int, input().split())))

    # 足場iにたどり着くまでの最小コスト
    # ただし足場は0番からn - 1番まで存在する
    dp = np.zeros(n, dtype=int)

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

    print(dp[n - 1])
    
resolve()
def main():
    from builtins import abs
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

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


main()
from numba import njit


@njit
def main(N, K, h):
    dp = [10**10] * (N+1)
    dp[0] = 0

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

    return dp[N-1]


if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    print(main(N, K, h))
N, K = map(int, input().split(' '))
h = [None] + list(map(int, input().split(' ')))

dp = [None for _ in range(N+1)]
# dp[0], dp[1], ..., dp[N] が用意される

dp[1] = 0
for k in range(2, N+1): # k = 2, ..., N
  candidate = []
  for i in range(1, K+1): # i = 1, 2, ..., K
    if k - i <= 0:
      break
    candidate.append(dp[k-i] + abs(h[k] - h[k-i]))
  dp[k] = min(candidate)

print(dp[N])
n,k=map(int,input().split())
h=list(map(int,input().split()))

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

print(dp[n-1])
#!/usr/bin/env python3
import numpy as np

N, K = list(map(int, input().split()))
h_list = list(map(int, input().split()))
h_list = np.array(h_list)
dp_list = np.zeros(N)
dp_list[0] = 0

for i in range(1, N):
    # j番目の足場からi番目の足場に飛ぶ

    min_j = max(i - K, 0)
    cost_list = dp_list[min_j:i] + np.abs(h_list[i] - h_list[min_j:i])
    # 最適コストで更新
    dp_list[i] = np.min(cost_list)

ans = int(dp_list[-1])
print(ans)
import sys

def main():
  n, k, *h = 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()
N, K = 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])