peterverebics/CodeNet_Python_118 huggingface dataset:
n, k = map(int, input().split())
h = [int(i) for i in input().split()]

dp = [1e5]*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[n-1])
N, K = map(int,input().split())
H = list(map(int,input().split()))

#DP[i] i番目までの足場を見たときの足場iにたどり着く最小値

DP = [100000000 for _ in range(N + 1)] 
DP[0] = 0
for i in range(N):
    for k in range(1,K + 1):
        if 0 <= i - k and i - k < N:
            cost_min = 100000000
            cost_min = min(cost_min, DP[i - k] +  abs(H[i] - H[i - k]))
            DP[i] = cost_min
print(DP[N - 1])
n,k=map(int, input().split())
cost=list(map(int, input().split()))
dp=[999999]*n
dp[0]=0
for i in range(n):
	for j in range(i,i+k+1):
		if j < n:
			dp[j]=min(dp[j],dp[i]+abs(cost[i]-cost[j]))
print(dp[n-1])
N,K=list(map(int,input().split()))
dp_list=[10**7]*N
h=list(map(int,input().split()))
dp_list[0]=0
for i in range(N):
    for j in range(1,K+1):
        j=i+j
        if j<N:
            # if dp_list[j] is None:dp_list[j]=abs(h[j]-h[i])+dp_list[i]
            dp_list[j]=min(dp_list[j],abs(h[j]-h[i])+dp_list[i])
print(dp_list[-1])
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-1]- h[0])
    for j in range(max(i-k,1),i-2):
        mindp= min(dp [j]+ abs(h[i]- h[j]), mindp)
    dp[i]=mindp
print(dp[n-1])
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[100000000]*N
dp[0]=0

#配るDP作戦 dp[i]の値が分かっている場合にdp[i]を用いてdp[i+1],dp[i+2]...を更新
for i in range(N):
    for k in range(1,min(K+1,N-i)):
        dp[i+k]=min(dp[i+k],dp[i]+abs(h[i]-h[i+k]))
        # print("i=",i,"  dp=",dp)
print(dp[-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):
    if i >= k:
        loop = k
    else:
        loop = i
    tmp = []
    dp[i] = 10 ** 4
    for j in range(1, loop+1):
        dp[i] = min(dp[i], abs(h[i]-h[i-j])+dp[i-j])

print(dp[n-1])
n,k=map(int,input().split())
h=list(map(int,input().split()))
s=[0,h[1]-h[0]]
total=10**5
for i in range(2,n):
    for j in range(1,k+1):
        if i-j>=0:
            total1=abs(h[i]-h[i-j])+s[i-j]
        if total>total1:
            total=total1
    s.append(total)
print(s[-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
    for j in range(i-1,i-k-1,-1):
        if i>=1:
            m=min(m,ans[j]+abs(h[i]-h[j]))
        else:
            break
    ans[i]=m
print(ans[n])
n, k = list(map(int,input().split()))
l = list(map(int,input().split()))
dp = [10**8]*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)
import numpy as np
import sys

n, k = map(int, input().split())
dp = np.full(n, 10005, dtype=int)
h = np.array([int(x) for x in input().split()])
dp[0] = 0
for i in range(n):
	j = 1
	while i - j >= 0 and j <= k:
		dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
		j += 1
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):
    mi=10000
    if i<K:
        for j in range(i):
            mi=min(mi,DP[j]+abs(h[j]-h[i]),abs(h[i]-h[0]))
    else:
        for j in range(1,K+1):
            mi=min(mi,DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=mi

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:j+1])

print(dp[N-1][K])
import sys; input = sys.stdin.buffer.readline
import numpy as np
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = 2 ** 63 - 1

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

#処理内容
def main():
	N, K = getlist()
	h = np.array(getlist() + [0] * K, dtype = np.int64)
	
	DP = np.full(N + K, INF, dtype = np.int64)
	DP[0] = 0
	for i in range(N):
		DP[i + 1:i + K] = np.minimum(DP[i + 1:i + K],
			DP[i] + np.abs(h[i] - h[i + 1:i + K]))


	print(DP[N - 1])

if __name__ == '__main__':
	main()
