CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input=sys.stdin.readline


n,k=list(map(int,input().split()))
cst=list(map(int,input().split()))
arr=[0]*(n+1)
arr[0]=0
arr[1]=abs(cst[1]-cst[0])
if n==2:
    print((arr[1]))
else:
    for i in range(2, n):
        #arr[i] = min(abs(cst[i - 1] - cst[i]) + arr[i - 1], abs(cst[i - 2] - cst[i]) + arr[i - 2])
        #t=cst.index(min(cst[max(i-k,0):i]))
        #arr[i]=min(abs(cst[max(i-j,0)]-cst[i])+arr[i-j] for j in range(1,k+1))
        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)

# Optimized solution
import sys

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

def main():
    n, k = list(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]))
if __name__ == '__main__':
    main()
# Original solution
N, K = list(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]))

# Optimized solution
n,k = list(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]))
# Original solution
def main():
    n, k = list(map(int, input().split()))
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(0, n):
        for j in range(1, k + 1):
            if i + j == len(heights):
                break
            dp[i + j] = min(dp[i + j], dp[i] + abs(heights[i] - heights[i + j]))

    print((dp[n - 1]))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min(dp[j] + abs(heights[i] - heights[j]) for j in range(max(i - k, 0), i))
        # for j in range(1, k + 1):
        #     if i - j < 0:
        #         break
        #     dp[i] = min(dp[i], dp[i - j] + abs(heights[i] - heights[i - j]))

    print((dp[n - 1]))


if __name__ == '__main__':
    main()
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
H = [H[0]]*K+H
def solve(H):
    dp = [0]*(N+K)
    for i in range(K,N+K):
        dp[i]= 10**10
        for j in range(1,K+1):
            dp[i] = min(dp[i],dp[i-j]+abs(H[i]-H[i-j]))
    ans = dp[-1]
    return ans
print((solve(H)))

# Optimized solution
def solve():
  N, K = list(map(int, input().split()))
  H = list(map(int, input().split()))
  dp = [float('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+k]-H[i]))
  ans = dp[N-1]
  return ans
print((solve()))
# Original solution
import sys
input = sys.stdin.readline

def frog_2():
    N, K = map(int,input().split())
    h = tuple(map(int,input().split()))

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

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

frog_2()


# Optimized solution
import sys
input = sys.stdin.readline

def frog_2():
    N, K = list(map(int,input().split()))
    H = tuple(map(int,input().split()))

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

    for n in range(1, N):
        k = max(0, n - K) # k: 'n' ループ目の最大ジャンプ幅 'K' 
        h = H[n]          # h: 'n' ループ目の足場の高さ'H'
        dp[n] = min(abs(h - H[i]) + dp[i] for i in range(k, n))
        
    print((dp[-1]))

frog_2()
# Original solution
import sys
input = sys.stdin.readline

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

dp = [0] + [float("inf")] * (n-1)
#print(dp)

for i in range(1,n):
    K = max(0, i - k) # K: 'i' ループ目の最大ジャンプ幅 'k' 
    H = h[i]          # H: 'i' ループ目の足場の高さ'h'
    for j in range(K, i):
        dp[i] = min(dp[i], dp[j]+abs(H - h[j]))
#print(dp)

print((dp[-1]))



# Optimized solution
# AC by Pypy3, not Python3

import sys
input = sys.stdin.readline

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

dp = [0] + [float("inf")] * (n-1)
#print(dp)

for i in range(1,n):
    K = max(0, i - k) # K: 'i' ループ目の最大ジャンプ幅 'k' 
    H = h[i]          # H: 'i' ループ目の足場の高さ'h'
    for j in range(K, i):
        dp[i] = min(dp[i], dp[j]+abs(H - h[j]))
#print(dp)

print((dp[-1]))
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = 10 ** 4 + 1
dp = [inf] * n
dp[0] = 0
for i in range(1,n):
    dp[i] = min([dp[j] + abs(h[j] - h[i]) for j in range(max(0, i - k), i)])
print((dp[-1]))


# Optimized solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = 10 ** 4 + 1
dp = [inf] * n
dp[0] = 0
for i in range(1,n):
    H = h[i]
    dp[i] = min([dp[j] + abs(h[j] - H) for j in range(max(0, i - k), i)])
print((dp[-1]))
# Original solution
import sys 
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = sys.maxsize

DP = [INF] * N

for i in range(N):
    for k in range(1, K+1):
        if DP[i] < INF:
            DP[i] = min(abs(H[i] - H[max(i - k, 0)]) + DP[max(i - k, 0)], DP[i])
        else:
            DP[i] = abs(H[i] - H[max(i - k, 0)]) + (DP[max(i - k, 0)] if DP[max(i - k, 0)]  != INF else 0)
        

print((DP[N-1]))


# Optimized solution
import sys 
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = sys.maxsize

DP = [INF] * N

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

print((DP[N-1]))
# Original solution
def main():
    N,K=list(map(int, input().split()))
    A = list(map(int, input().split()))

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1,len(A)):
        jmp = max(0,i-K)
        dp[i] = min([dp[j]+abs(A[i]-A[j]) for j in range(jmp, i)])
    print((dp[N-1]))

main()

# Optimized solution
N,K=list(map(int, input().split()))
A = list(map(int, input().split()))
 
INF = 10**9
dp = [INF]*N
 
for i in range(len(A)):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(A[1]-A[0])
    else:
        jmp = min(i,K)
        for j in range(1,jmp+1):
            dp[i] = min(dp[i], dp[i-j]+abs(A[i]-A[i-j]))
            
print((dp[N-1]))
# Original solution
N,K = list(map(int,input().split()))
H = [int(x) for x in input().split()]
dp = [0]*N
for i in range(1,N):
	L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
	dp[i] = min(L)
print((dp[-1]))

# Optimized solution
# -*- coding: utf-8 -*- 
def cost(h,N,K):
    dp=[0]*N
    dp[1]=abs(h[0]-h[1])
    for i in range(2,N):
        tmp=[abs(h[i]-h[j])+dp[j] for j in range(max(0,i-K),i)]
        dp[i]=min(tmp)
    return dp[-1]

N,K=list(map(int,input().split()))
h=[int(num) for num in input().split()]

print((cost(h,N,K)))
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))

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

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

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

