peterverebics/CodeNet_Python_118 huggingface dataset:
# ume 20/01/11
def chmin(a, b):
  if a>b:
    return b
  return a

def main():
  N, K = map(int, input().split())
  hs = list( map(int, input().split()) )
  dp = [1e4+10]*(N+10)
  dp[0] = 0
  for i in range(1, N):
    for j in range(K):
      if i-1-j < 0:
        continue
      dp[i] = chmin( dp[i], dp[i-1-j]+abs(hs[i]-hs[i-1-j]) )
  return dp[N-1]

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

dp = [1e7]*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:
            continue

print(dp[-1])
N, K = map(int, input().split())
h = tuple(map(int, input().split()))
dp = [float('inf')] * N
dp[0] = 0
def chmin(i, j):
  if dp[i] > j: dp[i] = j
def cost(i, j):
  return dp[i-j] + abs(h[i]-h[i-j])
for i in range(N):
  for j in range(1, K+1):
    if i+j < N:
      chmin(i+j, cost(i, j))
print(dp[-1])
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*N
for i in range(1,N):
    dp[i]=10**4
    for j in range(1,K+1 if i >= K else i+1):
        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 = [0]*N
for i in range(N-K-1,-1,-1):
    dp[i] = min(abs(h[i] - h[i+k]) + dp[i+k] for k in range(1, K+1))
print(dp[0])
N,K=map(int,input().strip().split())
h=list(map(int,input().strip().split()))

inf=100000000

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

inf = n * (10**4)
c = [inf] * n

c[0] = 0
c[1] = abs(h[1]-h[0]) + c[0]

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

print(c[n-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))
cost =10000
DP=[0]*(N+10)
DP[0]=0
DP[1]=abs(H[1]-H[0])
for i in range(2,N):
  cost=10010
  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

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):
    dp[i] = min(dp[max(0,i-K):i]+abs(dp[max(0,i-K):i]-h[i]))

print(dp[-1])
def solve(a,k,dp):
	for i in range(n-3,-1,-1):
		counter = 0
		if abs(i-len(a)) > k+1:
			counter = k+1
		else:
			counter = abs(i-len(a))
			# print(counter)

		for j in range(0,counter):
			# print(i,j)
			dp[i] = min(dp[i], dp[i+j] + abs(a[i]-a[i+j]))

	


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

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

solve(a,k,dp)


# print(dp)
print(dp[0])
n,k = map(int,input().strip().split(" "))
arr = list(map(int,input().strip().split(" ")))

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

for i in range(min(n,k),n):
    mn = 100000
    for j in range(i-1,i-k-1,-1):
        p = abs(arr[i]-arr[j])
        mn = min(mn,p+dp[j])
    dp[i] = mn
print(dp[n-1])
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [10**5+1 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[-1])
n,k=map(int,input().split())
# k=int(input())
h=list(map(int,input().split()))

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

dp=[1000001 for i in range(n)]
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(1,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])
N,K = map(int,input().split())
H = list(map(int,input().split()))

dp = [1000]*N
for i in range(N):
  if i == 0:
    dp[i] = 0
  elif i ==1:
    dp[i] = abs(H[i]-H[i-1])
  else:
    for j in range(K):
      if dp[i]>dp[i-j-1]+abs(H[i]-H[i-j-1]):
        dp[i] = dp[i-j-1]+abs(H[i]-H[i-j-1])
print(dp[-1])
def solve(a,k,dp):
	for i in range(n-3,-1,-1):
		counter = 0
		if i+k+1 > len(a):
			counter = len(a)-i
		else:
			counter = k+1

		# print(counter)

		for j in range(0,counter):
			# print(i,j)
			dp[i] = min(dp[i], dp[i+j] + abs(a[i]-a[i+j]))

	


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

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

solve(a,k,dp)


# print(dp)
print(dp[0])
n,k=map(int,input().split())
h=list(map(int,input().split()))
s=[0,abs(h[1]-h[0])]
total=10**5
for i in range(2,n):
    for j in range(1,k+1):
        if i-j<0:
            break
        else:
            total1=abs(h[i]-h[i-j])+s[i-j]
            if total>total1:
                total=total1
    s.append(total)
    total=10**5
print(s[-1])
n, k=map(int,input().split())
h=list(map(int,input().split()))

dp=[0]*n

dp[1]=dp[0]+abs(h[1]-h[0])
#足場iまでいく
for i in range(2,n):
    mn=10000
    for j in range(max(0,i-k),i):
        DP=dp[j]+abs(h[i]-h[j])
        if DP<mn:
            mn=DP
    dp[i]=mn

print(dp[n-1])
def main():
    N,K = map(int, input().split())
    h = list(map(int, input().split()))
    cost = [10**8]*N
    cost[0] = 0

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

    return

