peterverebics/CodeNet_Python_118 huggingface dataset:
import sys

stdin = sys.stdin

mod = 1000000007
inf = 1 << 60

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, k = na()
h = na()

dp = [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(h[i] - h[i + j]))

print(dp[n - 1])
from sys import stdin
n,k = map(int,stdin.readline().split())
h = list(map(int,stdin.readline().split()))
dp = [0]
for i in range(1,n):
    x = 10**10
    for j in range(max(0,i-k),i):
        x = min(x,dp[j]+abs(h[j]-h[i]))
    dp.append(x)
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(1,n):
    for l in range(1,k+1):
        if i-l < 0:continue
        dp[i] = min(dp[i],dp[i-l] + abs(h[i-l]-h[i]))

print(dp[n-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[n-1])
N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [10**10] * 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]))
print(dp[-1])
n, k, *L = map(int, open(0).read().split())

# dp = [float("inf")] * n
dp = [0] * n

for i in range(1, n):
    j = max(0, i - k)
    e = L[i]
    dp[i] = min([DP + abs(e - COST) for DP, COST in zip(dp[j:i], L[j:i])])

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

    dp = [0] * n
    for i in range(1, n):
        h = a[i]
        cost = 10**9
        for j in range(max(0, i-k), i):
            if dp[j] + abs(h - a[j]) < cost:
                cost = dp[j] + abs(h - a[j])
        dp[i] = cost

    print(dp[n-1])

main()
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(0,i-k),i):
    dp[i] = min(dp[i],dp[j]+abs(h[j]-h[i]))
print(dp[-1])
N, K = map(int, input().split())
H = list(map(int, input().split()))
INF = 10**9

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[-1])
n, k, *h = map(int, open(0).read().split())
dp = [0] * n
for i in range(1, n):
  dp[i] = min(dp[i-a] + abs(h[i] - h[i-a]) for a in range(1, min(i, k) + 1))
print(dp[n - 1])
n,k = list(map(int,input().split()))
arr = list(map(int,input().split()))
dp=[999999999]*n
dp[0]=0
for i in range(len(arr)):
    for j in range(i,i+k+1):
        if j<n:
            dp[j]=min(dp[j],dp[i] + abs(arr[j] - arr[i]))
    #print(dp)
print(dp[n-1])
def calc_dp():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

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

if __name__ == "__main__":
    calc_dp()
def main():
    n, k = map(int, input().split())
    h = [0] + list(map(int, input().split()))

    INF = 10 ** 12
    dp = [INF] * (n + 1)
    dp[1] = 0

    for i in range(1, 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()
import numpy as np
n, k = map(int, input().split())
arr = np.array([0] + list(map(int, input().split())) + [0] * 100)

INF = 1 << 60
dp = np.full(arr.shape[0] + 100, INF, dtype=int)

dp[1] = 0

for i in range(n):
    dp[i + 1: i + k + 1] = np.minimum(dp[i + 1: i + k + 1], dp[i] + np.abs(arr[i + 1: i + k + 1] - arr[i]))

print(dp[n])
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    DP = [10**10 for _ in range(N)]
    DP[0] = 0
    DP[1] = abs(h[0]-h[1])
    
    for i in range(2, N):
        DP[i] = min(DP[k]+abs(h[i]-h[k]) for k in range(max(0, i-K), i))    
    print(DP[N-1])
main()
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * n


for i in range(1,n):
    a = []
    if i-k < 0:
        start = 0
    else:
        start = i-k
    
    for j in range(start,i):
        a.append(dp[j] + abs(h[i] - h[j]))    
    
    dp[i] = min(a)
    
print(dp[-1])
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(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])
def main():
  num, K, *line = map(int, open(0).read().split())
  dp = [float("inf")]* num
  dp[0] = 0
  for i in range(1, num):
    min_j = max(0, i - K)
    dp[i] = min([dp[j] + abs(line[i] - line[j]) for j in range(min_j , i)])
  #kの範囲: k = 1,2,3...K 無理なときは k = 1,2..i
  print(dp[-1])
main()
n,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

dp = [float("inf")]*(n+k)
dp[0] = 0
for i in range(n):
    for j in range(i+1,i+k+1):
        dp[j] = min(dp[j],dp[i]+abs(h[i]-h[min(j,n-1)]))
