peterverebics/CodeNet_Python_118 huggingface dataset:
N,K = [int(_n) for _n in input().split()]
h = [int(_n) for _n in input().split()]
dp = [0]*N
dp[1] = abs(h[1]-h[0])
for i in range(1,N):
    dp[i] = 1000000
    for j in range(max(i-K,0), i):
        dp[i] = min(dp[i], dp[j]+abs(h[j]-h[i]))
print(dp[N-1])
n,k = map(int,input().split())
h = list(map(int,input().split()))

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

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

DP = [100000 for i in range(n)]
DP[0] = 0
for i in range(1, n):
    for j in range(1, k+1):
        if i - j >= 0:
            tmp = DP[i - j] + abs(H[i - j] - H[i])
            DP[i] = min(DP[i], tmp)
        else:
            pass

print(DP[n - 1])
def min_cost(h, n, k):
    dp = [10**6 for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        for k in range(1, k + 1):
            if (i - k) >= 0:
                dp[i] = min(abs(h[i] - h[i - k]) + dp[i - k], dp[i])


    print(dp[n - 1])

n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
min_cost(h, n, k)
import sys
sys.setrecursionlimit(10**6)
n,k = map(int,input().split())
h = list(map(int, input().split()))

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

     print(dp[n-1])
else:
     print(min([abs(h[0]-h[i]) for i in range(n)]))
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().split())
h = list(map(int, input().split()))
for j in range(K):
    h.append(0)
INF = float('inf')
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]-h[i+j]))
print(dp, h)
import math
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
S = [0] * N
S[0] = 0
 
for i in range(1, N):
    value = math.inf
    for step in range(1, N):
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
    S[i] = value
print(S[N - 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(2,n):
    mindp=dp[i-1]+ abs(h[i]- h[i-1])
    for j in range(max(i-k,0),i-2):
        mindp= min(dp[j]+ abs(h[i]- h[j]), mindp)
    dp[i]=mindp
print(dp[n-1])
def solve(a,k,dp):
	for i in range(2,n+1):		
		for j in range(i-1, max(0, i-k-1), -1):
			# print(i,j)
			dp[i] = min(dp[i], dp[j] + abs(a[i]-a[j]))
			# print(dp[j], i, j)
		# print(dp[i])
	


n,k = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0,0)

dp = [99999 for i in range(n+2)]
dp[1] = 0
# dp[1] = abs(a[1]-a[0])

solve(a,k,dp)


# print(dp)
print(dp[n])
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[10000]*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().split())
H=list(map(int,input().split()))
cost =200000
DP=[0]*(N+10)
DP[0]=0
DP[1]=abs(H[1]-H[0])
for i in range(2,N):
  cost=200000
  for k in range(max(i-K,0),i):
    cost=min(cost,(abs(H[i]-H[k])+DP[k]))
  DP[i]=cost
print(DP[N-1])
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()])
if n - 1 <= k:
	print(abs(h[n - 1] - h[0]))
	sys.exit()
if k == 1:
	s = 0
	for i in range(n - 1):
		s += abs(h[i + 1] - h[i])
	print(s)
	sys.exit()	 
dp[0] = 0
dp[1] = abs(h[1] - h[0])
for i in range(2, n):
	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 - 1])
N, K = map(int, input().split())
h = list(map(int, input().split()))

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

def cost(n, m):
  return abs(h[n] - h[m])

dp[0] = 0

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

print(dp)
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [1 << 62] * N
dp[0] = 0
for i in range(1, N):
    for j in range(i, min(i + 1 + K, N)):
        cost = abs(H[i - 1] - H[j])
        dp[j] = min(dp[j], dp[i - 1] + cost)
print(dp[-1])
import sys
N, K = map(int, input().split())
h = list(map(int, input().split()))
DP = [0]*N
if N < K:
    print(abs(h[0]-h[N-1]))
    sys.exit()

for i in range(1, K+1):
    DP[i] = abs(h[0]-h[i])
if N == K:
    print(DP[N-1])
    sys.exit()

for i in range(K, N):
    DPmin = 10000000
    for j in range(i-K, i):
        DPmin = min(DPmin, DP[j]+abs(h[j]-h[i]))
    DP[i] = DPmin
print(DP[N-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])
def main():
    N, K = list(map(int, input().split()))
    field = list(map(int, input().split()))

    dpList = [0] * N
    dpList[1] = abs(field[0] - field[1])

    for i in range(2, N):
        minCost = 1000000

        for j in range(1, min(K, i) + 1):
            minCost = min([minCost, dpList[i - j] + abs(field[i] - field[i - j])])

        dpList[i] = minCost

    print(dpList[-1])