if __name__ == "__main__":
    main()
def cost(n, k, nums):
    dp = [0 for _ in range(n)]
    dp[0] = 0
    dp[1] = abs(nums[1] - nums[0])
    for i in range(2, n):
        mincost = float('inf')
        for j in range(1, k+1):
            if i-k >= 0:
                mincost = min(mincost, dp[i-k] + abs(nums[i] - nums[i-k]))
        dp[i] = mincost
    return dp[-1]

n, k = [int(i) for i in input().split()]
nums = [int(i) for i in input().split()]
print(cost(n, k, nums))
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[0]*n
dp[0]=0
for i in range(1,n):
    s=min(k,i)+1
    ans=999999
    for j in range(1,s):
        ans=min(ans,dp[i-j]+abs(l[i]-l[i-j]))
    dp[i]=ans
print(dp[n-1])
N,K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
dp = [99999]*(N+1)
dp[1] = 0
dp[2] = abs(h[0] - h[1])

for i in range(3,N+1):
  Kl = max(1,i-K)
  fr = [dp[j]+abs(h[j-1] - h[i-1]) for j in range(Kl,i+1)]
  dp[i] = min(fr)
print(dp[N])
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+1,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])
import numpy as np

# change minimum
def chmin(a,b):
    return b if a>b else a

INF = 1000000000

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

h.extend([0]*10**5)

# DPテーブル
dp = [INF]*(10**5+10)

dp = np.array(dp)
h = np.array(dp)

# 初期条件
dp[0] = 0

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

print(dp[N-1])
import sys
n, k = map(int, input().split())
H = list(map(int, input().split()))
C = [10**5] * n
C[0] = 0

if n == 2:
  print(abs(H[0] - H[1]))
  sys.exit()

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

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

h = [int(x) for x in input().split()]

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

for i in range(1,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())
dp=[0]*(n+1)
h=list(map(int,input().split()))
tmp_lst=[]
dp[1]=abs(h[1]-h[0])
for i in range(2,n):
    for j in range(1,k+1):
        if i-j<0:
            break
        tmp=dp[i-j]+abs(h[i]-h[i-j])
        tmp_lst.append(tmp)
    dp[i]=min(tmp_lst)
print(dp[n-1])
n,k=map(int,input().split())
h=[int(l) for l in input().split()]
dp=[0,abs(h[0]-h[1])]
for i in range(2,n):
    ans=10**5
    num=i-k
    if num<0:
        num=-1
    else:
        num-=1
    for j in range(i-1,num,-1):
        ans=min(abs(h[i]-h[j])+dp[j],ans)
    dp.append(ans)
print(dp[n-1])
n,k = list(map(int,input().split()))
arr = list(map(int,input().split()))

dp = [0 for i in range(n)]
j = 2
dp[0] = 0
dp[1] = abs(arr[1]-arr[0])
for i in range(2,len(arr)):
    dp[i] =10000000
    for q in range(1,min(i+1,k+1)):
        op1 = dp[i-q]+abs(arr[i]-arr[i-q])
        dp[i] = min(dp[i],op1)

print(dp[-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+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])
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+1, k+1)):
        dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
print(dp[n-1])
import sys

input = sys.stdin.readline
n, k = map(int, input().split(" "))
h = [int(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):
        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=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[N-1])
n,k = map(int,input().split())
h = list(map(int,input().split()))
dp = [0] * n
t = []

for i in range(2,n):
    for l in range(1,k+1):
        if not i - l < 0:
            t.append(abs(h[i] - h[i-l]) + dp[i-l])
        else:
            break

    dp[i] = min(t)
    t.clear()

print(dp[-1])
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,k=nm()
h=nl()

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

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

print(dp[n-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))
INF=10**7
add_list=[INF]*K

H.extend(add_list)

dp=[INF]*(N+100) #Kの分追加
dp[0]=0
for n in range(N):
    for k in range(1,K+1):
        dp[n+k]=min(dp[n+k], dp[n]+abs(H[n]-H[n+k]))
print(dp[N-1])
n,k=map(int,input().split())
h=list(map(int,input().split()))
cost=[100000]*n
cost[0]=0
for i in range(n-1):
    for j in range(1,min(k+1,n-i)):
        cost[i+j]=min(cost[i+j],cost[i]+abs(h[i+j]-h[i]))

print(cost[-1])
INF = 110000
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 < n:
            dp[i+j] = min(dp[i+j], abs(h[i+j] - h[i]) + dp[i])
        else: break

print(dp[n-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])