print((dp[n-1]))

# Optimized solution
import sys

data = sys.stdin.readlines()

n, k = list(map(int, data[0].split()))
h = list(map(int, data[1].split()))

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

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

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

print((dp[n-1]))
# Original solution
import sys
def input():
    return sys.stdin.readline()[:-1]

N,K = list(map(int,input().split()))
H = list(map(int,input().split()))
inf = 10**9+7
dp = [inf] * (N+10)
dp[0] = 0
for i,h in enumerate(H):
    for j in range(1,K+1):
        if i+j < N:
            dp[i+j] = min(dp[i+j],dp[i] + abs(h-H[i+j]))

print((dp[N-1]))


# Optimized solution

import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

N,K = list(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]))
# Original solution
N,K = list(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):
  cost = []
  for j in range(1,min(K,i)+1):
    cost.append(dp[i-j] + abs(h[i]-h[i-j]))
  dp[i] = min(cost)
print((dp[-1]))

# Optimized solution
N,K = list(map(int,input().split()))
h = list(map(int,input().split()))
inf = float("inf")
dp = [0]*N
dp[0] = 0
dp[1] = abs(h[1]-h[0])

for i in range(2,N):
  cost = []
  for j in range(1,min(K,i)+1):
    cost.append(dp[i-j] + abs(h[i]-h[i-j]))
  dp[i] = min(cost)
print((dp[-1]))
# Original solution
num,jump=list(map(int,input().split()))
height=list(map(int,input().split()))

dp=[10**9]*(num)
dp[0]=0
# dp[1]=abs(height[1]-height[0])
# for i in range(2,num):
#   dp[i]=min(dp[i-1]+abs(height[i]-height[i-1]),dp[i-2]+abs(height[i]-height[i-2]))

for i in range(1,num):
    for j in range(1,min(i,jump)+1):
        dp[i]=min(dp[i],dp[i-j]+abs(height[i]-height[i-j]))

print((dp[-1]))

# Optimized solution
num,jump=list(map(int,input().split()))
height=list(map(int,input().split()))

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

for i in range(1,num):
    for j in range(1,min(i,jump)+1):
        dp[i]=min(dp[i],dp[i-j]+abs(height[i]-height[i-j]))