if __name__ == "__main__":
    main()
n,k=map(int,input().split())
a=list(map(int,input().split()))
cost=[100000]*(n)
cost[0]=0
for i in range(1, n):
  for j in range(max(i-k, 0), i+1):
    cost[i]=min(cost[i], cost[j]+abs(a[i]-a[j]))

print(cost[-1])
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [float('inf') for _ in range(n)]
    dp[0] = 0
    dp[1] = abs(h[0]-h[1])
    for i in range(1, n):
        for j in range(1, min(k+1, n-i)):
            num = dp[i]+abs(h[i]-h[i+j])
            if num < dp[i+j]:
                dp[i+j] = num
    print(dp[-1])

main()
n, k = map(int, input().split())
h = list(map(int, input().split()))
MAXINT = 10**8
dp = [MAXINT for _ in range(n)]
dp[0] = 0

def chmin(a, b):
    if (a > b):
        a = b
        return True
    return False

for i in range(n-1):
    for j in range(1, k):
        if i+j>=n:
            break
        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])
n,k = map(int,input().split())
h = list(map(int,input().split())) + [0]*k
cost = [0] + [10000]*(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])
N, K = map(int, input().split())
H = list(map(int, input().split()))
INF = 10**6

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

for i in range(N):
	for j in range(K+1):
		if i + j <= N-1:
			ans[i+j] = min(ans[i+j], ans[i] + abs(H[i+j] - H[i]))
print(ans[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()))
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[-j-1] + abs(h[i]-h[i-j-1])
            if y < x:
                x = y
        ans.append(x)

print(ans[-1])
import sys
n,k=list(int(i) for i in input().split())
A=list(int(i) for i in input().split())
# n,k=10,4
# A=[40,10,20,70,80,10,20,70,80,60]
dpmincost=[0 for i in range(n)]
dpmincost[0]=0
for i in range(1,n):
    x=sys.maxsize
    for j in range(1,k+1):
        if i-j>=0:
            x=min(dpmincost[i-j]+abs(A[i]-A[i-j]),x)
    dpmincost[i]=x
print(dpmincost)
def min_cost(h, n, k):
    dp = [10**6 for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        for k in range(1, k + 1):
            if (i - k) >= 0:
                dp[i] = min(abs(h[i] - h[i - k]) + dp[i - k], dp[i])


    return dp[n - 1] 

n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
print(min_cost(h, n, k))
import math
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
S = [0] * N
S[0] = 0
 
for i in range(1, N):
    value = math.inf
    for step in range(1, i + 1):
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
    S[i] = value
print(S[N - 1])
n,k = map(int,input().split())
h = list(map(int,input().split()))

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

for i in range(n):
    for j in range(k+1):
        if i+j<n:
            dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j])
print(dp[n-1])
n,k = list(map(int, input().split()))
nums = list(map(int, input().split()))
best = [99999999]*n
best[0] = 0
for i in range(1,n):
    for j in range(1,min(i+1,k+1)):
        #print(i,i-j, "CHECKING", best[i-j],abs(nums[i-j]-nums[i]))
        best[i] = min(best[i], best[i-j]+abs(nums[i-j]-nums[i]))

print(best[-1])
N,K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
minToatalCostList = [10000 for i in range(N)]
minToatalCostList[0] = 0

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

print(minToatalCostList[N-1])
n,k=map(int,input().split())
h=list(map(int,input().split()))
cost=[float("inf")]*n
cost[0]=0
cost[1]=abs(h[1]-h[0])

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

print(cost[n-1])
n,k=list(map(int, input().split()))
stones=list(map(int, input().split()))
infini=(10**4)*(10**5)
dp = [infini for _ in range(n)]
dp[0]=0

for pos in range(0, n-1):
   for m in range(1, k):
      if pos+k < n:
         dp[pos+k] = min(dp[pos+k], dp[pos] + abs(stones[pos] - stones[pos+k]))
print(dp[-1])
n,k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0]*(n-1)

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

print(dp[n-2])
n,k=map(int,input().split())
h=list(map(int,input().split()))
dp=[0 for i in range(n)]
dp[1] = dp[0] + abs(h[1]-h[0])
for i in range(2,n):
  choice=100000
  for j in range(max(i-k,0), i):
    tmp = dp[j]+abs(h[i]-h[j])
    if tmp < choice:
      choice = tmp
  dp[i] = choice