if K==N:
    print(min(dp))
    exit()

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])
size, k = map(int, input().split())
arr = list(map(int, input().split()))
max = 10000000
dp = [max]*(size)
for i in range(size):
    if i > k:
        for x in range(1, k +1):
            dp[i] = min(dp[i], dp[i-x] + abs(arr[i-x]-arr[i]))
    #dp[i] = min(dp[i-1] + abs(arr[i-1]-arr[i]), dp[i-2] + abs(arr[i-2]- arr[i]))
    else:
        dp[i] = abs(arr[0] - arr[i])

print(dp[size-1])
import sys

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


n,k=map(int,input().split())
cst=list(map(int,input().split()))
arr=[0]*n
arr[1]=abs(cst[1]-cst[0])
for i in range(2, n):
    arr[i]=min(abs(cst[j]-cst[i])+arr[j] for j in  range(max(i-k,0),i))
    print(arr[n-1])

#print(arr)
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [10 ** 5] * n
dp[0] = 0
for i in range(0, n):
    for j in range(1, k + 1):
        if j >= n:
            break
        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
dp[0] = 0
dp[1] = abs(h[1]-h[0])

for i in range(2,n):
    dp[i] = dp[i-1]+abs(h[i-1]-h[i])
    print("dp(i)={}".format(dp[i]))
    for j in range(2,k+1):
        if i-j>=0:
            dp[i] = min(dp[i],dp[i-j]+abs(h[i-j]-h[i]))
            print(dp)
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(1, n):
  temp = False
  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, *hs = [int(x) for x in open(0).read().split()]
dp = [0] * n
for i in range(1, n):
    v = 10000
    for j in range(max(0, i - k), i):
        v = min(v, dp[j] + abs(hs[i] - hs[j]))
    dp[i] = v
print(dp[n-1])
N,K = map(int,input().split())

H = [0]
F = [0]*(N+1)

*h, = map(int,input().split())
for i in h:
    H.append(i)

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


print(F[-1])
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

n,k=input2()
H=input_array()
INF=10**9
# 最小化問題
dp=[INF]*n
# 初期化
dp[0]=0
flag=True

for i in range(n):
	for j in range(1,k+1):
		if i+j>=n:
			flag=False
			break
		a=dp[i]+(abs(H[i+j]-H[i]))
		dp[i+j]=min(dp[i+j],a)
	if flag==False:
		break