print((dp[-1]))
# Original solution
N,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
DP = [1000000000]*N
DP[0] = 0
def handan(i,x):
    if DP[i] > x:
        DP[i] = x
for i in range(1,N):
    for j in range(min(i,k)):
        x = DP[i-j-1] + abs(h[i]-h[i-j-1])
        handan(i,x)
print((DP[N-1]))

# Optimized solution
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]))
# Original solution
N,K = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
DP=[1000000000]*(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]))

# Optimized solution
N,K = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
DP = [10**10]*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]))
# Original solution
N, K = list(map(int, input().split()))
HHH = list(map(int, input().split()))

dp = [1000000000] * (N+K+2)
HHH.extend([0]*(K+2))

dp[0] = 0
"配るdpでやる？"

for i in range(0, N):
    for j in range(1, K+1):  # 1段~K段飛ぶ
        dp[i+j] = min(dp[i+j], dp[i]+abs(HHH[i+j]-HHH[i]))

print((dp[N-1]))


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


def frog(n, k, h):
    d = [float('inf')] * n
    d[0] = 0

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


print((frog(n, k, h)))
# Original solution
import heapq

N,K=list(map(int,input().split()))
h=list(map(int,input().split()))
dp=[0]*N
for i in range(N):
    he=[]
    if i == 0:
        dp[i] = 0
    else:
        for j in range(K):
            heapq.heappush(he, dp[i-(j+1)]+abs(h[i]-h[i-(j+1)]) if i-(j+1) >= 0 else float('inf'))
        dp[i] = heapq.heappop(he)
print((dp[N-1]))


# Optimized solution
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N,K=list(map(int,input().split()))
    h=list(map(int,input().split()))+[INF]*K
    dp=[INF]*(2*10**5)
    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[N-1]))

if __name__ == '__main__':
    main()
# Original solution
import sys
from functools import lru_cache
sys.setrecursionlimit(20000000)
N, K = list(map(int, input().split()))
heights = [int(h) for h in input().split()]


@lru_cache(None)
def dfs(i):
    if i == len(heights) - 1:
        return 0
    res = float("inf")
    for k in range(1, K + 1):
        if i + k < len(heights):
            res = min(res, abs(heights[i] - heights[i + k]) + dfs(i + k))
    return res


def helper():
    dp = [float("inf")] * len(heights)
    dp[0] = 0
    for i in range(1, len(heights)):
        dp[i] = min(dp[i],
                    min(dp[j] + abs(heights[j] - heights[i]) for j in range(max(i - K, 0), i))
                    )

    return dp[-1]


# print(dfs(0))
print((helper()))

# Optimized solution
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
 
def frog(n, k, h):
    d = [float('inf')] * n
    d[0] = 0
 
    for i in range(1, n):
        d[i] = min(
            d[j] + abs(h[i] - h[j]) for j in range(max(i - k, 0), i)
        )
    return d[n - 1]
 
 
print((frog(n, k, h)))
# Original solution
def main():
    # n, k = map(int, input().split())
    # stns = [int(x) for x in input().split()]
    n,k = list(map(int,input().split()))
    stns = list(map(int, input().split()))

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

    for i in range(n):
        for j in range(1, k + 1): # consider more than one step
            j = i + j # step i + 1, i + 2
            if j < n:
                # min(existing min, new path using i)
                dp[j] = min(dp[j], dp[i] + abs(stns[i] - stns[j]))
    print((dp[n-1]))

main

if __name__ == "__main__":
    main()

# Optimized solution
def main():
    n,k = list(map(int,input().split()))
    h = list(map(int,input().split()))
    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1,n):
        for j in range(1,min(i+1,k+1)):
            m = dp[i-j] + abs(h[i]-h[i-j])
            if dp[i]>m:
                dp[i] = m
    print((dp[n-1]))
    
 
if __name__ == "__main__":
    main()
# Original solution
import sys
def main():
    input=sys.stdin.readline
    N,K=list(map(int,input().split()))
    H=list(map(int,input().split()))
    if N-1<=K:
        print((H[N-1]-H[0]))
        return
    inf=10**9+7
    dp=[inf]*N
    dp[0]=0

    for i,h in enumerate(H):
        if i==0:continue
        m=inf
        M=max(0,i-K)
        for ht,dpt in zip(H[M:i],dp[M:i]):
            m=min(m, dpt+abs(h-ht))
        dp[i]=m
    print((dp[N-1]))
    return
