peterverebics/CodeNet_Python_118 huggingface dataset:
import math
from decimal import *

n, k = map(int, input().split())
nos = list(map(int, input().split()))
dp = [int(10e5) for i in range(n)]
dp[0] = 0
if(n>1):
    dp[1] = abs(nos[1]-nos[0])
for i in range(2, n):
    for x in range(1, min(k, i+1)+1):
        dp[i] = min(dp[i], abs(nos[i-x]-nos[i])+dp[i-x])
print(dp[n-1])
def solver():
	N,K = map(int, input().split())
	h = list(map(int, input().split()))
	dp = [0 for _ in range(N)]
	for i in range(1,N):
		min_cost = 100000000000000
		for j in range(max(0, i-K),i):
			cost = dp[j] + abs(h[i]-h[j])
			if cost < min_cost : min_cost = cost
		dp[i] = min_cost
	print(dp)
	print(dp[N-1])

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

dp = [0]*n 

for i in range(1,n):
    temp = 1000000
    for j in range(1,k+1):
        if i-j>=0:
            temp = min(dp[i-j]+abs(h[i]-h[i-j]), temp)
    dp[i] = temp
    
print(dp[-1])
n, k = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))

dp = [0] * (n)

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

MAX = 10**5

for i in range(2, n):
    temp = MAX
    for j in range(1, k+1):
        if (i - j) >= 0:

            temp = min(temp, dp[i-j] + (abs(arr[i] - arr[i-j])))
    dp[i] = temp

print(dp[-1])
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])
m = [0 if i-k<0 else i-k for i in range(2, n)]
for i, (hi, mi) in enumerate(zip(h[2:], m), 2):
    di = 10**9
    for dj, hj in zip(dp[mi:i-1], h[mi:i-1]):
        d = dj + abs(hj-hi)
        if di > d:
            di = d
    dp[i] = di
print(dp[-1])
n, k = map(int, input().strip().split(" "))
h = list(map(int, input().split(" ")))

dp = [10**5] * 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]-h[i+j])+dp[i])
        else:
            break

print(dp[n-1])
N,K=map(int,input().split())
listH=list(map(int,input().split()))
cost=[10**5]*N
cost[0]=0
for i in range(1,N):
  for k in range(1,K+1):
    if i-k>=0:
      cost[i]=min(abs(listH[i-k]-listH[i])+ cost[i-k],cost[i])
ans=cost[N-1]      
print(ans)
n,k=map(int,input().split())
h=[int(x) for x in input().split()]
a=[10000]*(n+k+1)
h.extend(a)
a[0]=0
for i in range(n):
  for j in range(1,k+1):
    a[i+j]=min(a[i+j], a[i]+abs(h[i]-h[i+j]))
print(a[n-1])
n,k=map(int,input().split())
hlist=list(map(int,input().split()))

mins=[0]

if n == 2:
	print(abs(hlist[1]-hlist[0]))

for i in range(1,n):	
	smallest=10001
	tmp=hlist[i]
	for j in range(min(k,i)):
		smallest=min(smallest,mins[i-j-1]+abs(tmp-hlist[i-j-1]))
	mins.append(smallest)

print(mins[n-1])
N,K=map(int,input().split())
h=list(map(int,input().split()))
cost=[0]
for i in range(1,N):
    cand=10**5
    for j in range(1,K+1):
        if j>i:
            break
        cand=min(cand,cost[i-j]+abs(h[i]-h[i-j]))
    cost.append(cand)
        
print(cost[-1])
n, k = [int(v) for v in input().split()]
h = [int(v) for v in input().split()]

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

for i in range(n - 1):
    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,h=map(int,input().split())
l=list(map(int,input().split()))
c=[10**9]*n
c[1]=abs(l[0]-l[1])
for i in range(2,n):
    c[i]=min( c[i-j]+abs(l[i]-l[i-j]) for j in range(1,h+1) if i-j > -1 )
print(c[-1])
n,k=map(int,input().split())
h=list(map(int,input().split()))

