CodeEff/ECCO huggingface dataset:
# Original solution
def main():
    N, K = list(map(int, input().split()))
    field = list(map(int, input().split()))

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

    for i in range(2, N):
        tmpList = []
        minCost = -1

        for j in range(max(0, i-K), i):
            cost = dpList[j] + abs(field[i] - field[j])

            if(minCost == -1):
                minCost = cost
            elif minCost > cost:
                minCost = cost

        dpList[i] = minCost

    print((dpList[-1]))

if __name__ == "__main__":
    main()

# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    field = list(map(int, input().split()))

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

    for i in range(2, N):
        tmpList = []
        minCost = -1

        minS = max(0, i - K)

        for dpJ, fJ in zip(dpList[minS:i], field[minS:i]):
            cost = dpJ + abs(field[i] - fJ)

            if(minCost == -1):
                minCost = cost
            elif minCost > cost:
                minCost = cost

        dpList[i] = minCost

    print((dpList[-1]))

if __name__ == "__main__":
    main()
# Original solution
N,K = list(map(int,input().split()))
h = list(map(int,input().split()))
 
dp = [100000000000]*N
 
dp[0] = 0
dp[1] = abs(h[1]-h[0])
 
for i in range(2,N):
  for num in range(K):
    if num+1<=i:
      A = dp[i-num-1]+abs(h[i]-h[i-num-1])
      if dp[i] >= A:
        dp[i] = A
 
print((dp[N-1]))

# Optimized solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
INF = 10**18

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+j] - h[i]))
        else:
            break
print((dp[-1]))
# Original solution
n, k = list(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[i - j] + abs(h[i] - h[i - j]) for j in range(1, k + 1) if i - j >= 0)
print((dp[-1]))

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

dp = [0] * n
dp[1] = abs(h[0] - h[1])
for i in range(1, n):
  dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]) for j in range(1, k + 1) if i - j >= 0)
print((dp[-1]))
# Original solution
from sys import stdin

if __name__ == '__main__':
    n = stdin.readline()
    n, K = n.split(' ')
    n = int(n)
    K = int(K)
    arr = stdin.readline()
    arr = [int(a) for a in arr.split(' ')]
    dp = [float('inf')]*n
    dp[0] = 0
    for i in range(n):
        for k in range(1, K+1):
            if i - k < 0:
                break
            dp[i] = min(dp[i], dp[i-k]+abs(arr[i]-arr[i-k]))
    print((dp[-1]))

# Optimized solution
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 = [int(x) for x in input().split()]
    height = tuple([int(x) for x in input().split()])

    return n, k, height

main()
# Original solution
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

INF = 10**12

dp = [INF]*100010

dp[0] = 0

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

print((dp[N-1]))


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

INF = 10**9

dp = [INF]*100010

dp[0] = 0

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

print((dp[N-1]))
# Original solution
# coding: utf-8
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
dp = [100000]*N
dp[0] = 0
for i in range(1, N):
    dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(max(i-K, 0), i))
print((dp[N-1]))


# Optimized solution
def test():
    N, K = list(map(int, input().split()))
    H = list(map(int, input().split()))
    dp = [float('inf')]*N
    dp[0] = 0

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

    print((dp[-1]))

if __name__ == '__main__':
    test()
# Original solution
N,K=list(map(int,input().split()))
h=[0]*100010
h[:N]=[int(x) for x in input().split()]

dp=[float('inf')]*100100
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]))

# Optimized solution
N,K=list(map(int,input().split()))
h=[int(x) for x in input().split()]

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

for i, hi in enumerate(h):
  if i > 0:
    x = max(0,i-K)
    dp[i]=min([dpj + abs(hi-hj) for dpj, hj in zip(dp[x:i], h[x:i])])
     
print((dp[N-1]))
# Original solution
N,K=list(map(int,input().split()))
H=list(map(int,input().split()))
dp=[float('inf')]*N
dp[0]=0

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]+abs(H[i]-H[i-k]))

print((dp[N-1]))