N,K=map(int,input().split())
H=list(map(int,input().split()))
DP=[10**8]*N
DP[0]=0

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

print(DP[-1])
N, K = map(int, input().split())
H = list(map(int, input().split()))
H = [0] + H
dp = [0] * (N + 1)
dp[1] = 0
dp[2] = abs(H[2] - H[1])

for i in range(3, N+1):
    dp[i] = 1000000000
    for j in range(max(1, i - K), 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()))

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

for i in range(1, N):
    min_cost = 10**5
    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])
[a,k] = [int(x) for x in input().split()]
hs = [int(x) for x in input().split()]

dp = [0]* len(hs)
dp[0] = 0
dp[1] = abs(hs[1] - hs[0])
     
for i in range(2, a):
  
	dp[i] = min(abs(hs[i-1]- hs[i]) + dp[i-1], abs(hs[i-2]- hs[i]) + dp[i-2] )
      
print(dp[-1])
n,k = map(int,input().split())
h = list(map(int,input().split()))
dp = [10000]*n
for i in range(min(k+1,len(h))):
    dp[i] = abs(h[i]-h[0])
if k>=n:
    print(dp[-1])
    exit()
else:
    for i in range(k+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[-1])
n, k = map(int, input().split())
h = [int(i) for i in input().split()]

dp = [1e7]*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[n-1])
from builtins import abs
def main():
    n, k = map(int, input().split())
    stns = list(map(int, input().split()))
    INF = 10**9
    dp = n * [0]
    dp[0] = 0

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

if __name__ == "__main__":
    main()
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [float('inf') for _ in range(n)]
dp[0] = 0

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

DP = [1000001]*N
DP[0] = 0
DP[1] = abs(High[0]-High[1])

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

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

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

cost = [0]
for i in range(2,N+1):
    print(i)
    cost.append( min([cost[j] + abs(h[i-1]-h[j]) for j in range(max(0,i-K-1), i-1)]) )
print(cost[-1])
def chmin(dp,dif_h):
  if dp>dif_h:
    dp=dif_h
  return dp  

n,k,*h=map(int,open(0).read().split())
dp=[10000]*n
dp[0]=0
dp[1]=abs(h[0]-h[1])
for i in range(2,n):
  for j in range(i-k,i):
    if j<0:
      continue
    dp[i]=chmin(dp[i],dp[j]+abs(h[j]-h[i]))
print(dp[-1])
import sys
read = lambda: sys.stdin.readline().rstrip()
 
def main():
    INF = 10 ** 20
 
    n,k = map(int,input().split())
    height = tuple(map(int,read().split()))
    dp = [INF]*(n+1)
    dp[0] = 0
 
    
    for step in range(1, k + 1):
    	for target in range(1, n):
            if target-step >= 0: dp[target] = min(dp[target], dp[target - step] + abs(height[target] - height[target - step]))
 
    print(dp[n-1])

if __name__ == "__main__":
    main()
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[10000]*N
dp[0]=0
for i in range(N-1):
    for j in range(1,K+1):
        try:
            dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
        except:
            break
print(dp[N-1])
dp = [9999999 for i in range(100200)]
n, k = map(int, input().split())
hs = list(map(int, input().split()))

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

print(dp[n-1])
MAX = 10**4 + 1

n, k = map(int, input().split())
cost = list(map(int, input().split()))
dp = [MAX] * (n+1)
dp[0] = 0; dp[1] = 0
#dp[2] = abs(cost[1] - cost[0])

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

print(dp[-1])
n,m=input().split()
n=int(n)
m=int(m)
li=[int(k) for k in input().split()]
dp=[0]*n
dp[0]=0
dp[1]=li[1]-li[0]
for i in range(2,n):
  ans=10**9
  for j in range(1,m+1):
    if i-j<0:
      break
    ans=min(ans,abs(li[i]-li[i-j])+dp[i-j])
  dp[i]=ans
print(dp[n-1])
def frog(arr,n,k):
    if k>n or k <=0:
        return 0
    dp=[0]*(n+1)
    
    for i in range(2,n+1):
        dp[i]=10**9
        for j in range(i-1,0,-1):
            if i-j<k:
                dp[i]=min(dp[j]+abs(arr[i-1]-arr[j-1]),dp[i])
                
    return dp