print(dp[n-1])
N, K = map(int, input().split())
places = list(map(int, input().split()))

dp_table = [99999999] * (N+1)

# 初期化
dp_table[0] = 0
dp_table[1] = abs(places[1] - places[0])

for i in range(2, N):
    for j in range(1, K+1):
        cost = dp_table[i-j] + abs(places[i] - places[i-j])
        dp_table[i] = dp_table[i] if cost > dp_table[i] else cost

print(dp_table)
[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])
 
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])
 
print(dp[N-1][K])
import numpy as np

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



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

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

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

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

    print(dp.pop())

main()
N,K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
minToatalCostList = [100000 for i in range(N)]
minToatalCostList[0] = 0

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

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

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

inf = 100000

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])
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):
    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]))
        else:
            break
print(dp[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(3,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)
from sys import stdin,stdout
INT_MAX= 100000000
n,k=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+1]+abs(h[i-p+1]-h[i]),dp[i])
        p+=1
stdout.write(str(dp[n-1])+"\n")
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,min(k+1,n)):
    dp[i]=abs(l[i]-l[0])
for i in range(1,n):
    for j in range(max(i-k,1),i):
        dp[i]=min(dp[i],abs(l[i]-l[j])+dp[j])

print(dp[n-1])
#print(dp)
N,K=map(int,input().split())
h_list=list(map(int,input().split()))
dp=[0]*N
for i in range(1,N):
    dp[i]=float("inf")
    for j in range(1,min(i,K+1)+1):
        dp[i]=min(dp[i],dp[i-j]+abs(h_list[i-j]-h_list[i]))
print(dp[-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))
cost =20000
DP=[0]*(N+10)
DP[0]=0
DP[1]=abs(H[1]-H[0])
for i in range(2,N):
  cost=20000
  for k in range(max(i-K,0),i):
    cost=min(cost,(abs(H[i]-H[k])+DP[k]))
  DP[i]=cost
print(DP[N-1])
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    c = [0] + [abs(h[1] - h[0])]
    for i in range(2, n):
        min_step = 10 ** 4
        num = i if i <= k else k + 1
        for j in range(1, num):
            step = abs(h[i] - h[i-j]) + c[i-j]
            if step < min_step:
                min_step = step
        c.append(min_step)
    print(c.pop())


if __name__ == "__main__":
    main()
N, K = map(int, input().split())
H = list(map(int, input().split()))
DP = [100000]*N
for i, h in enumerate(H):
    if i == 0:
        DP[0] = 0
    for k in range(1,K+1):
        if i - k >= 0 :
            DP[i] = min(DP[i], DP[i-k]+abs(H[i]-H[i-k]))
print(DP[-1])
n,k = map(int,input().split())
h = [int(i) for i in 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<0:
            break
        
        dp[i]= min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
    print(dp)
print(dp[n-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [float("inf")]*n
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])
import sys
INF = 1e9 + 5
def solve(n, k, ls):
	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(ls[i+j]-ls[i]))
	return(dp[n-1])  
 
if __name__ == "__main__":
    f = sys.stdin
    fo = sys.stdout
    n, k = map(int, f.readline().split())
    arr = list(map(int, f.readline().split()))
    result = solve(n, k, arr)
    print(result)
n, k = map(int, input().split())
h = list(map(int, input().split()))
cost = [-1] * n

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

for i in range(k, n):
  tmp = 100000
  for j in range(i-k, i):
    tmp = min(tmp, abs(h[j] - h[i]) + cost[j])
  cost[i] = tmp

print(cost[n-1])
N, K = map(int, input().split())
h = list(map(int, input().split()))
h = h + [0] * (K + 1)

# DPテーブル
dp = [10 ** 10] * (N + 1 + K)

# 初期値
dp[0] = 0
dp[1] = 0

# ループ
for i in range(1, N + 1):
    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])
# EDPC A Frog 1
N, K = map(int, input().split())
H = list(map(int,input().split()))

cost = [float("inf")]*N
cost[0] = 0
cost[1] = abs(H[1]-H[0])

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

print(cost[N-1])
N,K = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
DP = [100000000]*N
DP[0] = 0
DP[1] = abs(h[0] - h[1])
for i in range(2,N):
    for j in range(1,min(K+1,i+1)):
        DP[i] = min(DP[i],DP[i-j] + abs(h[i]-h[i-j]))
