peterverebics/CodeNet_Python_118 huggingface dataset:
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
dp = [float("inf")] * N
dp[0] = 0
 
for i in range(1, N):
    for j in range(1, K+1):
        if i - j < 0:
            break
        else:
            step = abs(h[i] - h[i-j])
            dp[i] = min(dp[i], dp[i-j]+step)
 
print(dp[N-1])
import numpy as np

def resolve():
    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):
        start = max(0, i - k)
        dp[i] = min(dp[start:i] + np.abs(h[i] - h[start:i]))

    print(dp[n - 1])

resolve()
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
inf = 10**9+7

n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [inf for i in range(n+1)]
dp[1] = 0
for i in range(1,n):
    for j in range(k):
        if i-j-1 >= 0:
            dp[i+1] = min(dp[i+1], dp[i-j]+abs(h[i]-h[i-j-1]))
        else:
            break
print(dp[n])
def main():
    N,K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [0]*N
    for i in range(1,N):
        dp[i]= min(abs(h[i]-h[j])+dp[j] for j in range(max(0,i-K),i))
    print(dp[-1])
main()
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
S = [0] * N

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

print(S[N-1])
def solve():
    N, K, *h = map(int, open(0).read().split())
    h += [h[-1]] * K
    
    dp = [10 ** 10] * (N + K)
    dp[0] = 0
    for i in range(N - 1):
        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 - 1])


if __name__ == '__main__':
    solve()
import numpy as np
N, K = map(int, input().split())
H = np.array(list(map(int, input().split())) + [0] * (K + 2), np.int32)
INF = 10 ** 9
dp = np.array([INF] * (N + K + 2), np.int32)
dp[0] = 0

for i in range(N):
    dp[i + 1:i + K + 1] = np.minimum(dp[i + 1:i + K + 1],
                                     dp[i] + np.abs(H[i + 1:i + K + 1] - H[i]))
print(dp[N - 1])
import numpy as np

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

dp = np.zeros(n)
dp[0] = 0
dp[1] = abs(h[1]-h[0])

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

print(int(dp[n-1]))
def chmin(a, b):
  if (a > b):
    return b

  return a

def chmax(a, b):
  if (a < b):
    a = b
    return True

  return False

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

dp = [10**9] * N

for n in range(N):
  if n == 0:
    dp[n] = 0
    continue
  if n == 1:
    dp[n] = abs(li[n] - li[n-1])
    continue

  for k in range(1, K+1):
    if k > n:
      break

    dp[n] = chmin(dp[n], dp[n-k] + abs(li[n] - li[n-k]))

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


print(main())
import sys
input = sys.stdin.buffer.readline

#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n, k = map(int, input().split())
h = list(map(int, input().split()))
dp=[0]+[1000000000]*(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(h[i]-h[i-j]))

print(dp[-1])
N,K=map(int, input().split())
h=list(map(int, input().split()))
h=[0]+h
dp=[10*10]*(N+1)
dp[1]=0
for n in range(2,N+1):
  m=[]
  ma=K+1 if n>K else n
  for i in range(1,ma):
    m.append(dp[n-i]+abs(h[n-i]-h[n]))
  dp[n]=min(m)
print(dp[N])
N, K = map(int, input().split())
h = list(map(int, input().split()))

dp = [10**9]*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], 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=list(map(int,input().split()))

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

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

print(dp[-1])
def main():
  N,K=map(int,input().split())
  h=list(map(int,input().split()))
  DP=[0]*N
  for i in range(1,N):
    min_k=float('inf')
    min_k=min([DP[j]+abs(h[i]-h[j]) for j in range(max(i-K,0),i)])
    DP[i]=min_k
    pass
  print(DP[-1])

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

N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()] + [0]*100

dp = [INF] * 100200
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())) + [0]*(N+K)