dp=[10**5]*n
dp[0]=0
dp[1]=abs(h[1]-h[0])

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

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

    dp = [10**5 for _ in range(n)]
    dp[0] = 0

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

main()
N,K= map(int,input().split())
h=[100000]+list(map(int,input().split()))
dp=[0]*(N+1)

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

if N>=3:
  for i in range(3,N+1):
    mini=100000
    for j in range(1,K+1):
      if i-j <1:
        pass
      else:
        mini=min(mini, dp[i-j] + abs(h[i]-h[i-j]) )
    dp[i]=mini
    
#print(dp)
print(dp[-1])
INF = 10 ** 20
 
n,k = map(int,input().split())
h = list(map(int,input().split()))
dp = [INF]*(n+1)
dp[0] = 0
 
for step in range(1, k+1):
    for i in range(step,n):
        dp[i] = min(dp[i], dp[i-step]+abs(h[i]-h[i-step]))
 
print(dp[n-1])
n,k=list(map(int, input().split()))
h=list(map(int, input().split()))
c=[0]
for i in range(n-1):
    d = 10100
    if i+1<k:
        for j in range(i+1):
            d=min(d,c[i-j]+abs(h[i-j]-h[i+1]))
    else:
        for j in range(k):
            d=min(d,c[i-j]+abs(h[i-j]-h[i+1]))
    c.append(d)
print(c[-1])
N,K=list(map(int, input().split()))
h=list(map(int, input().split()))


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


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

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

for i in range(n):
    if i == 0:
        ans.append(0)
    
    else:
        x = 10**5
        for j in range(k):
            if i-j-1 < 0:
                break
            y = ans[i-j-1] + abs(h[i]-h[i-j-1])
            if y < x:
                x = y
        ans.append(x)

print(ans[-1])
N, K = map(int, input().split())
h = list(map(int, input().split()))
dp = [float('inf') for i in range(N)]

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

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

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

dp = [-1] * N
dp[0] = 0

inf = 10000000

for i in range(1, N):
    mindp_i = inf

    for jump in range(1, min(i, K) + 1):
        dpcand = dp[i - jump] + abs(h[i] - h[i - jump])
        if dpcand < mindp_i:
            mindp_i = dpcand

    dp[i] = mindp_i

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] + 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 numpy as np

#入力
N,K=map(int,input().split())
H=list(map(int,input().split()))

dp = np.ones(N+1,dtype=float)*float('inf')
dp[0]=0
dp[1]=0
    

for i in range(N):
    if i!=0:
        for k in range(K):
            if i-k>=1:
                dp[i+1] = min ( dp[i+1] ,dp[i-k] + abs(H[i]-H[i-(k+1)]) )
print(dp[N])
# print(dp)
num=list(map(int, input().split()))
n=num[0]
k=num[1]
h=list(map(int, input().split()))
DP=[]
for i in range(n):
  DP.append(0)
DP[0]=0
DP[1]=abs(h[1]-h[0])
for j in range(2,n):
  DP[j]=DP[j-k]+abs(h[j-k]-h[k])
  for l in range(k,0,-1):
    if DP[j]>DP[j-l]+abs(h[j-l]-h[l]):
      DP[j]=DP[j-l]+abs(h[j-l]-h[j])
#print(DP)
print(DP[n-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))
DP=[0]*(N+1)
for i in range(2,N+1):
  a=10**4
  for j in range(min(K,i-1)):
    a=min(a,DP[i-j-1]+abs(H[i-1]-H[i-j-2]))
  DP[i]=a
print(DP[-1])
N,K=map(int,input().split())
h=list(map(int,input().split()))

cost=[]

for i in range(N):
  a=10**5
  if(i==0):
    cost.append(0)
  else:
    for j in range(min(i,K)):
      a=min(a,cost[i-j-1]+abs(h[i]-h[i-j-1]))
    cost.append(a)
print(cost[N-1])
def solve(N, K, arr):

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

	for k in range(1, K + 1):
		for i in range(1, len(ways)):
			if i - k >= 0:
				ways[i] = min(ways[i], ways[i-k] + abs(arr[i] - arr[i-k]))

	return ways[-1]