print(dp[n-1])
INF = 10**9


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

    # 初期化
    dp = [INF] * N

    # 初期条件
    dp[0] = 0

    # DPループ
    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i - j - 1] + abs(h[i] - h[i - j - 1]))

    print(dp[-1])


if __name__ == "__main__":
    solve()
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [-1] * N
dp[0] = 0
 
for i in range(1, N):
    l = []
    m = min(K, i)
    for j in range(1, m+1):
        l.append(dp[i-j] + abs(h[i] - h[i-j]))
    dp[i] = min(l)
print(dp[-1])
N, K = map(int, input().split())
hs = list(map(int, input().split()))

costs = list(map(lambda h: abs(h - hs[0]), hs[:K]))
for i in range(K, N):
    costs.append( min( costs[i-j-1] + abs(hs[i-j-1] - hs[i]) for j in range(K) ) )
    
print(costs[-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(0, min(i, k)+1):
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))

print(dp[-1])
import sys
N, K = map(int, input().split())
h = [0] + list(map(int, input().split()))
dp = [0]*(N+1) # 足場iにたどり着くまでの最小コスト
for i in range(2,N+1):
  dp[i] = dp[i-1] + abs(h[i]-h[i-1])
  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])
import numpy as np 

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

dp = np.array([0] * n)

for i in range(1, n):
    j = max(0, i-k)
    dp[i] = np.min(dp[j:i] + np.abs(h[i] - h[j:i])) #ブロードキャストしてるんか

print(dp[-1])
import sys

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

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

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)
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**9
    if k>=i:
        x=i
    else:
        x=k
    for j in range(1,x+1):
        cost=min(cost,abs(l[i]-l[i-j])+l1[-j])
    l1.append(cost)
stdout.write(str(l1[-1])+'\n')
n,k = map(int, input().split())
h = [i for i in map(int, input().split())]

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

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


c_array = [0] * N
c_array[0] = 0

for i in range(1, N):
    c_min = 10 ** 10
    for k in range(1, K + 1):
        if i - k >= 0:
            c_min = min(c_min, c_array[i - k] +
                        abs(h_array[i - k] - h_array[i]))
    c_array[i] = c_min


print(c_array[-1])
N, K = map(int, input().split())
H = [int(x) for x in input().split()]
inf = float("inf")

dp = [inf] * (N)
dp[0] = 0
dp[1] = abs(H[0]-H[1])

for i in range(1, N):
    for k in range(1,K+1):   
        if i - k < 0:
            break
        else:
            dp[i] = min(dp[i], dp[i-k]+abs(H[i-k]-H[i]))
        
print(dp[N-1])
INF = 10**10

def chmax(a, b):
    if(a>b):
        return a
    else:
        return b

def chmin(a, b):
    if(a<b):
        return a
    else:
        return b


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

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

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


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

#print(dp)
print(dp[N-1])
N,K = map(int, input().split())
h = [int(x) for x in input().split()]
cost = [0] * N

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


for i in range(N - 1):
    min = cost[i] + 10000
    for j in range(K):
        if j < i + 1:
            c1 = cost[i - j] + abs((h[i + 1] - h[i - j]))
            if min > c1:
                min = c1
    cost[i + 1] = min

print(min)
# 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 k in range(1,K+1):
        if i+k<N:
            dp[i+k]=min(dp[i+k],dp[i]+abs(H[i+k]-H[i]))
    
    
print(dp[-1])
def main():
    N, K = map(int, input().split())
    h = [int(x) for x in input().split()]

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

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

if __name__ == "__main__":
    main()
n, k  = map(int, input().split())
H = list(map(int, input().split()))

memo = [10**9 for _ in range(n)]
memo[0] = 0
for i in range(1, n):
  for j in range(1, k+1):
    if i - j >= 0:
      memo[i] = min(memo[i], memo[i-j] + abs(H[i] - H[i-j]))
print(memo[-1])
def main():
    n, k = map(int, input().split())
    stones = tuple(map(int, input().split()))
    inf = 10**9
    dp = [inf]*n
    dp[0] = 0
    for i in range(n):
        for t in range(1, k+1):
            step = i+t
            if step < n:
                v = dp[i]+abs(stones[i]-stones[step])
                if dp[step] > v:
                    dp[step] = v
    print(dp[-1])