value=[int(i) for i in input().split()]
n,k=value[0],value[1]
arr=[int(i) for i in input().split()]

print(frog(arr,n,k))
import numpy
N, K = map(int,input().split())
h = numpy.array(input().split(),numpy.int32)


dp = [10**5]*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,m = map(int,input().split())
l = list(map(int,input().split()))
dp = [0]+ [100000000000]*(n-1)
print(dp)
for i in range(1,n):
    ans = dp[i]
    for j in range(1,m+1):
        if i-j >= 0:
            ans = min(ans,dp[i-j]+abs(l[i]-l[i-j]))
    dp[i] = ans
print(dp[n-1])
n,k=map(int,input().split())
a=list(map(int,input().split()))
cost=[100000]*(n)
cost[0]=0
for i in range(n-1):
  for j in range(max(i-k, 0), i+1):
    cost[i+1]=min(cost[i+1], cost[j]+abs(a[i+1]-a[j]))
print(cost)
print(cost[-1])
n,k=map(int,input().split())
h=list(map(int,input().split()))

dp=[10*5]*(n+2)

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

for i in range(3,n+1):
  for j in range(max(i-k,1),i):
    dp[i]=min(dp[j]+abs(h[i-1]-h[j-1]),dp[i])
print(dp[n])
#!/usr/bin/env python3
# dp を何回も更新する方法

n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [10001 for _ in range(n)]

# 初期値
dp[0] = 0 
dp[1] = abs(h[1]-h[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+j]-h[i]))

print(dp[n-1])
INF = 10e5
n,k = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [INF for _ in range(n)]

dp[0] = 0

for i in range(0,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])
def frog(arr,n,k):
    if k>=n:
        return 0
    dp=[0]*(n+1)
    for i in range(2,n+1):
        dp[i]=10**9
        for j in range(i-1,0,-1):
            if i-j<=k:
                dp[i]=min(dp[j]+abs(arr[i-1]-arr[j-1]),dp[i])
                
    return dp[n]





value=[int(i) for i in input().split()]
n,k=value[0],value[1]
arr=[int(i) for i in input().split()]

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


print(dp[N-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))
cost =10000
DP=[0]*N
DP[0]=0
DP[1]=abs(H[1]-H[0])
for i in range(2,N):
  cost=10010
  for k in range(1,K+1):
    if (i-k)==-1:
      break
    cost=min(cost,(abs(H[i]-H[i-k])+DP[i-k]))
    #print(H[i],H[i-k],DP[i-k])
    DP[i]=cost
    #print(DP[i],i)
print(DP[N-1])
def main():
	n, k = map(int, input().split())
	costs = list(map(int, input().split()))
	v= [0] * n
	for step in range(1, n):
		v[step] = min([v[j] + abs(costs[step]-costs[step-j]) for j in range(max(0,step-k), step)])
	return v[-1]
print(main())
N, K = map(int, input().split())

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

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

for i in range(1, N):
    for j in range(1, K+1):
        if i - j < 0:
            continue
        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 = [2*10**4]*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+j]-h[i]))
print(dp[n-1])
import sys

n, jL = map(int, input().split())
values = list(map(int, input().split()))

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

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

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

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


for i in range(N):
    if i==0:
        cost[i]=0
    else:
        siten=max(i-K,0)
        min_cost=10**6
        for j in range(siten,i):
            m=cost[j]+abs(h[j]-h[i])
            min_cost=min(min_cost,m)
        cost[i]=min_cost

print(int(cost[N-1]))
# dp[i] 足場iまでいくコストの最小値

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

# dp[2]までは初期条件で確定
dp = [0 for i in range(n+1)]
dp[2] = abs(h[2]-h[1])

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


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

# 5,3
# 10,30,40,50,20

table = [0]*N
for i in range(1, N):
#     print(i)
    if i == 1:
        table[i] = abs(H[i-1]-H[i])
    else:
        table[i] = min([table[i-j]+abs(H[i]-H[i-j]) for j in range(1,K+1)])