print(dp[-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(N):
  for j in range(1, K+1):
    if i+j < N:
      m = abs(h[i+j] - h[i]) + dp[i]
      if dp[i+j] > m:
        dp[i+j] = m
      
print(dp)
import numpy as np


def main():
    N, K = list(map(int, input().split(' ')))
    H = np.array(list(map(int, input().split(' '))))
    dp = np.full(N, 10**10)
    dp[0] = 0
    for i in range(1, N):
        dp[i:(i+K)] = np.fmin(dp[i:(i+K)], dp[i-1] + np.abs(H[i:(i+K)] - H[i-1]))
    print(dp[N-1])


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

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

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

for i in range(1, n):
  left = max(0, i - k)
  min_c = 10**5
  for j in range(left, i):
    min_c = min(min_c, C[j] + abs(H[i] - H[j]))
    #print(i, j, min_c)
  C[i] = min_c
  
print(C[-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [10**9 for _ in range(n)]
dp[0] = 0
for i in range(n):
  for j in range(1, k+1):
    if i + j >= n:
      break
    dp[i+j] = min(dp[i+j], dp[i] + abs(h[j]-h[i]))
ans = dp[n-1]
print(ans)
##Educational-DP B
n,k = map(int,input().split())
h = tuple(map(int,input().split()))

dp = [10**4 + 1] * 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)
print(dp[-1])
N,K=map(int,input().split())
h=list(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[N-1])
import sys
input = sys.stdin.readline
import numpy as np

N, K = [int(item) for item in input().split()]
h =np.array([int(item) for item in input().split()])

dp = np.full(N+1, 10**10)
dp[0] = 0
dp[1] = abs(h[1] - h[0])

for i in range(N):
    start = max(0, i-K)
    cost = np.abs(h[start:i] - h[i])
    np.minimum(dp[start:i] + cost, dp[i+1], out=dp[start+1:i+1])
    # print(start, i)

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


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

for i in range(N - K + 2):   
    check = float('inf')
    for j in range(1, K + 1):
        check = min(check, height_memo[i + K - 2 - j] + abs(H[i + K - 2] - H[i + K - 2 - j]))

    height_memo[i + K - 2] = min(check, height_memo[i + K - 2])

print(height_memo[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(k,0,-1):
        if j > len(h):
            continue
        con = min(con,dp[i-j]+abs(h[i]-h[i-j]))
    dp[i] = con
print(dp[n-1])
[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 < n:
            dp[i + j] = min(dp[i + j] , dp[i] + abs(h[i] - h[i+j]))
print(dp[n-1])
#B-Frog2
N,K=map(int,input().split())
h=list(map(int,input().split()))

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


for i in range(2,N):
    min_num=10000
    for j in range(max(0,i-K),i):
        print(dp[i-j]+abs(h[i]-h[j]),j)
        if min_num>dp[j]+abs(h[i]-h[j]):
            min_num=dp[j]+abs(h[i]-h[j])
    dp[i]=min_num

print(dp)
n, k = map(int, input().split())
s = list(map(int, input().split()))
inf = 10 ** 8
dp = [inf] * n
dp[0] = 0

for j in range(1, n):
    for i in range(max(0, j - k), j):
        dp[j] = min(dp[j], dp[i]+abs(s[j]-s[i]))
print(dp[-1])
from collections import deque
n, k = map(int, input().split())
h = deque(list(map(int, input().split())))
dp = deque([10000]*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[j] - h[i]))
print(dp[n-1])
from collections import deque
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
h = deque(list(map(int, input().split())))
dp = deque([10000]*100001)
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[j] - h[i]))
print(dp[n-1])
#B-Frog2
N,K=map(int,input().split())
h=list(map(int,input().split()))

dp=[0 for i in range(N)]
dp[1]=abs(h[1]-h[0])
print(dp)

for i in range(1,N):
    max_num=0
    for j in range(1,max(0,i-K)+2):
        print("j",j)
        if max_num<dp[j]+abs(h[i]-h[j]):
            max_num=dp[j]+abs(h[i]-h[j])
    dp[i]=max_num

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

dp = [0]

for i in range(1, N):
    m = 10 ** 4
    for j in range(1, min(i, K) + 1):
        m = min(m, dp[i - j] + abs(h[i] - h[i - j]))
    dp.append(m)
print(dp[-1])
n,k = map(int, input().split())
h = list(map(int, input().split()))
sol = [100000000 for i in range(n)]
sol[0] = 0
for i in range(1, n):
    for j in range(i-1, max(i-k-1, -1), -1):
        sol[i] = min(sol[i], sol[j] + abs(h[i]-h[j]))

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

dp = [10**4]*N

for i in range(N):
  if i == 0:
    dp[i] = min(dp[i], 0)
  else:
    for j in range(max(i-K, 0), i):
      dp[i] = min(dp[i], dp[j] + abs(H[i] - H[j]))

print(dp[-1])
import sys
sys.setrecursionlimit(10**9)
#dp=[-1]*(10**5+2)
def frog2(arr,N,K):
    if N==0:
        return 0
    cnt=10**8
    for i in range(N-1,max(N-K-1,-1),-1):
        cnt=min(cnt,frog2(arr,i,K)+abs(arr[N]-arr[i]))
    return cnt
if __name__ == "__main__":
    N,K=map(int,input().split())
    h=[int(i) for i in input().split()][:N]
    print(frog2(h,N-1,K))
    #print(dp[N-1])
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]+abs(h[i-p]-h[i]),dp[i])
        p+=1
stdout.write(str(dp[n-1])+"\n")
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(2,x):
    p=1000001
    #print(i-k,i)
    for j in range(max(0,i-k),i):
        #print(i,j)
        p=min(p,dp[j]+abs(a[i]-a[j]))
        
    dp[i]=p
print(dp[x-1],dp)
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

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

cost = [0] * N
for i in range(1, N):
  #print('i=', i)
  st = max(i - K, 0)
  ci = 10**6

  for j in range(st, i):
    cj = cost[j] + abs(H[i] - H[j])
    #print('cj=', cj)
    ci = min(cj, ci)

  #print('ci=', ci)
  cost[i] = ci

#print(cost)
ans = cost[N-1]
print(ans)
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[99999999]*n
dp[0]=0
for i in range(1,n):
    for j in range(1,k+1):
        if i-j>=0:
            dp[i]=min(abs(l[i]-l[i-j])+dp[i-j], dp[i])

print(dp[n-1])
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
c=0
f=[99999999]*(n)
f[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            f[j]=min(f[j],f[i]+abs(a[i]-a[j]))
print(f[n-1])
size, jumps = map(int, input().split())
array =  list(map(int, input().split()))
dp = [0]* size
dp[1]=  dp[0] + abs(array[1] - array[0]) 
for i in range(1,size):
	for j in range(i ,size):		
		dp[i] = min( 
		dp[j],abs(array[i] - array[j])
		
		)
print(dp[-1])
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [10**8+1 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,k = list(map(int, input().split()))
nums = list(map(int, input().split()))
best = [999999]*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 = map(int, input().split())
H = [0]+list(map(int, input().split()))
dp = [float("inf")]*(n+1)
dp[0] = 0
dp[1] = 0

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