CodeEff/ECCO huggingface dataset:
# Original solution
n,k = list(map(int,input().split(' ')))
h = list(map(int, input().split(' ')))

dp = [0]*n 

for i in range(1,n):
    temp = dp[i-1]+abs(h[i]-h[i-1])
    for j in range(2,k+1):
        if i-j>=0:
            temp = min(dp[i-j]+abs(h[i]-h[i-j]), temp)
    
    dp[i] = temp
    
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):
    dp[i] = dp[i-1]+abs(h[i]-h[i-1])
    for j in range(2,k+1):
        if i-j>=0:
            dp[i] = min(dp[i-j]+abs(h[i]-h[i-j]), dp[i])
    
print((dp[-1]))
# Original solution
#!/usr/bin/env python3
import sys

def solve(N: int, K: int, h: "List[int]"):
    cumsums = [float("inf")]*N
    cumsums[0]=0
    cumsums[1]=abs(h[1]-h[0])

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


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    print((solve(N, K, h)))

if __name__ == '__main__':
    main()


# Optimized solution
import sys

def solve(N: int, K: int, h: "List[int]"):
    cumsums = [float("inf")]*N
    cumsums[0]=0
    cumsums[1]=abs(h[1]-h[0])

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

N, K = list(map(int, sys.stdin.readline().rstrip().split()))
h = list(map(int, sys.stdin.readline().rstrip().split()))
print((solve(N, K, h)))
# Original solution
n, k = list(map(int, input().split()))
h = tuple(map(int, input().split()))

INF = 10**10
dp = [INF] * n
dp[0] = 0
# dp[i] = (足場iにたどり着いた時点でのコストの最小値)

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

print((dp[n - 1]))


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

INF = 10**10
dp = [INF] * n
dp[0] = 0
# dp[i] = (足場iにたどり着いた時点でのコストの最小値)

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[n - 1]))
# 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)
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[-1]))

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

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

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

# Optimized solution
import sys
n, k = list(map(int, input().split()))
h = 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(h[i]-h[j]))
            
print((dp[-1]))
# Original solution
def main():
    N, K = list(map(int,input().split()))
    h = [int(v) for v in input().split()]
    
    dp = [0 for _ in range(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(i - K, i) if j >= 0])
    
    print((dp[-1]))

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    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[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(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
    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()
# Original solution
N,K = list(map(int, input().split()))
lst = list(map(int, input().split()))
dp = [0 for i in range(N)]
for i in range(1,N):
    mn = 999999999999999999
    for j in range(1,K+1):
        if i-j >= 0:
            mn = min(mn,dp[i - j] + abs(lst[i] - lst[i - j]))
        else:
            break
    dp[i] = mn
print((dp[-1]))

# Optimized solution
N,K = list(map(int, input().split()))
lst = list(map(int, input().split()))
dp = [0 for i in range(N)]
for i in range(1,N):
    mn = 999999999999999999
    for j in range(max(i-K,0), i):
        mn = min(mn,dp[j] + abs(lst[i] - lst[j]))
    dp[i] = mn
print((dp[-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):
    dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(max(i - K, 0), i))
print((int(dp[i])))


# 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
import math
import bisect
import heapq
from collections import deque

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

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

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

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

    d[j] = min(p)

print((d[n - 1]))


# Optimized solution
import math
import bisect
import heapq
from collections import deque

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

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

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

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

        d[j] = min(p)

    print((d[-1]))

if __name__ == "__main__":
    main()
# Original solution
import math
import bisect
import heapq
from collections import deque

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

    h = list(map(int, input().split()))
    inf = 10**7
    d = [inf] * n
    d[0] = 0

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

        d[j] = min(p)

    print((d[-1]))

if __name__ == "__main__":
    main()

# Optimized solution
# import math
# import bisect
# import heapq
# from collections import deque

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

    h = list(map(int, input().split()))
    inf = 10**7
    d = [inf] * n
    d[0] = 0

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

        d[j] = min(p)

    print((d[-1]))

if __name__ == "__main__":
    main()
# Original solution
from sys import stdin
N,K=list(map(int,stdin.readline().split()))
h=[int(i) for i in stdin.readline().split()][:N]
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],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print((dp[-1]))

# Optimized solution
from sys import stdin
N,K=list(map(int,stdin.readline().split()))
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf') for i in range(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(h[j]-h[i]))
    #print(dp)
print((dp[-1]))
# Original solution
from collections import deque
def solve(n,k,arr):
    q,cost_=deque([ (0,0) ]),float('inf') # (idx,cost)
    last_level=0
    while q:
        for _ in range(len(q)):
            idx,cost=q.popleft()
            if idx==n-1:
                cost_=min(cost_,cost)
                last_level=1
            else:
                if not last_level:
                    # choose next 3 elem as the exploring options
                    options = [j for j in range(idx + 1, idx + 1 + k) if j < n]
                    for index in options:
                        q.append((index, cost+abs(arr[index] - arr[idx])))
    print(cost_)
n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
solve(n,k,arr)

# Optimized solution
def solve(n,k,arr):
    if n==1:
        return 0
    else:
        dp=[float('inf') for _ in range(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(arr[i]-arr[j])
                    )
    return dp[-1]

n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
print((solve(n,k,arr)))
# Original solution
inf = float('inf')

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

dp = [inf] * n
dp[0] = 0

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[n - 1]))


# Optimized solution
inf = float('inf')

n, k = list(map(int, input().split()))
h = list(map(int, input().split())) + [inf] * k

dp = [inf] * (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 + j] - h[i]))
print((dp[n - 1]))
# Original solution
def solve(n,k,ar):

    dp = [int(1e9)]*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(ar[i]-ar[j]))
    print((dp[n-1]))