print(table[-1])
n, k = map(int, input().split(" "))
h = [i for i in input().split(" ")]
dp = [10 ** 4] * n

dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        #print("i:%d j:%d" % (i, j))
        if i + j < n:
            dp[i + j] = min(int(dp[i + j]), dp[i] + abs(int(h[i]) - int(h[i + j])))
        else:
            break

print(dp[n - 1])
import numpy as np

#  入力
n, k = list(map(int, input().split()))
h = np.array(list(map(int, input().split())))

#  方針
#  i番目の足場にいるという状態に対して
#  その足場に最も少ないコストを記録する．

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

print(dp[n - 1])
import math

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

res = [10**6] * n
res[0] = 0

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

print(res[n-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(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])
INF = 10 ** 4 + 10

n, k = map(int, input().split())
H = list(map(int, input().split()))
for i in range(k):
    H.append(0)
DP = [INF] * (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])
n,k = map(int, input().split())
h = list(map(int, input().split()))
dp = [10 ** 18] * (n+1)
dp[1] = 0
for i,v in enumerate(h,1):
  for j in range(i+1,min(n+1,i+k+1)):
    dp[j] = min(dp[j],dp[i]+v)
print(dp[n])
N, K = [int(item) for item in input().split()]
h =[0] + [int(item) for item in input().split()]

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

for i in range(2, N+1):
    for k in range(1, K+1):
        if i - k >= 1:
            if dp[i] != 0:
                dp[i] = min(dp[i-k] + abs(h[i] - h[i-k]), dp[i])
            else:
                dp[i] = dp[i-k] + abs(h[i] - h[i-k])
print(dp[N])
from sys import stdin,stdout
n,k=map(int,stdin.readline().split())
l=list(map(int,stdin.readline().split()))
l1=[]
for i in range(min(n,k)):
    l1.append(abs(l[i]-l[0]))
for i in range(min(n,k),n):
    cost=10**5
    for j in range(1,k+1):
        cost=min(cost,abs(l[i]-l[i-j])+l1[-j])
    l1.append(cost)
stdout.write(str(l1[-1])+'\n')
import sys

readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
# INF = float('inf')
INF = 10**4

import numpy as np

N, K = map(int, readline().split())
H = np.array(readline().split(), dtype=np.int64)

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

# 配るDP
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]))
    
ans = dp[-1]
print(ans)
#Frog1
n,k=map(int,input().split())
h=list(map(int,input().split()))
h.insert(0,0)
dp={1:0}
for val3 in range(2,n+1):
    dp[val3]=int(1e4)
for val in range(2,n+1):
    for val2 in range(1,min(val,k+1)):
        dp[val]=min(dp[val],dp[val-val2]+abs(h[val]-h[val-val2]))
print(dp[n])
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
import bisect
import numpy as np

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

dp = np.zeros(N)
dp[0] = 0
dp[1] = abs(H[1] - H[0])

for i in range(2, N):
    start = max(0, i-K)
    min_cost = 10**4
    for j in range(start, i):
        cost = dp[j] + abs(H[j] - H[i])
        min_cost = min(min_cost, cost)
    dp[i] = min_cost

print(dp[N-1])
# coding=utf-8

if __name__ == '__main__':
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [10001] * 100010
    dp[0] = 0

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

    print(dp[N-1])
import sys
def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
n,k=get_ints()
h=get_array()
li=[10001]*n
li[0]=0

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



            li[i]=min(li[i],li[j]+abs(h[i]-h[j]))
##print(li)
print(li[n-1])
import sys
reader = (s.rstrip() for s in sys.stdin)
MAX = 1000000
sys.setrecursionlimit(MAX)
input = reader.__next__
# do magic here
n, k = map(int, input().split())
heights = [int(x) for x in input().split()]

dp = [0 for x in range(n)]

for x in range(1, n):
    i = x-1
    ans = MAX
    while i >= max(0, x-k):
        ans = min(ans, abs(heights[x]-heights[i]) + dp[i])
        i -= 1
    dp[x] = ans