if __name__=="__main__":
    main()

# Optimized solution
import sys
def main():
    input=sys.stdin.readline
    N,K=list(map(int,input().split()))
    H=list(map(int,input().split()))
    if N-1<=K:
        print((H[N-1]-H[0]))
        return
    inf=10**9+7
    dp=[inf]*N
    dp[0]=0

    for i,h in enumerate(H):
        if i==0:continue
        m=inf
        M=max(0,i-K)
        dp[i]=min([dpt+abs(h-ht) for ht, dpt in zip(H[M:i],dp[M:i])])
    print((dp[N-1]))
    return
if __name__=="__main__":
    main()
# Original solution
#b
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))

N=100010
INF=float('INF')
dp=[INF]*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[n-1]))

# Optimized solution
n,K=list(map(int,input().split()))
h=list(map(int,input().split()))
dp=[0]*n 

for i in range(1,n):
    dp[i]=10**10
    for k in range(1,min(K,i)+1):
        dp[i]=min(dp[i],dp[i-k]+abs(h[i]-h[i-k]))
print((dp[n-1]))
# Original solution
N, K = list(map(int, input().split()))
h_list = list(map(int, input().split()))
dp = [float('INF') for i in range(N)]
dp[0] = 0

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

for i in range(N):
  for j in range(1, K + 1):
    if i + j > N - 1: continue
    dp[i + j] = change_min(dp[i] + abs(h_list[i] - h_list[i + j]), dp[i + j])

print((dp[N - 1]))

# Optimized solution
N, K = list(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]))
# Original solution
if __name__=='__main__':
    n,k,*h=list(map(int,open(0).read().split()))
    dp=[0]*n
    for i in range(1,n):
        t=h[i]
        d=10**18
        for v,w in zip(dp[max(0,i-k):i],h[max(0,i-k):i]):
            d=min(d,v+abs(t-w))
        dp[i]=d
    print((dp[-1]))

# Optimized solution
if __name__=='__main__':
    n,k,*h=list(map(int,open(0).read().split()))
    dp=[0]
    for i,t in enumerate(h):
        if i:
            j=max(0,i-k)
            dp.append(min(v+abs(t-w)for v,w in zip(dp[j:i],h[j:i])))
    print((dp[-1]))
# Original solution
number = list(map(int,input().split()))
line = list(map(int,input().split()))
 
num = number[0]
K = number[1]
dp = [float("inf")]* num
dp[0] = 0

for i in range(1, num):
  I = min(i + 1 , K + 1)
  dp[i] = min([dp[i - k] + abs(line[i] - line[i - k]) for k in range(1 , I)])
  #kの範囲: k = 1,2,3...K 無理なときは k = 1,2..i
  
print((dp[-1]))

# Optimized solution
def ans():
    n, k, *h = list(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]))
 
ans()
# Original solution
num, K, *line = list(map(int, open(0).read().split()))
def main():
  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()