def solve1(n,k,ar):
    dp = [int(1e9)] * (n)
    dp[0] = 0
    for i in range(1,n):
        for k in range(1,k+1):
            if i-k >= 0:
                dp[i] = min(dp[i], dp[i-k] + abs(ar[i]-ar[i-k]))

    print((dp[n-1]))


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

    solve(n,k,ar)

# Optimized solution
import sys

def solve(n,k,ar):

    dp = [sys.maxsize]*n
    dp[0] = 0

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

    print((dp[n-1]))



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

    solve(n,k,ar)
# Original solution
def solve():
  N, K = list(map(int, input().split(" ")))
  h = input().split(" ")
  h = [int(number) for number in h]

  minimumCostToReach = [0 for i in range(N)]

  minimumCostToReach[0] = 0
  for currentIndex in range(1, N):
    minimumCostToReach[currentIndex] = 1000000000

    for previousIndex in range(max(currentIndex - K, 0), currentIndex):
      cost = minimumCostToReach[previousIndex] + abs(h[currentIndex] - h[previousIndex])
      minimumCostToReach[currentIndex] = min(minimumCostToReach[currentIndex], cost)

  print((minimumCostToReach[N - 1]))

solve()

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

  minimumCost = [0] * N

  for currentIndex in range(1, N):
    minimumCost[currentIndex] = min(minimumCost[previousIndex] + abs(h[currentIndex] - h[previousIndex]) for previousIndex in range(max(currentIndex - K, 0), currentIndex))

  print((minimumCost[N - 1]))

solve()
# Original solution
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = list(map(int, input().split()))
h = list(map(int, input().split()))

suml = [1e9 for i in range(N)]
suml[0] = 0

for e in range(1,N):

    # for k in range(1, min([e, K])+1):
    #     suml[e] = min(suml[e],
    #             abs(h[e]-h[e-k]) + suml[e-k]
    #     )
    start = max(0, e-K)
    cost  = min(cst + abs( h[e] - hgh ) for cst, hgh in zip(suml[start:e], 
           h[start:e]))
    suml[e] = cost

print((suml[N-1]))
        



# Optimized solution
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = list(map(int, input().split()))
h = list(map(int, input().split()))

# suml = [1e9 for i in range(N)]
# suml[0] = 0
suml = [0]*N

for e in range(1,N):

    # for k in range(1, min([e, K])+1):
    #     suml[e] = min(suml[e],
    #             abs(h[e]-h[e-k]) + suml[e-k]
    #     )
    start = max(0, e-K)
    cost  = min(cst + abs( h[e] - hgh ) for cst, hgh in zip(suml[start:e], 
           h[start:e]))
    suml[e] = cost

print((suml[N-1]))
# Original solution
n, k = tuple(map(int, input().split()))
hs = list(map(int, input().split()))
cs = [0 for _ in range(k)]
k_i = 0
for i in range(1, n):
    if i < k:
        cs[i] = min([cs[j] + abs(hs[j] - hs[i]) for j in range(i)])
    else:
        k_i = i % k
        cs[k_i] = min([
            cs[(k_i - j - 1) % k] + abs(hs[i - j - 1] - hs[i])
            for j in range(k)
        ])
print((cs[i % k]))


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

dp = [0] + [float("inf") for _ in range(n - 1)]