dp = [10**100] * (N + K + 100)
dp[0] = 0
for i in range(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-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [0] * (n + 1)
dp[0] = 0
dp[1] = abs(h[1] - h[0])
for i in range(2, n):
    l = []
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0, i- k), i))
print(dp[n - 1])
def resolve():
    import sys
    import numpy as np

    read = sys.stdin.read
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = np.array(read().split(), np.int32)

    # 貰うDP
    inf = 10 ** 9
    dp = np.full(N, inf, np.int64)
    dp[0] = 0


    for i, e in enumerate(h[1:], 1):
        lb = max(0, i - K)
        dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - e))

    print(dp[-1])

    
resolve()
import numpy as np
from numba import njit

n, k = map(int, input().split())
h = np.array(list(map(int, input().split())), np.int64)
INF = 10 ** 18


@njit
def f():
    dp = np.full(n, INF, np.int64)
    dp[0] = 0

    for i, e in enumerate(h[1:], 1):
        lb = np.maximum(0, i - k)
        dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - e))

    ans = dp[-1]
    return ans


ans = f()
print(ans)
#2問目
N, K = map(int, input().split())
h = list(map(int, input().split()))
dp = [float("inf")] * N
dp[0] = 0;
for i in range(1, N):
    for j in range(max(i-K, 0), i):
        dp[i] = min(dp[i], abs(h[j] - h[i]) + dp[j])
print(dp[N-1])
def main():
    INF = 10**20
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [INF] * N
    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] + abs(h[i]-h[i-j]))
            else:
                continue
    print(dp[-1])


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

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

dp = [10**9]*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())
h=list(map(int,input().split()))
dp=[float('inf') for i in range(N)]
dp[0]=0
if(N<K):
  K=N-1
for i in range(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[N-1])
N,K = map(int,input().split())
A = list(map(int, input().split()))
dp = [0 for _ in range(len(A))]
dp[0] = 0 
for i in range(1,N):
    tmp = [float("inf")]
    for j in range(1,K+1):
        if i - j>=0:
            tmp.append(dp[i-j] + abs(A[i] - A[i-j]))
    dp[i] = min(tmp)

print(dp[len(A)-1])
def main():
  #import sys
  #input = sys.stdin.readline
  N,K=map(int,input().split())
  h=list(map(int,input().split()))
  DP=[0]*N
  DP[0]=0
  for i in range(1,N):
  #j=1
  #while j<=K and j<=i:
  #  DP[i]=min(DP[i],abs(h[i]-h[i-j])+DP[i-j])
  #for j in range(max(0, i-K),i):
    #DP[i]=min(DP[i],abs(h[i]-h[j])+DP[j])
    r=max(0, i-K)
    DP[i]=min([abs(h[i]-h[j])+DP[j] for j in range(r,i)])
  print(DP[-1])
if __name__ == '__main__':
  main()
import sys
N,k=map(int,input().split())
l=[int(x) for x in input().split()]
l1=[sys.maxsize]*(N)
l1[0]=0
for i in range(N):
    for j in range(i+1,i+k+1):
        if j<N:
            l1[j]=min(l1[j],l1[i]+abs(l[j]-l[i]))
print(l1[N-1])
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
import numpy as np
n,k=mi()
h=np.array(li())
dp = np.array([0]*n)
for i in range(1,n):
    st = max(0,i-k)
    dp[i] = min(dp[st:i]+abs(h[i] - h[st:i]))
print(dp[n-1])
N, K = map(int, input().split())
h = list(map(int, input().split()))

INF = 10 ** 18
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])
import numpy as np

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

dp = np.zeros(N, dtype=int)
for i in range(1,N):
  s = max(0,i-K)
  dp[i] = np.min(dp[s:i] + np.abs(H[i]-H[s:i]))
  
print(dp[-1])
N, K  = map(int,input().split())
h_list = list(map(int,input().split()))

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

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