# Optimized solution
N,K=list(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 n in range(2,N):
    if n-K>0:
        dp[n]=min([(dp[i]+abs(H[n]-H[i]))for i in range(n-K,n)])
    else:
        dp[n]=min([dp[i]+abs(H[n]-H[i])for i in range(0,n)])

print((dp[N-1]))
# Original solution
def resolve():
    N, K = list(map(int, input().split()))
    H = list(map(int, input().split()))

    dp = [0 for _ in range(N+K)]

    dp[0] = 0
    dp[1] = abs(H[1] - H[0])
    for tmp in range(1, K):
        #print("tmp:", tmp)
        if tmp+1 >= N:
            break
        candidates = [dp[tmp-k] + abs(H[tmp+1] - H[tmp-k]) for k in range(tmp+1)]
        #print("candidates_a:", candidates)
        dp[tmp+1] = min(candidates)

    #print(dp)
    for i in range(K-1, N-1):
        #print("i", i, "H[i+1]", H[i+1])
        #print("H:", [H[i - k] for k in range(K)])
        #print("diff:", [H[i+1] - H[i - k] for k in range(K)])
        #print("dp:", dp)
        candidates = [dp[i-k] + abs(H[i+1] - H[i-k]) for k in range(K)]
        #print("candidates:", candidates)
        dp[i+1] = min(candidates)

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

if __name__ == "__main__":
    resolve()


# Optimized solution
def resolve():
    N, K = list(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()
# Original solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))

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

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

for i in range(2, N):
    candidate = []
    for j in range(i-K, i):
        if j >= 0:
            candidate.append(dp[j]+abs(h[i]-h[j]))
    dp[i] = min(candidate)

print((dp[-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):
    h_to = h[i]
    dp[i] = min(
        [
            dp_from + abs(h_to - h_from)
            for dp_from, h_from in zip(
                dp[max(0, i-K):i], h[max(0, i-K):i]
            )
        ]
    )

print((dp[-1]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split())) + [0]*100010

dp = [float("inf")]*100010
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]))

# Optimized solution
N, K = list(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]))
# Original solution
n,k = list(map(int, input().split()))
a = list(map(int,input().split()))
dp = [0]*(n)
dp[1] = abs(a[1]-a[0]) 

for i in range(2,n):
    c = float("inf")
    for j in range(1,min(k,i)+1):
        c = min(c,dp[i-j] + abs(a[i] -a[i-j]))
    dp[i] = c
print((dp[n-1]))

# Optimized solution
n,k = list(map(int, input().split()))
a = list(map(int,input().split()))
dp = [0]*n
dp[1] = abs(a[1]-a[0]) 

for i in range(2,n):
    c = float("inf")
    for j in range(1,min(k,i)+1):
        c = min(c,dp[i-j] + abs(a[i] -a[i-j]))
    dp[i] = c
print((dp[n-1]))
# Original solution

def func():
    N, K = [int(i) for i in input().split()]
    H = [int(i) for i in input().split()]
    
    dp = [0]*N
    for i in range(1, N):
        L = []
        for j in range(max(0, i-K), i):
            L.append(dp[j] + abs(H[i] - H[j]))
        dp[i] = min(L)

    print((dp[-1]))
    
if __name__=="__main__":
    func()

# Optimized solution
N, K = [int(i) for i in input().split()]
H = [int(i) for i in input().split()]
dp = [0] * N

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

        )


print((dp[N-1]))


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

d = [0] * n

if k >= n:
    k = n-1


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

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

print((d[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
n,k=list(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,min(k+1,i+1)):
        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 = [10 ** 9] * N
dp[0] = 0

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

n,k = list(map(int,input().split()))
arr = list(map(int,input().split()))
dp = [float("inf")]*len(arr)
dp[0] = 0
cost = 0
for i in range(len(arr)):
    for jump in range(i+1,i+k+1):
        if(jump<len(arr)):
            dp[jump] = min(dp[jump],dp[i]+abs(arr[jump]-arr[i]))
print((dp[-1])) 

# Optimized solution
if __name__ == "__main__":
    n,k = list(map(int,input().split()))
    arr = list(map(int,input().split()))
    dp = [-1]*len(arr)
    curr = 1 
    dp[0] = 0
    dp[1] = abs(arr[1]-arr[0])
    cost = 0
    for i in range(2,len(arr)):
        m = float("inf")
        x = i-1
        c = 0
        while(x>=0 and c<k):
            m= min(m,dp[x]+abs(arr[i]-arr[x]))
            x -= 1
            c += 1 
        dp[i] = m
    print((dp[-1]))
# Original solution
from sys import stdin
n, k = [int(i) for i in stdin.readline().split(' ')]
h = [int(i) for i in stdin.readline().split(' ')]

minCosts = [0, abs(h[1]-h[0])]
for i in range(2, len(h)):
    minCosts.append(min([abs(h[i]-h[i-j])+minCosts[i-j] for j in range(1, min(i, k)+1)]))

print((minCosts[-1]))

# Optimized solution
def test():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [float('inf')]*N
    dp[0] = 0
    
    for i in range(1,N):
        dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K, 0), i))
    
    print((dp[N-1]))
    