for i in range(1, n):
    for j in range(1, min(i, k) + 1):
        dp[i] = min(dp[i], dp[i - j] + abs(hs[i - j] - hs[i]))
print((dp[i]))
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
dp=[float('inf')]*n
dp[0]=0
for i in range(0,n):
    for j in range(1,k+1):
        if i+j>n-1:
            continue
        dp[i+j]=min(dp[i+j],dp[i]+abs(a[i]-a[i+j]))
print((dp[-1]))

# Optimized solution
n,k=list(map(int,input().split()))
a=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:
            continue
        dp[i]=min(dp[i],dp[i-j]+abs(a[i]-a[i-j]))
print((dp[n-1]))
# Original solution
# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(eval(input()))

def main():
    N, K = geta(int)
    h = geta(int)

    INF = 1 << 60
    dp = [INF for _ in range(N)]
    dp[0], dp[1] = 0, abs(h[0] - h[1])

    for i in range(2, N):
        lb = i - K if i > K else 0
        for j in range(lb, i):
            tmp = dp[j] + abs(h[j] - h[i])
            if tmp < dp[i]:
                dp[i] = tmp

    print((dp[N - 1]))

if __name__ == "__main__":
    main()

# Optimized solution
# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(eval(input()))

def main():
    N, K = geta(int)
    h = geta(int)

    INF = 1 << 60
    dp = [INF]*N
    dp[0], dp[1] = 0, abs(h[0] - h[1])

    for i in range(2, N):
        lb = i - K if i > K else 0
        for j in range(lb, i):
            tmp = dp[j] + abs(h[j] - h[i])
            if tmp < dp[i]:
                dp[i] = tmp

    print((dp[N - 1]))

if __name__ == "__main__":
    main()
# Original solution
# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(eval(input()))
 
def main():
    N, K = geta(int)
    h = geta(int)
 
    INF = 1 << 60
    dp = [INF]*N
    dp[0], dp[1] = 0, abs(h[0] - h[1])
 
    for i in range(2, N):
        lb = i - K if i > K else 0
        for j in range(lb, i):
            tmp = dp[j] + abs(h[j] - h[i])
            if tmp < dp[i]:
                dp[i] = tmp
 
    print((dp[-1]))
 
if __name__ == "__main__":
    main()

# Optimized solution
# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(eval(input()))

def main():
    N, K = geta(int)
    h = geta(int)

    INF = 1 << 60
    dp = [INF]*N
    dp[0], dp[1] = 0, abs(h[0] - h[1])

    for i in range(2, N):
        lb = i - K if i > K else 0
        for j in range(lb, i):
            tmp = dp[j] + abs(h[j] - h[i])
            if tmp < dp[i]:
                dp[i] = tmp

    print((dp[N - 1]))

if __name__ == "__main__":
    main()
# Original solution
n, k = [int(e) for e in input().split()]
h = [int(e) for e in input().split()]
cost = [0] * n
for i in range(1, n):
  cost[i] = min(abs(h[i] - h[j]) + cost[j] for j in range(max(0, i - k), i))
print((cost[n - 1]))

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

    #初期化
    dp[0]=0
    dp[1]=abs(asiba[1]-asiba[0])

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

# Optimized solution
def main():
    N,K = [int(e) for e in input().split()]
    asiba = [int(e) for e in input().split()]
    dp =[0] *N

    #初期化
    dp[0]=0
    dp[1]=abs(asiba[1]-asiba[0])

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

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

  for i in range(2,N):
    index = i - K
    if index < 0: index = 0
    ans = 10**9
    for j in range(index,i):
      ans = min(ans,dp[j] + abs(h[i] - h[j]))
    dp[i] = ans
  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 = [-1]*N
  dp[0] = 0
  dp[1] = abs(h[0] - h[1])
  for i in range(2,N):
    tmp = i - K
    if tmp < 0: tmp = 0
    cost_min = 10**9
    for j in range(tmp,i):
      ans = dp[j] + abs(h[i] - h[j])
      if cost_min > ans: cost_min = ans
    dp[i] = cost_min
  print((dp[N-1]))

if __name__ == '__main__':
  main()
# 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):
  dp[i] = min(dp[j]+abs(h[i]-h[j]) for j in range(max(0, i-k), i))

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

    cost = n * [None]
    cost[0] = 0
    for i in range(1, len(h)):
        #cost[i] = min([cost[s] + abs(h[i] - h[s]) for s in range(max(0, i-k), i)])
        cost[i] = min([cost[s] + abs(h[i] - h[s]) for s in range(max(0, i-k), i)])

    return cost[n-1]