print(DP[N-1])
infinity=10**8
N,K = map(int,input().split())
h = list(map(int,input().split()))

dp=[infinity]*N
dp[0]=0

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

print(dp[-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==1: 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])
import math
from decimal import *

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

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

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

print(dp[N-1])
"""
Code of Ayush Tiwari
Codeforces: servermonk
Codechef: ayush572000

"""
import sys
input = sys.stdin.buffer.readline

def solution():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    dp=[0]*n
    dp[1]=abs(l[1]-l[0])
    for i in range(2,n):
        m=2e12
        for j in range(1,min(i,k)+1):
            m=min(m,dp[i-k]+abs(l[i]-l[i-j]))
        dp[i]=m
    print(dp[n-1])

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

c = []
c.append(0)

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

print(c)
print(c[n-1])
import numpy as np
N, K = map(int,input().split())
h = list(map(int, input().split()))

dp = np.full(N, np.inf)
dp[0] = 0
for i in range(1,N):
  for j in range(1,K+1):
    if i >= j: dp[i] = min(dp[i],dp[i-j]+np.abs(h[i] - h[i-j]))
print(dp)
print(int(dp[-1]))
#!/usr/bin/env python3
n,k = map(int,input().split())
h = list(map(int,input().split()))
INF = 10**6
dp = [INF]*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 >= 0: 
            dp[i] = min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
print(dp[n-1])
N,K = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
DP=[10000]*(N+1)
DP[0] = 0
DP[1] = 0
DP[2] = DP[1] + abs(h[0]-h[1])
for i in range(3,N+1):
    for j in range(1,min(K+1,i)):
        DP[i] = min(DP[i],DP[i-j]+abs(h[i-j-1]-h[i-1]))
print(DP[N])
[n, k] = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
dp = [10 ** 5 for _ in range(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())
hl = list(map(int,input().split()))

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

for i in range(1,n):
    tmp = float('inf')
    for j in range(1,k+1):
        if i-j < 0:
            continue
        tmp = min(dp[i-j] + abs(hl[i]-hl[i-j]),tmp)
    dp[i] = tmp
print(dp)
x,k=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]*x
#dp[1]=abs(a[1]-a[0])
for i in range(1,x):
    p=1000001
    #print(i-k,i)
    for j in range(i-1,max(0,i-k)-1,-1):
        p=min(p,dp[j]+abs(a[i]-a[j]))
        
    dp[i]=p
print(dp[x-1])
INF=100000000
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[INF]*100010
dp[0]=0
for i in range(N):
    for j in range(K+1):
        if(i+j<N):
            dp[i+j]=min(dp[i+j],dp[i]+abs(h[i]-h[i+j]))
        else:
            break
    
print(dp[N-1])
n,k=map(int,input().split())
A=list(map(int,input().split()))


if k>=n:
  print(abs(A[n-1]-A[0]))
else:
  dp=list(range(n))
  for i in range(k):
    dp[i]=abs(A[i]-A[0])
  for i in range(n-k):
    temp=list(range(k))
    for j in range(k):
      temp[j-1]=dp[i]+abs(A[i+k]-A[i])
      dp[i+k]=min(temp)
  print(dp[n-1])
#!/usr/bin/env python3
# dp を何回も更新する方法

n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [100100000 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])
n,k=map(int,input().split())
h=list(map(int,input().split()))
dp=[1001]*(n+1)
h.insert(0,0)
dp[1]=0
dp[0]=0
dp[2]=abs(h[2]-h[1])
for i in range(3,n+1):
    for j in range(i-1,max(i-k-1,0),-1):
        dp[i]=min(dp[i],abs(h[i]-h[j])+dp[j])
print(dp[n])
N, K = map(int, input().split())
h = list(map(int, input().split()))

DP = [100000]*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[-1])
#!/usr/bin/env python3
N, K = map(int, input().split())
H = list(map(int, input().split()))

dp = [float('inf') for i in range(N+K+1)]

if K >= N:
    print(0)
    exit()
dp[0] = 0
for i in range(1, K+1):
    dp[i] = abs(H[i]-H[0])


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

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

for i in range(2, n):
    m1 = 1000001
    j = 1
    while (j <=k and i - j >= 0):
        m = abs(h[i] - h[i - j]) + dp[i - j]
        if m < m1:
            m1 = m
        j += 1
    dp[i] = m1

print(dp[n - 1])