print(dp[N-1])
def main():
    infinity = 1e10

    n, k, height = getInputs()

    cost = [infinity] * n
    cost[0] = 0
    cost[1] = abs(height[0] - height[1])

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

    print(cost[-1])

def getInputs():
    n, k = map(lambda x: int(x), input().split())
    height = tuple(map(lambda x: int(x), input().split()))

    return n, k, height

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

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

dp[0] = 0

for i in range(1, N):
    for j in range(min(K, 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()))
x = float("inf")

dp = [x] * 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[n - 1])
import numpy as np

N, K = map(int, input().split())
lst = np.array([int(x) for x in input().split()])
dp = np.zeros(N, dtype=int)

for i in range(1, N):
    start = max(i - K, 0)
    dp[i] = np.min(dp[start:i] + np.abs(lst[start:i] - lst[i]))
print(dp[-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))

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

for i in range(n-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 = [0]*N

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

print(dp[N-1])
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

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

INF = 10**20
dp = [INF]*(N+10)
dp[0] = 0
def distance(x,y):
    return abs(h[x]-h[y])

for i in range(1,N):
    for k in range(1,K+1):
        if i-k<0:break
        dp[i] = min(dp[i], dp[i-k] + distance(i-k,i))
print(dp[N-1])
N, K = map(int, input().split())
arr = list(map(int, input().split()))
dp = [float('inf')] * N
dp[0] = 0
for i in range(0,N):
    k = 1
    while (i + k < N) and (k <= K):
        dp[i+k] = min(abs(arr[i]-arr[i+k])+dp[i],dp[i+k])
        k += 1
print(dp[N-1])
N, K = map(int,input().split())
H = list(map(int,input().split()))

#sub problem: min cost from 0 to i
memo = []
for i, num in enumerate(H):
  if i == 0:
    memo.append(0)
  elif i == 1:
    memo.append(abs(num - H[i-1]))
  else:
    m = min(abs(num - H[i-j])+ memo[i-j] for j in range(1, K+1) if i - j >= 0)
    memo.append(m)
print(memo[-1])
def solve():
    INF = 10**12

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

    dp = [INF] * N
    dp[0] = 0
    for i, H in enumerate(Hs):
        dpi = dp[i]
        for j in range(i+1, min(N, i+K+1)):
            dp2 = dpi + abs(H-Hs[j])
            if dp2 < dp[j]:
                dp[j] = dp2

    print(dp[N-1])


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

DP=[10000000000]*N

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

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

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

for i in range(N):
    for k in range(1, K+1):
        dp[i+k] = min(dp[i+k], dp[i] + abs(h[i+k] - h[i]))
    # print(dp)
print(dp[N-1])
import numpy as np

n, k = map(int, input().split())
h = np.array(list(map(int, input().split())), np.int64)
INF = 10 ** 18

dp = np.full(n, INF, np.int64)
dp[0] = 0

for i, e in enumerate(h[1:], 1):
    lb = max(0, i - k)
    dp[i] = np.min(dp[lb:i] + np.abs(h[lb:i] - e))

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

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

INF = 1e12

dp = [INF] * n

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

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


print(dp[n-1])
N, K = map(int, input().split())
h_list = list(map(int, input().split()))
dp = [float('INF') for i in range(N)]
dp[0] = 0
 
def update_dp(now, next):
  global h_list
  global dp
  global N
  if next > N - 1: return
  if dp[next] > dp[now] + abs(h_list[next] - h_list[now]):
    dp[next] = dp[now] + abs(h_list[next] - h_list[now])
  return
 
for i in range(N):
  for j in range(1, K + 1):
    update_dp(i, i + j)
 
print(dp[N - 1])
# coding: utf-8
# Your code here!
N,K=map(int,input().split())

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

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

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

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

dp = [float('inf')] * n
dp[0]= 0

for i in range(n):
    for j in range(k+1):
        if i + j < n:
            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 = [float('inf')]*(N+1)
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(int(dp[N]))
# print(dp)
N, K = map(int, input().split())
H = list(map(int, input().split()))

dp = [0] * N
for i in range(1, N):
    tmp = []
    for j in range(K):
        if (i - j - 1) < 0:
            break
        tmp.append(dp[i-j-1] + abs(H[i]-H[i-j-1]))
    dp[i] = min(tmp)
    # print("tmp", tmp)
    # print("dp ", dp)
print(dp[-1])
import numpy as np

n, k = map(int, input().split())
h = np.array(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):
    ik = max(0, i-k)
    dp[i] = np.min(dp[ik:i]+np.abs(h[i]-h[ik:i]))

print(int(dp[n-1]))
n,k=map(int,input().split())
h=[int(i) for i in input().split()]
k=min(k,n-1)
dp=[0]

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

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

print(dp[-1])
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):
    ddp=dp[i-1]+abs(h[i]-h[i-1])
    for j in range(1,K+1):
        if i-j<0:
            continue
        elif ddp>=dp[i-j]+abs(h[i]-h[i-j]):
            ddp=dp[i-j]+abs(h[i]-h[i-j])
    dp[i]=ddp