if __name__ == '__main__':
    test()
# Original solution
def test():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [float('inf')]*N
    dp[0] = 0
    
    for i in range(1,N):
        dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K, 0), i))
    
    print((dp[N-1]))
    
if __name__ == '__main__':
    test()

# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    a = [0] * n
    a[0] = 0
    for i in range(1, n):
        a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)])
    print((a[-1]))
 
if __name__ == '__main__':
    main()
# Original solution
import math

class Input():
	def readInt(self):
		return int(eval(input()))

	def readIntArray(self):
		return [int(x) for x in input().split(" ")]

	def readString(self):
		return eval(input())

	def readStringArray(self):
		return input().split(" ")

	def readFloat(self):
		return float(eval(input()))

	def readFloatArray(self):
		return [float(x) for x in input().split(" ")]

def main():
	inp = Input()
	n, k = inp.readIntArray()
	height = inp.readIntArray()

	cost = [1000000002 for i in range(n)]
	cost[0] = 0

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

	print((cost[n-1]))
	

if __name__ == "__main__":
   main()


# Optimized solution
n,k = list(map(int,input().split()))
l = list(map(int,input().split()))
a = [0]*n
a[1] = abs(l[0]-l[1])
i = 2
while(i < len(a)):
    e = i-k
    ma = 10**10
    j = i-1
    while(j > -1 and j >= e):
        r = a[j]+abs(l[i]-l[j])
        ma = min(ma,r)
        j = j-1

    a[i] = ma
    i = i+1

#print(a)
print((a[-1]))
# Original solution
N,K = list(map(int,input().split(" ")))
hs = list(map(int,input().split(" ")))
dp = [9999999999999] * N

dp[0] = 0
for i in range(1,N):

    stepLim = min(K,i)
    temp = 9999999999999999999
    for j in range(1,stepLim + 1):
        ind = i-j
        c = dp[ind]+abs(hs[i]-hs[ind])
        if temp >c:
                temp = c
    dp[i] = temp
print((dp[-1]))

# Optimized solution
N,K = list(map(int,input().split(" ")))
hs = list(map(int,input().split(" ")))
dp = [9999999999999] * N

dp[0] = 0
for i in range(1,N):
#ind → i-1 ~ i-stepLim -1   [i-stepLim -1:i]
    stepLim = min(K,i)
    temp = 9999999999999999999
    dp[i] = min([d+abs(hs[i]-h) for d,h in zip(dp[i-stepLim:i],hs[i-stepLim:i])])
print((dp[-1]))
# Original solution
N,K=list(map(int,input().split(" ")))
cost=[0]*(N+1)

h=[0]
hh=list(map(int,input().split(" ")))
h.extend(hh)
h[0]=h[1]

cost[1]=0
 
for i in range(2,N+1):
  j=min(i,K)
  cost[i]=min(mm+abs(h[i]-nn) for mm,nn in zip(cost[i-j:i],h[i-j:i]))
print((cost[N]))

# Optimized solution
N,K=list(map(int,input().split(" ")))
cost=[0]*(N+1)

h=[0]
hh=list(map(int,input().split(" ")))
h.extend(hh)
h[0]=h[1]

cost[1]=0
 
for i in range(2,N+1):
  z=max(0,i-K)
  cost[i]=min(mm+abs(h[i]-nn) for mm,nn in zip(cost[z:i],h[z:i]))
print((cost[N]))
# Original solution
#配るDP（多分
n,k=list(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(k):
        if i+j<n:
            dp[i+j]=min(dp[i+j],dp[i-1]+abs(h[i+j]-h[i-1]))

print((dp[-1]))

# Optimized solution
#DP-B

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

print((dp[-1]))
# Original 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] = min(
		dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
	)

print((dp[N-1]))

# Optimized solution
def main():
	N, K = list(map(int, input().split()))
	h = list(map(int, input().split()))

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print((dp[N-1]))
main()
# Original 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] = min(
        dp[g] + abs(h[i] - h[g]) for g in range(max(i - K, 0), i)
    )
print((dp[N - 1]))

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

if __name__ == "__main__":
  main()

# Optimized solution
def main():
  n, k = list(map(int, input().split()))
  h = [int(n) for n in input().split()]
  dp = [0]*n
  for i in range(1, 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()
# Original 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] = min([dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)])
print((dp[-1]))

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

main()
# Original solution
def main():
  N, K = list(map(int,input().split()))
  H = list(map(int,input().split()))

  dp = [1e9]*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]))
      else:
        break
  print((dp[N-1]))