print(dp[n-1])
n, K = map(int, input().split())
h = [int(x) for x in input().split()]
inf = float('inf')
dp = [inf] * n  # initialize dp
for i in range(1, n):
    if i == 1:
        dp[i] = abs(h[i] - h[i-1])
    else:
        for k in range(1, K+1):
            if i - k >= 0:
                kouho = dp[i-k] + abs(h[i] - h[i-k])
                dp[i] = min(kouho, dp[i])
            else:
                break
print(dp[-1])
[N,K]=list(map(int, input().split()))
h=list(map(int, input().split()))
K=min(N,K-1)

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:j+1])

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])
I = list(map(int, input().split()))
N = I[0]
K = I[1]
H = list(map(int, input().split()))

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

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

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

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

inf = sum(h)

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])
        #print(dpcand)
        if dpcand < mindp_i:
            mindp_i = dpcand

    dp[i] = mindp_i

print(dp[N-1])
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [0 for n in range(N)]
inf = 10**6
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 = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10**6

DP = [INF] * N
DP[0] = 0

for i in range(1, N):
    for j in range(1, K+1):
        if 0 <= i - j < N:
            DP[i] = min(DP[i], DP[i-j] + abs(H[i-j] - H[i]))
print(DP[N-1])
import math
import os
import random
import re
import sys
def dpkfindpath(a,k):
	dpath=[int(0) for _ in range(len(a))]
	for i in range(k):
		dpath[i]=abs(a[i]-a[0])
	
	for i in range(k,len(a)):
		li=[abs(a[i]-a[ik])+dpath[ik] for ik in range(i-1,i-k-1,-1) ]
		dpath[i]=min(li)
		
	return dpath[len(a)-1]
		
n,k=input().split()
n=int(n)
k=int(k)
a=input().split()
for i in range(n):
  a[i]=int(a[i])
if n>=k:
	print (dpkfindpath(a,k))
import sys
INT_MAX = 10000000000
data = sys.stdin.readline()
n,k = map(int,data.split())
data = sys.stdin.readline()
a = list(map(int,data.split()))
dp =[INT_MAX]*(n+1)
dp[0] = 0
if n==2:
    sys.stdout.write(str(a[1] - a[0]))
else:
    for i in range(n):
        for j in range(i+1,i+k):
            if i+j<n:
                dp[i+j] = min(dp[i+j],dp[i]+ abs(a[i+j]-a[i]))
    sys.stdout.write(str(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 = [int(c) for c in input().split()]
hs = [int(c) for c in input().split()]
dp = [0] * n
for i in range(1, n):
  dp[i] = 0
  pre = min(i, k)
  while pre > 0:
    dp[i] = min(dp[i], dp[i-pre] + abs(hs[i-pre] - hs[i]))
    pre -= 1
print(dp[-1])
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[i-1]+ abs(h[i]- h[i-1])
    for j in range(max(i-k-1,0),i-2):
        mindp= min(dp [j]+ abs(h[i]- h[j]), mindp)
    dp[i]=mindp
print(dp[n-1])
n,k= map(int,input().split())
h = [int(i) for i in input().split()]
inf = 10**7
dp = [inf]*100005
dp[0] = 0
for i in range(1,n):
    con = inf
    for j in range(1,k):
        if j > len(h):
            break
        con = min(con,dp[i-j]+abs(h[i]-h[i-j]))
    dp[i] = con
print(dp[n-1])
import numpy as np
N,K=map(int,input().split())
listH=list(map(int,input().split()))
cost=np.array([10**5]*N)
cost[0]=0
for i in range(1,N):
  for k in range(min(i+1,K)):
    cost[i]=min(abs(listH[i-k-1]-listH[i])+ cost[i-k-1],cost[i])
ans=cost[-1]      
print(ans)
N,K = map(int,(input().split()))
h = list(map(int,input().split()))
H = [0]*N
for i in range(1,N):
    if i == 1:
        H[i] = abs(h[1]-h[0])
    else:
        Min = 1000
        start = max((i-K),0)
        for j in range(start,i):
            if Min > H[j]+abs(h[i]-h[j]):
                Min = H[j]+abs(h[i]-h[j])
        H[i] = Min
print(H[N-1])