print(dp[-1])
# 配るDPで解く
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [float('inf')]*N

dp[0] = 0
# 配っていく
for i in range(N):
    for j in range(i+1, min(i+1+K, N)):
        dp[j] = min(dp[j], dp[i]+abs(H[j]-H[i]))
print(dp[N-1])
N,K = map(int,input().split())
import numpy as np
H = np.array(input().split(), dtype = int)

INF = 10 ** 9 + 1
dp = np.full(N,INF,dtype = int)
dp[0] = 0

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

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

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

# dp = np.array([-1 for _ in range(N)])
dp = np.zeros(N, dtype=int)

dp[0] = 0

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

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

def chmin(a, b):
    if a > b:
        return b
    else:
        return a
    
for i in range(n-1):
    if i == 0:
        dp[i+1] = abs(h[i+1] - h[i])
    else:
        for j in range(k):
            dp[i+1] = chmin(dp[i+1], dp[i-j] + abs(h[i+1] - h[i-j]))
print(dp[n-1])
# B - Frog2
import numpy as np

N, K = list(map(int, input().split()))
h = np.array(list(map(int, input().split()))+[0]*K)

dp = np.array([10**9]*(N+K))
dp[0] = 0

for i in range(N):
    dp[i+1:i+K+1] = np.minimum(dp[i+1:i+K+1], dp[i]+abs(h[i+1:i+K+1]-h[i]))
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
dp[0] = 0
for i in range(1, n):
    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])
import numpy as np

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

dp = [0] * n

for i, val in enumerate(h):
    if i > 0:
        p = max(i-k, 0)
        dp[i] = min(j + abs(l - val) for j, l in zip(dp[p:i], h[p:i]))

print(dp[-1])
import sys
def main():
    INF=sys.maxsize
    N,K=tuple(map(int,sys.stdin.readline().split()))
    h=tuple(map(int,sys.stdin.readline().split()))
    dp=[INF for _ in range(N)]
    dp[0]=0
    for i in range(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+j]-h[i])])
    print(dp[N-1])
if __name__=='__main__':main()
##Educational-DP B
n,k = map(int,input().split())
h = tuple(map(int,input().split()))

dp = [float('inf')] * n
dp[0] = 0

for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i] = min(dp[i],dp[j]+abs(h[i]-h[j]))
        
#print(dp)
print(dp[-1])
import numpy as np
n,k = map(int,input().split())
h = np.array(list(map(int, input().split())))
dp = np.array([np.inf] * n)
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(2,n):
    if i-k > 0:
        dp[i] = min(dp[i-k:i]+abs(h[i]-h[i-k:i]))
    else:
        dp[i] = min(dp[0:i]+abs(h[i]-h[0:i]))