if __name__ == "__main__":
  main()

# Optimized solution
def main():
  N, K = list(map(int,input().split()))
  H = list(map(int,input().split()))

  dp = [1e10]*N
  dp[0] = 0
  for i, h in enumerate(H):
    if i == 0:
      continue
    x = max(0, i-K)
    dp[i] = min([d+abs(h-ht) for d, ht in zip(dp[x:i], H[x:i])])
  print((dp[-1]))

if __name__ == "__main__":
  main()
# Original solution
def main():
  n,k = list(map(int,input().split()))
  h = list(map(int,input().split()))
  dp= [0]*n

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

if __name__=='__main__':
  main()

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

for i in range(1,n):
  tmp = float('inf')
  for j in range(1,min(i+1,k+1)):    
    d = dp[i-j] + abs(h[i] - h[i-j])    
    tmp = min(tmp,d)  
  dp[i] = tmp
  
print((dp[-1]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [float("inf") for _ in range(N)]
derta = [[-1 for i in range(N)] for j in range(N)]

def cost(n, m):
  if derta[n][m] == -1:
    derta[n][m] = abs(h[n] - h[m])
    return derta[n][m]
  else:
    return derta[n][m]

dp[0] = 0

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

print((dp[N - 1]))

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

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

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

dp[0] = 0

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

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



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

main()
# Original solution
#TLE

import sys

INF = float('inf')


input = sys.stdin.readline


def LIST():
    return list(map(int, input().split()))


N, K = LIST()
h = LIST()

# DP配列を初期化
dp = [INF] * N

# 初期条件を入力
dp[0] = 0

# テーブルを更新 もらうDP
for i in range(1, N):
    dp[i] = min([dp[i - j] + abs(h[i] - h[i - j]) for j in range(1, min(K, i) + 1)])

print((dp[N - 1]))


# Optimized solution
import sys

INF = float('inf')

input = sys.stdin.readline


def LIST():
    return list(map(int, input().split()))


N, K = LIST()
h = LIST()

# DP配列を初期化
dp = [INF] * N

# 初期条件を入力
dp[0] = 0

# テーブルを更新 もらうDP
for i in range(1, N):
    x = max(0, i - K)
    dp[i] = min([dp_ + abs(h[i] - h_) for dp_, h_ in zip(dp[x:i], h[x:i])])

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

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

# dp[i] := 足場iに来るのにかかる最小のコスト
INF = 10**18
dp = [INF] * (K + N)
dp[0] = 0
for i in range(1, N + K):
    for j in range(1, K + 1):
        v = dp[i - j] + abs(H[i] - H[i - j])
        dp[i] = min(dp[i], v)
# print('dp', dp)
ans = dp[-1]
print(ans)


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

# dp[i] := 足場iに来るのにかかる最小のコスト
INF = 10**18
dp = [INF] * (K + N)
dp[0] = 0
for i in range(K, N + K):
    h = H[i]
    dp[i] = min(x + abs(y - h) for x, y in zip(dp[i - K:i], H[i - K:i]))
#     for j in range(1, K + 1):
#         v = dp[i - j] + abs(H[i] - H[i - j])
#         dp[i] = min(dp[i], v)
# print('dp', dp)
ans = dp[-1]
print(ans)
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
H = [H[0]] * K + H

# dp[i] := 足場iに来るのにかかる最小のコスト
INF = 10**18
dp = [INF] * (K + N)
dp[0] = 0
for i in range(K, N + K):
    h = H[i]
    dp[i] = min(x + abs(y - h) for x, y in zip(dp[i - K:i], H[i - K:i]))
#     for j in range(1, K + 1):
#         v = dp[i - j] + abs(H[i] - H[i - j])
#         dp[i] = min(dp[i], v)
# print('dp', dp)
ans = dp[-1]
print(ans)


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

# dp[i] := 足場iに来るのにかかる最小のコスト
INF = 10**18
dp = [INF] * (K + N)
dp[0] = 0
for i in range(K, N + K):
    h = H[i]
    tmp = [x + abs(y - h) for x, y in zip(dp[i - K:i], H[i - K:i])]
    dp[i] = min(tmp)
#     dp[i] = min(x + abs(y - h) for x, y in zip(dp[i - K:i], H[i - K:i]))
#     for j in range(1, K + 1):
#         v = dp[i - j] + abs(H[i] - H[i - j])
#         dp[i] = min(dp[i], v)
# print('dp', dp)
ans = dp[-1]
print(ans)