if __name__ == '__main__':    
    main()
n,k=map(int,input().split())
h=list(map(int,input().split()))
dp=[float('inf')]*n
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(2,n):
  for j in range(k):
    dp[i]=min(dp[max(0,i-j-1)]+abs(h[i]-h[max(0,i-j-1)]),dp[i])
print(dp[n-1])
N,K=map(int,input().split())

h=list(map(int,input().split()))
for i in range(101):
  h.append(0)
  
big=10**5+101
dp=[float('inf') for i in range(big)]

dp[0]=0

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

dp = [float('inf')] * n
dp[0] = 0
for i in range(1, n):
    from_jump = max(0,i-k)
    dp[i] = min(dp[j]+abs(h_l[i]-h_l[j]) for j in range(from_jump,i))
print(dp[n-1])
n,k=map(int,input().split())
H=list(map(int,input().split()))
C=[0]*n
for i in range(1,n):
  c=C[i-1]+abs(H[i]-H[i-1])
  for j in range(2,min(k,i)+1):
    d=C[i-j]+abs(H[i]-H[i-j])
    if d<c:
      c=d
  C[i]=c
print(C[-1])
def cost(n, k, nums):
    dp = [0] * n
    dp[0] = 0
    dp[1] = abs(nums[0] - nums[1])
    for i in range(2, n):
        dp[i] = min(abs(nums[i]-nums[j]) + dp[j] for j in range(max(0, i-k), i))
    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().strip().split())
hs = list(map(int, input().strip().split()))

dp = [0] * n

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

print(dp[n - 1])
n,k=map(int,input().split())
h=list(map(int, input().split()))
v=[0]*n
for i in range(1,n):
	u = h[i]
	v[i] = min([v[j] + abs(h[j]-u) for j in range(max(0, i-k), i)])
print(v[n-1])
N,K = map(int,input().split())
h = list(map(int,("0 "+input()).split()))
ans=[float('inf')]*(N+1)
ans[1]=0
for i in range(1,N+1):
 for j in range(i+1,min(i+K+1,N+1)):
  ans[j]=min(ans[j],ans[i]+abs(h[j]-h[i]))
print(ans[N])
def solve(h, k):
    dp = [0]
    for i in range(1, len(h)):
        best = float("inf")
        for j in range(1, min(i, k)+1):
            best = min(best, dp[i - j] + abs(h[i - j] - h[i]))
        dp.append(best)
    return dp[-1]


if __name__ == "__main__":
    n, k = map(int, input().split())
    h = [int(x) for x in input().split()]
    print(solve(h, k))
# numpy version

import numpy as np

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

dp = np.zeros(n)

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

print(int(dp[-1]))
n,k = map(int,input().split())
if k > n:
  k = n
a = list(map(int, input().split()))
dp = [0]*n
for i in range(1,n):
  if i <= k:
    dp[i] = abs(a[i]-a[0])
  else:
    dp[i] = min([abs(a[i]-a[i-j])+dp[i-j] for j in range(1,k+1)])
print(dp[-1])
import numpy as np
N,K=list(map(int,input().split()))
dp_list=np.full(N,10**10,dtype=int)
h=list(map(int,input().split()))
h=np.array(h)
dp_list[0]=0
for i in range(1,N):
        dp_list[i:i+K] = np.fmin(dp_list[i:i+K], dp_list[i-1] + np.abs(h[i:i+K] - h[i-1]))
print(dp_list[-1])
def a():
  n,k  = map(int, input().split())
  h = list(map(int, input().split()))
  dp = [0] * n
  for i in range(1, n):
    start = max(0, i - k)
    dp[i] = min([dp[j] + abs(h[j]-h[i]) for j in range(start, i)])  
  print(dp[n-1])
  
if __name__ == '__main__':
  a()
import sys;
import math;
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip()

n,k = get_ints();
arr = get_array();
dp = [10**18 for i in range(n)];
dp[0]=0;
dp[1]=abs(arr[1]-arr[0]);
for i in range(2,n):
    for j in range(1,k+1):
        dp[i]=min(dp[max(0,i-j)]+abs(arr[max(0,i-j)]-arr[i]),dp[i]);