print(int(dp[n-1]))
n, k = map(int, input().split())
h = [int(i) for i in input().split()]
dp = [0]
for i in range(1, n):
    to = min(i, k) + 1
    dp += [min([dp[-j] + abs(h[i] - h[i - j]) for j in range(1, to)])]
print(dp[-1])
(N,K) = map(int,input().split())
h = list(map(int,input().split()))
n = 2
cost = [0]*N
cost[1] = abs(h[1]-h[0])
for n in range(2,N):
    costmin = 10**10
    for i in range(max(0,n-K),n):
        costmin = min(costmin,cost[i]+abs(h[n]-h[i]))
    cost[n] = costmin
    
print(cost[N-1])
inf=float("inf")
n,k = map(int, input().split())
h = list(map(int, input().split()))
dp = [inf]*n
dp[0] = 0
for i in range(n-1):
    for j in range(1, min(k + 1, n - i)):
        dp[i+j] = min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
print(dp[n-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))
Cost=[1e30 for i in range(N)]
Cost[0]=0
Cost[1]=abs(H[1]-H[0])
for i in range(2,N):
    for k in range(1,min(i,K)+1):
        Cost[i]=min(Cost[i], Cost[i-k] + abs(H[i]-H[i-k]))
print(Cost[N-1])
import numpy as np

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

dp = np.array([np.inf] * N)
dp[0] = 0
dp[1] = abs(h[1]-h[0])
for i in range(2,N):
        if i - K >= 0:
            dp[i] = min(dp[i-K:i]+abs(h[i]-h[i-K:i]))
        else:
            dp[i] = min(dp[0:i]+abs(h[i]-h[0:i]))
print(int(dp[N-1]))
def main():
    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:
                dp[i+j] = min(dp[i+j], dp[i]+abs(h[i+j]-h[i]))
    print(dp[-1])


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

mm = list(map(int, input().strip().split()))

dp = [float('inf')] * 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(mm[i+j] - mm[i]))

print(dp[n-1])
n,k=map(int,input().split())
h=list(map(int,input().split()))
dp=[float('inf') for i in range(n)]
dp[0]=0
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
		else:
			dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
print(dp[n-1])
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

def main():
	n,k=map(int,input().split())
	h=list(map(int,input().split()))
	dp=[10**10]*(n+200)
	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]+abs(h[i+j]-h[i]))
	print(dp[n-1])

	
if __name__ == "__main__":
	main()
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print(DP[-1])
N,K = map(int, input().split())
H = list(map(int, input().split()))
L = [0]*N
for i in range (1,N):
  L[i] = min(L[i-j] + abs(H[i] - H[i-j]) for j in range(1, min(i,K)+1))
print(L[N-1])
#Frog2
n,k=map(int,input().split())
h=list(map(int,input().split()))
h.insert(0,0)
dp={1.0:0}
for val3 in range(2,n+1):
    dp[val3]=float(1e9)
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[int(val)]-h[int(val-val2)]))
print(dp[n])
# PyPy で提出しないと通らない
import sys
input = sys.stdin.readline

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

dp = [0] * n

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

print(dp[n - 1])
#B
n,k=map(int,input().split())
h_list=list(map(int,input().split()))
h1=abs(h_list[0]-h_list[1])
cst_list=[0,h1]
for i in range(2,n):
    x_list=[]
    for j in range(1,min(i+1,k+1)):
        x=cst_list[i-j]+abs(h_list[i]-h_list[i-j])
        x_list.append(x)
    cst_list.append(min(x_list))
print(cst_list[n-1])
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] = dp[i-1]+abs(h[i]-h[i-1])
    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])
def main():
    n, k, *h = map(int, open(0).read().split())
    dp = [float("inf")] * n
    dp[0] = 0
    for i in range(1, n):
        min_j = max(0, i-k)
        dp[i] = min([dp[j] + abs(h[i] - h[j]) for j in range(min_j, i)])
    print(dp[n-1])

main()