N, K = [int(s) for s in input().split()]
arr = [int(s) for s in input().split()]
print(solve(N, K, arr))
N, K = map(int,input().split())
h = [int(i) for i in input().split()]

dp = [10**4+1]*N 
dp[0] = 0

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

print(dp[-1])
n,k=map(int,input().split())
f=[100000000]*n
a=list(map(int,input().split()))
f[0]=0
f[1]=abs(a[0]-a[1])
for i in range(2,n):
    for j in range(1,k+1):
       if i-j<0:
           break
       a1=abs(a[i]-a[i-j])
       f[i]=min((f[i],f[i-j]+a1))
print(f[n-1])
INF = 10 ** 20

N,K = 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
    if i-K >= 0:
        dp[i] = min([dpk + abs(hi-hk) for dpk,hk in zip(dp[i-K:i],h[i-K:i])])
 
print(dp[-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))

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

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

INF = 10 ** 5
dp = [INF] * N

dp[0] = 0

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

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

dp=[10000000]*(N)
dp[0]=0
for i in range (1,N):
    dp[i]=min(dp[i],abs(hi[i]-hi[i-1])+dp[i-1])
    # print(dp)
    if 1 < i :
        for j in range(1,K+1):
            if j>i:
                pass
            else:
                dp[i]=min(dp[i],abs(hi[i]-hi[i-j])+dp[i-j])
            # print(dp)

# print(dp)
print(dp[-1])
from sys import stdin 

def main():

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

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

	L = [0 for _ in range(n)]

	for cur_ind in range(1, n):
		min_val = 10000000
		for prev_ind in range(max(cur_ind - k, 0), cur_ind):
			min_val = min(min_val, L[prev_ind] + abs(h[cur_ind] - h[prev_ind]))
		L[cur_ind] = min_val

	print(L[n - 1])







if __name__ == '__main__':
	main()
def solve(n,k,ar):

    dp = [int(1e5)]*n
    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(ar[i+k]-ar[i]))

    print(dp[n-1])


if __name__ == '__main__':
    n,k = map(int,input().split())
    ar = list(map(int,input().split()))

    solve(n,k,ar)
# EDPC A Frog 1
N, K = map(int, input().split())
H = list(map(int,input().split()))

f_inf = float("inf")
cost = [f_inf]*N

cost[0] = 0
cost[1] = abs(H[1]-H[0])

for i in range(N):
    for k in range(1,min(K+1,N-i)):
        cost[i] = min(cost[i], cost[i-k]+abs(H[i]-H[i-k]))
print(cost[N-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))
DP=[0]*(N+1)
for i in range(2,N+1):
  a=10**4
  for j in range(min(K,i-1)):
    a=min(a,DP[i-j-1]+abs(H[i-1]-H[i-j-2]))
  DP[i]=a
print(DP[N])
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[10**5 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(l[i+j]-l[i]))
print(dp[n-1])
N,K = map(int,input().split())
h = [0] + list(map(int,input().split()))

dp = [0] * (N+1)

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

print(dp[N])
import numpy as np

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

i = 0
total_cost = 0
 
a = np.zeros(N, int)
a[0] = 0
a[1] = abs(hs[1] - hs[0])

for i in range(2, N):
    bmin = float('inf')
    for j in range(K):
        if i-j+1 >= 0:
        	a[i] = min(a[i], a[max(i-(j+1),0)] + abs(hs[i] - hs[max(i-(j+1),0)]))    
        else:
        	break