#print(dp);
print(dp[n-1]);
import numpy as np

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

npmin = np.min
npabs = np.abs

k = min(k, n-1)
dp = np.zeros(n+1)

for i in range(2, k+1):
    dp[i] = npmin(dp[1:i] + npabs(h[i-1] - h[0:i-1]))

for i in range(k+1, n+1):
    dp[i] = npmin(dp[i-k:i] + npabs(h[i-1] - h[i-k-1:i-1]))
    
print(int(dp[n]))
N, K = map(int, input().split())

H = list(map(int, input().split())) + [0] * K

dp = [10**10] * (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[N-1])
import sys
n,k = map(int,input().split())

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

dp = [sys.maxsize]*n
dp[0] = 0
for i in range(n):
  for j in range(i+1,i+k+1):
    if j<n:
      dp[j]= min(dp[j],dp[i]+abs(a[i]-a[j]))
      
      
print(dp[n-1])
N,K = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
DP=[100000000000]*(N+1)
DP[0] = 0
DP[1] = 0
DP[2] = DP[1] + abs(h[0]-h[1])
for i in range(1,N):
    for j in range(i+1,min(i+K+1,N+1)):
        DP[j] = min(DP[j],DP[i]+abs(h[j-1]-h[i-1]))
print(DP[N])
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    H = list(map(int, input().split()))

    dp = [f_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:
                dp[i] = min(dp[i], dp[i - j] + abs(H[i] - H[i - j]))

    print(dp[-1])


if __name__ == '__main__':
    resolve()
n,k = map(int,input().split())
h = list(map(int,input().split()))
dp = [100000000000 for _ in range(n)]
dp[0] = 0
for i in range(n):
    for j in range(min(i+1,k+1)):
        dp[i] = min(dp[i-j]+abs(h[i]-h[i-j]),dp[i])
print(dp[-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))

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

for i in range(1, n):
    temp = []
    limit = max(i-k, 0)
    for j in range(limit, i):
        temp.append(dp[j] + abs(h[i] - h[j]))

    dp[i] = min(temp)

print(dp[-1])
n, k = map(int,input().split())
h = list(map(int,input().split()))
dp = [float('inf')]*n
dp[0] = 0
dp[1] = abs(h[0]-h[1])
if n ==2: print(dp[1]);exit()
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())
lis = list(map(int,input().split()))
dp = [10 ** 10 for i in range(n+k)]
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(lis[i+j]-lis[i]))
print(dp[n-1])
N, K = map(int, input().split())
High = list(map(int, input().split()))

inf = float("inf")
DP = [inf]*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])
def main():
  import sys
  input = sys.stdin.readline
  n,k=map(int,input().split())
  h=tuple(map(int,input().split()))
  dp=[0]*(n+1)
  for i in range(2,n+1):
    a=min(i-1,k)+1
    m=float('inf')
    for j in range(1,a):
      x=dp[i-j]+abs(h[i-1]-h[i-j-1])
      m=x if m>=x else m
    dp[i]+=m
  print(dp[-1])
if __name__=='__main__':
  main()
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(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=[int(i) for i in input().split()]
h.insert(0,0)
ans=[0]*(n+1)
ans[1]=0
ans[2]=abs(h[2]-h[1])
for i in range(3,n+1):
    m=1000000000000000
    boo=False
    for j in range(i-1,i-k-1,-1):
        if j>=1:
            m=min(m,ans[j]+abs(h[i]-h[j]))
            boo=True
        else:
            break
    ans[i]=m
print(ans[n])
n, k = map(int, input().split())
h = [int(i) for i in input().split()]

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

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

print(dp[-1])
N, K = map(int, input().split())
A = list(map(int, input().split()))
D = [0 for i in range(N)]
for i in range(1, N):
  D[i] = D[i-1] + abs(A[i-1] - A[i])
  for j in range(2, K + 1):
    if i - j < 0:
      break
    D[i] = min(D[i], D[i-j] + abs(A[i-j] - A[i]))
print(D[-1])
N, K = map(int, input().split())
h = list(map(int, input().split()))
INF = 10**9
dp = [INF] * N
dp[0] = 0
for i in range(N):
    for k in range(1, K+1):
        if i + k < N:
            dp[i+k] = min(dp[i+k], dp[i]+abs(h[i] - h[i+k]))
        else:
            break
print(dp[N-1])
def solve():
    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
            dp[i] = min(dp[i], dp[i-j] + abs(h[i-j] - h[i]))
    
    print(dp[-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, dtype=np.int64)

dp=np.full(N+K, 1e+14, dtype=np.int64)
dp[0]=0
for i in range(1, N):
  dp[i:i + K]=np.minimum(dp[i:i + K], np.abs(H[i:i + K]-H[i-1])+dp[i-1])

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

if __name__ == '__main__':
    main()
S=lambda:input()
I=lambda:int(input())
L=lambda:list(map(int,input().split()))

def main():
    n,k=L()
    h=L()
    dp=[1e+12]*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[i]-h[i+j]))
    print(dp[n-1])
    
    