if __name__ == "__main__":
    n, k = list(map(int, input().split(" ")))
    h = list(map(int, input().split(" ")))
    res = calc_cost2(h, k)
    print(res)

# Optimized solution
def calc_cost2(h, k):
    n = len(h)
    if (n == 0):
        return 0

    cost = n * [None]
    cost[0] = 0
    for i in range(1, len(h)):
        cost[i] = min([cost[s] + abs(h[i] - h[s]) for s in range(max(0, i-k), i)])
        #cost[i] = min(map(lambda s: cost[s] + abs(h[i] - h[s]), range(max(0, i-k), i)))

    return cost[n-1]


if __name__ == "__main__":
    n, k = list(map(int, input().split(" ")))
    h = list(map(int, input().split(" ")))
    res = calc_cost2(h, k)
    print(res)
# Original solution
def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    c = [0] + [abs(h[1] - h[0])]
    for i in range(2, n):
        min_step = 10 ** 4
        for j in reversed(list(range(i))):
            if i - j > k:
                continue
            step = abs(h[i] - h[j]) + c[j]
            if step < min_step:
                min_step = step
        c.append(min_step)
    print((c.pop()))


if __name__ == "__main__":
    main()


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


def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [0] + [float("inf")] * (n-1)
    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[-1]))


if __name__ == "__main__":
    main()
# Original solution
N, K = list(map(int, input().split()))
h = [int(x) for x in input().split()]

INF = float('inf')
dp = [INF for _ in range(N+K+10)]

# initial state
dp[0] = 0

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

print((dp[N-1]))

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

INF = float('inf')
dp = [INF for _ in range(N+K+10)]

# initial state
dp[0] = 0

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

print((dp[N-1]))
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))

dp=["INF" for _ in range(n)]
dp[0]=0
#print(r)

for i in range(n):
  for j in range(min(k,n-i-1)):
    if dp[i+j+1]=="INF":
      dp[i+j+1]=dp[i]+abs(a[i+j+1]-a[i])
    else:
      dp[i+j+1]=min(dp[i+j+1],dp[i]+abs(a[i+j+1]-a[i]))
      #print(i,i+j,">>",r)
  
print((dp[-1]))


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

dp=[100000000000000000 for _ in range(n)]
dp[0]=0
#print(r)

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


def totalcost_recur(i,cost,N,lookup):
  if i>=N:
    return 0
  if i in lookup:
      return lookup[i]
  option1 = abs(cost[i]-cost[i+1]) + totalcost_recur(i+1,cost,N,lookup)
  ans = option1
  if i+2<=N:
    option2 = abs(cost[i]-cost[i+2]) + totalcost_recur(i+2,cost,N,lookup)
    ans = min(option2,ans)
  lookup[i] = ans
  return ans



def totalcost_dp(cost,N,k):
    dp = [float('inf')]*(N+1)
    dp[1] = 0
    
    
    for i in range(1,N+1):
        
        for j in range(i+1,k+i+1):
            if j < N+1:
                dp[j] = min(dp[j],dp[i]+abs(cost[i]-cost[j]))
                
            
        
        

    return dp[N]


n ,k= list(map(int,stdin.readline().split()))
cost = list(map(int,stdin.readline().split()))
cost = [0]+cost


print((totalcost_dp(cost,n,k)))

# Optimized solution

def totalcost_dp(cost,N,k):
    dp = [float('inf')]*(N+1)
    dp[1] = 0
    
    
    for i in range(1,N+1):
        
        for j in range(i+1,k+i+1):
            if j < N+1:
                dp[j] = min(dp[j],dp[i]+abs(cost[i]-cost[j]))
                
    return dp[N]


n ,k= list(map(int,input().split()))
cost = list(map(int,input().split()))
cost = [0]+cost
print((totalcost_dp(cost,n,k)))
# Original solution
# 貰うDP
N,K=list(map(int,input().split()))
h=list(map(int,input().split()))
INF=10**9
dp=[INF]*(N)
dp[0]=0

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

# Optimized solution
# 配るDP
N,K=list(map(int,input().split()))
H=list(map(int,input().split()))
INF=10**9
add_list=[INF]*K

H.extend(add_list)

dp=[INF]*(N+100) #Kの分追加
dp[0]=0
for n in range(N):
    for k in range(n+1,n+K+1):
        dp[k]=min(dp[k], dp[n]+abs(H[n]-H[k]))
print((dp[N-1]))