print(a[-1])
(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(0, n, 1):
  for j in range(1, k+1, 1):
    if i+j < n:
    	dp[i+j] = min(dp[i+j], dp[i] + abs(h[i] - h[i+j]))
  print(dp)

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

c = [10001] * N
c[0] = 0

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

print(c[-1])
import sys
read = lambda: sys.stdin.readline().rstrip()
 
def main():
    INF = 10 ** 20

    n,k = map(int,input().split())
    h = tuple(map(int,read().split()))
    dp = [INF]*(n+1)
    dp[0] = 0

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

    print(dp[n-1])
if __name__ == "__main__":
    main()
N, K = map(int, input().split())
h = list(map(int, input().split()))
dp = [10**4 for _ in range(N)]
dp[0] = 0
for i in range(1, min(N, K+1)):
  dp[i] = abs(h[i] - h[0])

for j in range(N):
  for k in range(min(N, K+1)):
    if dp[j] > dp[j-k] + abs(h[j] - h[j-k]):
      dp[j] = dp[j-k] + abs(h[j] - h[j-k])
print(dp[N-1])
def solver():
	N,K = map(int, input().split())
	h = list(map(int, input().split()))
	dp = [0 for _ in range(N)]
	for i in range(1,N):
		min_cost = 100000
		for j in range(max(0, i-K),i):
			cost = dp[j] + abs(h[i]-h[j])
			if cost < min_cost : min_cost = cost
		dp[i] = min_cost
	print(dp)
	print(dp[N-1])

solver()
import numpy as np

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

i = 0
total_cost = 0
 
a = np.zeros(N, int)
a[0] = 0
a[1] = abs(hs[1] - hs[0])

for i in range(2, N):
    a[i] = np.min(a[max(i-K, 0):i] + abs(hs[i]-hs[max(i-K, 0)]))
print(a[-1])
number = list(map(int,input().split()))
line = list(map(int,input().split()))

num = number[0]
K = number[1]
dp = [0, abs(line[1] - line[0])]
for i in range(2, num):
  line2 = []
  for k in range(1, min(K + 1,i)):
    a = dp[-k] + abs(line[i] - line[i - k])
    line2.append(a)
  dp += [min(line2)]
print(dp[-1])
n,k=map(int,input().split())
h=list(map(int,input().split()))
INF=float('inf')

dp=[INF]*(n+1)
dp[0]=INF
dp[1]=0
s=[]
for i in range(2,n+1):
  if i-3<0:
    dp[i]=dp[i-1]+abs(h[i-1]-h[i-2])
  else:
    for j in range(1,k):
      s.append(dp[i-j]+abs(h[i-1]-h[i-j-1]))
      s.sort()
      dp[i]=s[0]
  
print(dp[n])
import numpy as np

N, K = map(int, input().split())
places = np.array(list(map(int, input().split())))
dp_table = np.zeros(N+1, dtype=np.int)
print(places)

for i in range(1, N):
    start = max(0, i-K)
    dp_table[i] = min(dp_table[start:i] + np.abs(places[i]-places[start:i]))

print(dp_table[N-1])
import numpy as np

N, K = map(int, input().split(" "))
Hs = [int(i) for i in input().split(" ")]

Hs = [0] + Hs

dp = np.full((N+1), np.Inf)

dp[0] = 0
for i in range(1,N+1):
    dp[i] = dp[i-1]+ abs(Hs[i-1]-Hs[i])

    for j in range(1,K+1):
        if i-j<0:
            continue
        
        dp[i] = min(dp[i], dp[i-j] + abs(Hs[i-j]-Hs[j]))
print(int(dp[N]))
n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=[0,abs(l[1]-l[0])]
for i in range(2,n):
    m=10**4+1
    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])
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 17 14:00:57 2020
@author: Ahmad Hamadneh
"""
x,y=input().split()
m=input().split()

dp=[]
dp.append(0)
print(dp[0],end=" ")
for i in range(1,int(x)):
    dp.append(abs(dp[i-1]+abs(int(m[i-1])-int(m[i]))))
    for j in range(2,int(y)+1):
        if i>=j:
           dp[i]=min(dp[i],abs(dp[i-j]+abs(int(m[i-j])-int(m[i]))))
    print(dp[i],end=" ")
#print(dp[int(x)-1])
n,k=map(int,input().split())
# k=int(input())
h=list(map(int,input().split()))

dp=[1000001 for i in range(n+1)]
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(1,n+1):
    for j in range(1,k+1):
        if(i+j<n):
            dp[i+j]=min(dp[i]+abs(h[i+j]-h[i]),dp[i+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):
    for k in range(1, K+1):
        if DP[i] > 0:
            DP[i] = min(abs(H[i] - H[max(i - k, 0)]) + DP[max(i - k, 0)], DP[i])
        else:
            DP[i] = abs(H[i] - H[max(i - k, 0)]) + DP[max(i - k, 0)]
        

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

dp = [10**4]*(n+1)
dp[0] = 0
for i in range(1,n):
    K = k if i >= k else i
    for j in range(1,K+1):
        t = dp[i-j] + abs(h[i-j]-h[i])
        if dp[i] > t: dp[i] = t
# print(dp)
print(dp[n-1])
n,k=map(int,input().split())
h=list(map(int,input().split()))
cost=[0]*n
cost[-2]=abs(h[-1]-h[-2])
for i in range(n-3,-1,-1):
  mini=10**6
  for j in range(1,k+1):
    
    if i+j<n:
      #print(i,i+j,abs(h[i]-h[i+j])+cost[i+j])
      mini=min(abs(h[i]-h[i+j])+cost[i+j],mini)
  cost[i]=mini
print(cost[0])
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,k,*H = list(map(int,read().split()))
h = [0]
h.extend(H)
for _ in range(k+1):
    h.append(0)

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

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

print(dp[n])
# coding: utf-8
# Your code here!

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

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


dp[0] = 0
dp[1] = abs(h[0]-h[1])
for i in range(N-1):
    tmp = 999999
    dp[i+1] = dp[i] + abs(h[i+1] - h[i])
    for j in range(min(K,i)+1):
        tmp = min(tmp, dp[i-j] + abs(h[i+1] - h[i-j]))
    dp[i+1] = tmp
print(dp[N-1])
n, k = map(int, input().split())
h = [int(i) for i in input().split()]
dp = [0]*n
dp[1] = abs(h[0] - h[1])
for i in range(2, n):
  if i <= k:
    dp[i] = abs(h[0] - h[i])
  else:
    dp[i] = min(dp[i-j] + abs(h[i-j]-h[i])
    for j in range(1, k+1))
  print(dp[-1])
INF = 10 ** 60


def li():
    return [int(x) for x in input().split()]


N, K = li()
H = li() + [INF] * K

dp = [INF] * (N + K)

dp[0] = 0

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

print(dp)
print(dp[N - 1])
n,k=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*n
rage=range #lol
for i in rage(1,n):
    if i<k: dp[i]=dp[i-1]+abs(h[i]-h[i-1])
    else: dp[i]+=min([dp[i-j]+abs(h[i]-h[i-j]) for j in rage(1,k+1)]);
print(dp[-1])
n,k=map(int,input().split())
a=list(map(int,input().split()))
dp=[99999999 for i in range(n)]
dp[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            dp[j]=min(dp[j],dp[i]+abs(a[i]-a[j]))
print(dp[n-1])
import numpy as np
from sys import stdin,stdout
n,k=[int(s) for s in stdin.readline().split()]
inf=100000000
# arr = [int(x) for x in stdin.readline().split()] 
a = np.asarray((stdin.readline()+(" 0"*(k+2))).split(), dtype=np.int64)
dp=np.full((n+k+2),inf,dtype=np.int64)
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(a[i+j]-a[i]))
    # dp[i+2]=min(dp[i+2],dp[i]+abs(a[i+2]-a[i]))
# print(dp)
print(dp[n-1])
n,k=map(int,input().split())
h=[int(i) for i in input().split()]
h.insert(0,0)
ans=[0]*(n+1)
ans[1]=0
ans[2]=abs(h[2]-h[1])
for i in range(3,n+1):
    m=1000000000000000
    boo=False
    for j in range(i-1,i-k-1,-1):
        if i>=1:
            m=min(m,ans[j]+abs(h[i]-h[j]))
            boo=True
        else:
            break
    if boo:
        ans[i]=m
print(ans[n])
INF = 10e5
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [INF for _ in range(n)]

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

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

print(dp[n-1])
n,k=tuple(map(int,input().split()))
l=list(map(int,input().split()))
dp=[1e5 for i in range(n)]
dp[0]=0
for i in range(1,n):
    for j in range(max(i-k,0),i):
        dp[i]=min(dp[i],abs(l[i]-l[j])+dp[j])

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

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


dp = [10000]*n
dp[0] = 0

for i in range(n):
    for j in range(1,k+1):
        if 0 <= i-j:

            dp[i] = min(dp[i], dp[i-j] + abs(h[i-j]-h[i]))

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

dp=[10**5]*n
dp[0]=0
dp[1]=abs(h[1]-h[0])

for i in range(2,n):
    for j in range(1,min(i,k)+1):
        dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
        
print(dp[-1])
def solve():
    dp = [0]+[max(heights)]*(n-1)
    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]))
    return dp[n-1]


if __name__ == '__main__':
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))
    print(solve())
import math
n,k =map(int, input().split())
h= list(map(int,input().split()))
dp=[0]*n
dp[1]=abs(h[1]-h[0])
for i in range(1,n):
    mindp=dp[0]+ abs(h[i]- h[0])
    for j in range(max(1,i-k),i-1):
        mindp= min(dp [j]+ abs(h[i]- h[j]), mindp)
    dp[i]=mindp
print(dp[n-1])
if __name__ == "__main__":
    n,k = map(int,input().split())
    arr = list(map(int,input().split()))
    dp = [-1]*len(arr)
    curr = 1 
    dp[0] = 0
    dp[1] = abs(arr[1]-arr[0])
    cost = 0
    for i in range(2,len(arr)):
        x = i-1
        c = 0
        while(x>=0 and c<k):
            dp[i]= min(dp[i],dp[x]+abs(arr[i]-arr[x]))
            x -= 1
            c += 1 
    print(dp[-1])
num=list(map(int, input().split()))
n=num[0]
k=num[1]
h=list(map(int, input().split()))
DP=[]
for i in range(n):
  DP.append(0)
DP[0]=0
DP[1]=abs(h[1]-h[0])
for j in range(2,n):
  DP[j]=DP[j-1]+abs(h[j-1]-h[j])
  for l in range(2,k+1):
    if l>j:
      continue
    if DP[j]>DP[j-l]+abs(h[j-l]-h[j]):
      DP[j]=DP[j-l]+abs(h[j-l]-h[j])
print(DP)
print(DP[n-1])
from math import inf

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

dp = [inf for i in range(n)]
dp[0] = 0

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

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

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

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

dp = [0] * N

for i in range(1, N):
    m = 10**6
    for j in range(1, K+1):
        k = i - j
        if k >= 0:
            m = min(m, abs(h[i] - h[k]) + dp[k])
        else:
            dp[i] = m
            break
    else:
        dp[i] = m
print(dp[N-1])
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):
    hi = h[i]
    di = dp[i-1] + abs(h[i-1]-hi)
    for j in range(max(1, i-k), i-1):
        dj = dp[j] + abs(h[j]-hi)
        if di > dj:
            di = dj
    dp[i] = di
print(dp[-1])
N, K = map(int, input().split())
h = [i for i in map(int, input().split())]

dp = [0]*N
dp[0] = 0
for i in range(1,N):
    start = min(K,i)
    min_h = 10**4*2
    for j in range(i-start,i):
        min_h = min(min_h, dp[j]+abs(h[j]-h[i]))
    dp[i] = min_h
print(dp[-1])
N, K = map(int, input().split())

H = list(map(int, input().split()))

dp = [0 for _ in range(N)]

for i in range(1, N):
    min_cost = 10000
    for j in range(1, K+1):
        if i - j < 0:
            continue
        cost = dp[i-j] + abs(H[i-j] - H[i])
        if cost < min_cost:
            min_cost = cost
    dp[i] = min_cost

print(dp[N-1])
N,K=map(int,input().split())
h=tuple(map(int,input().split()))
DP=[0]*N
DP[0]=0
DP[1]=DP[0]+abs(h[0]-h[1])
for i in range(1,N):
  for j in range(max(i-K,0),i):
    DP[i]=min(DP[i],DP[j]+abs(h[i]-h[j]))
print(DP[-1])