if __name__ == '__main__':
    main()
N,K= map(int,input().split())
h = list(map(int,input().split()))
dp =[0]*N
dp[0] = 0
dp[1] = h[0]
for j in range(1,N):
  dp[j] = dp[j-1] + abs(h[j] - h[j-1])
  for i in range(max(j-K,0),j):
    dp[j]=min(dp[i]+ abs(h[j]-h[i]),dp[j]) 
print(dp[-1])
def main():
    N,K=map(int,input().split())
    h=list(map(int,input().split()))
    dp=[10**10]*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],dp[i]+abs(h[i]-h[i+j]))
    print(dp[N-1])

if __name__=="__main__":
    main()
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [10**9+7] * n
dp[0] = 0
dp[1] = abs(h[1]-h[0])
for i in range(2,n):
	for j in range(k):
		j += 1
		dp[i] = min(dp[i],dp[i-j]+abs(h[i] - h[i-j]))
print (dp[n-1])
def resolve():
    N, K = map(int, input().split())
    H = list(map(int, input().split()))

    dp = [0] * N

    for i in range(1, N):
        s = max(0, i-K)
        dp[i] = min([c + abs(h - H[i]) for c, h in zip(dp[s:i], H[s:i])] )

    print(dp[N-1])

if __name__ == "__main__":
    resolve()
# dp_b.py
def chmin(h, k, i):
    if i == 0:
        return 0
    ans = 10**9
    for j in range(min(i, k)):
        _min = dp[i-j-1] + abs(h[i]-h[i-j-1])
        if ans > _min:
            ans = _min
    return ans


N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [0]*N
for i in range(N):
    dp[i] = chmin(H, K, i)
    # print(dp)
print(dp[N-1])
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  min_ = -1
  for i in range(2, n):
    dp[i] = min([(abs(arr[s] - arr[i]) + dp[s]) for s in range(max(0, i-k), i)])
  return dp[-1]
    

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [0]*N
dp[1] = abs(arr[1] - arr[0])
print(get_cost(arr, N, K, dp))
import itertools,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,K = LI()
h = LI()
dp = [float('INF')]*N
dp[0] = 0
for i,j in itertools.product(range(N),range(1,K+1)):
    if i+j>=N:
        continue
    dp[i+j] = min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
print(dp[-1])
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [10**9+1]*n
    dp[0] = 0
    for i in range(1, n):
        buf = [dp[j] + abs(h[j]-h[i]) for j in range(max(i-k,0), i)]
        dp[i] = min(buf)
    print(dp[n-1])

if __name__ == "__main__":
    main()
[n, k] = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
dp = [10 ** 10 for _ in range(n)]
dp[0] = 0
dp[1] = abs(h[1] - h[0])
for i in range(2, n):
    for j in range(1, min(i, 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())
H = list(map(int, input().split()))
for _ in range(K+2):
    H.append(10**9)
dp = [10**9] * (N+K+1)
dp[0] = 0

for i in range(N):
    for j in range(1, K+1):
        dp[i+j] = min(dp[i] + abs(H[i]-H[i+j]), dp[i+j])
print(dp[N-1])
import numpy
N, K = map(int,input().split())
h = numpy.array(input().split(),dtype=numpy.int64)
dp = numpy.array([0]*N,dtype = numpy.int64)
for i in range(1,N) :
    s = max(0,i-K)
    c = dp[s:i] + numpy.abs(h[i]-h[s:i])
    dp[i] = numpy.min(c)
print(dp[-1])