# Optimized solution
def main():
  num, K, *line = list(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()
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
def chmin(a, b):
    if a > b:
        return b
    else:
        return a

dp = [float('inf')] * n
dp[0] = 0
for i in range(1, n):
    for j in range(1, min(k+1, i+1)):
        dp[i] = chmin(dp[i], dp[i-j] + abs(h[i] - h[i-j]))

print((dp[n-1]))

# Optimized solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [float('inf')] * (n + 10)
dp[0] = 0
def chmin (a, b):
    if a < b:
        return a
    else:
        return b

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

print((dp[n - 1]))
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
N,K=list(map(int,input().split()))
H=[int(x) for x in input().split()]
answer=[0,abs(H[1]-H[0])]+[0]*(N-2)
for i in range(2,N):
  a=float('inf')
  answer[i]=min(answer[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i))
print((answer[-1]))

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
def main():
  N,K=list(map(int,input().split()))
  H=[int(x) for x in input().split()]
  answer=[0,abs(H[1]-H[0])]+[0]*(N-2)
  for i in range(2,N):
    a=float('inf')
    answer[i]=min(answer[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i))
  print((answer[-1]))
main()
# Original solution
# atcoder Educational DP contest/DP まとめコンテスト
# B-Frog2

def get_inputs():
    [n, k] = [int(x) for x in input().split()]
    h = list(map(int, input().split()))
    
    return n, k, h


if __name__ == "__main__":
    # 入力を受け取る
    # n 足場の数 2<= N <= 10**5
    # k k本先の足場まで飛ぶことができる 1<=K<=100
    # h 足場の高さ 配列
    n, k, h = get_inputs()
    
    # for文の二重ループではTLEになってしまう
    # dp = [10000000000] * (n + k)        # k個分を含む配列を用意し最大値で初期化する
    # h= h + [100000] * k
    # dp[0] = 0                             # 0番目の足場はコスト0
    # 足場iからj個先まで行く場合の最小コストを求める
    # for i in range(0, n - 1):
    #    for j in range(1, k + 1):
    #        dp[i + j] = min(dp[i] + abs(h[i + j] - h[i]), dp[i + j])
    # print(dp[n-1])
    
    dp = [10**9] * n
    dp[0] = 0
    
    for i in range(1, n):
        mc = 10**9
        for j in range(max(0, i - k), i):
            mc = min(mc, dp[j] + abs(h[i] - h[j]))
        dp[i] = mc
    
    print((dp[n-1]))
          

# Optimized solution
# atcoder Educational DP contest/DP まとめコンテスト
# B-Frog2

def get_inputs():
    [n, k] = [int(x) for x in input().split()]
    h = list(map(int, input().split()))
    
    return n, k, h


if __name__ == "__main__":
    # 入力を受け取る
    # n 足場の数 2<= N <= 10**5
    # k k本先の足場まで飛ぶことができる 1<=K<=100
    # h 足場の高さ 配列
    n, k, h = get_inputs()
    
    ########## first try
    # for文の二重ループではTLEになってしまう
    # dp = [10000000000] * (n + k)        # k個分を含む配列を用意し最大値で初期化する
    # h= h + [100000] * k
    # dp[0] = 0                             # 0番目の足場はコスト0
    # 足場iからj個先まで行く場合の最小コストを求める
    # for i in range(0, n - 1):
    #    for j in range(1, k + 1):
    #        dp[i + j] = min(dp[i] + abs(h[i + j] - h[i]), dp[i + j])
    # print(dp[n-1])
    
    ########## second try
    # 前案より速度は改善したが、まだ一部の問題でTLEになる
    # dp = [10**9] * n
    # dp[0] = 0
    #
    # for i in range(1, n):
    #     mc = 10**9
    #     for j in range(max(0, i - k), i):
    #         mc = min(mc, dp[j] + abs(h[i] - h[j]))
    #    dp[i] = mc
    # print(dp[n-1])
    
    ########## third try
    # 二重ループを内包表記表現に変更
    # zipを使うのがミソ
    
    dp = [10**9] * n
    dp[0] = 0
    for i in range(1, n):
        j = max(0, i - k)
        dp[i] = min([dd + abs(h[i] - hh) for (dd, hh) in zip(dp[j:i], h[j:i])])
    print((dp[n-1]))
# Original solution
n,k = list(map(int,input().split()))
arr = [int(i) for i in input().split()]
dp = [0 for j in range(n)]
for i in range(1,n):
    dp[i] = float("inf")
    for j in range(i-1,-1,-1):
        if i-j <= k:
            val = (abs(arr[i]-arr[j]) + dp[j]) 
            dp[i] = min(dp[i],val)
print((dp[n-1]))


# Optimized solution
n,k = list(map(int,input().split()))
arr = [int(i) for i in input().split()]
dp = [float("inf")] * 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],abs(arr[i]-arr[j]) + dp[i])
print((dp[n-1]))
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10**10
#dp = [[INF]*K]*N
dp = [[INF] * (K+1) for i in range(N)]

for i in range(N):
    #j_max = min(K+1, k)
    #print(dp)
    H_i = H[i]
    for j in range(1, K+1):
        if i==0:
            dp[i][j] = 0
        elif j>i:
            dp[i][j] = dp[i][j-1]
        else:
            #print(i,j)
            #print(dp[i-j][K]+abs(H[i]-H[i-j]), dp[i][j-1])
            dp[i][j] = min(dp[i-j][K]+abs(H_i-H[i-j]), dp[i][j-1])
            #print(dp[i][j])

print((dp[N-1][K]))

# Optimized solution
N, K = list(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 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]))