CodeEff/ECCO huggingface dataset:
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
INF = float('inf')
dp = [INF] * (1 + 10 ** 5)
dp[0] = 0
for i in range(n):
    for j in range(k + 1):
        if i - j < 0:continue
        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()))
INF = float('inf')
dp = [INF] * (1 + 10 ** 5)
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        if i - j < 0:
            continue
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
print((dp[n-1]))
# Original solution
#       )                         .MMdMMM;                    .M#(,dM]                        .:
#       )                        .M@d@d#Mb                    dMGMM(MN                        .:
#       )                        dM1MCdbMM_                  .M@MSN;dM-                       .:
#       )                       .M#(#?d#(Mb.. ...............JMIMIMLdM]                       .:
#       )                      ..M#?NHM# ?MMMMMMMMMH"""""""HMM@(Nad#(Mb                       .:
#       )                  .(MMMMH=..-(Jg[   `               daggmp  MNm,                     .:
#       )                ..MMY`     !qN;                       (J     ?MMN,                   .:
#       )               (MMD`       .MM@                      .MM{      .WMN,                 .:
#       )              dM@`         dMM%                      .MM}        (MMm                .:
#       )             .M#`          dMN!                      .MM}        ~(MMr               .:
#       )            .MM:           dM#                       .MM}        ..JM#               .:
#       )      ``    .M@            dMF                       .MM}        .~(M#   `  .        .:
#       )      ?MMMMMMMNgJ,         .7`                        T"!       .JJjMNgNNMMMB^       .:
#       )            dMr ?Mm,                                            7YYMM#!`             .:
#       )            dMb   ?`                  .,   ` ..                 ~.(MM%               .:
#       )         ..JgMMNNN)                  .Mn.(Mm-JM{               dHHMMNag.,            .:
#       )         ("!  ?MN,..,                (NM@"`~OMM}             ..~-(M@` _?"M[          .:
#       )               ,MMM#_                .M#    .M#`           .~.~JMMMNg-..             .:
#       )           ..gMB"?TMMNJ..              `     _`         .~~.(JdM#^  _"TMMNgJ-,       .:
#       )        .gMB=`      _7WMMNaJ...                 ....__-(JJgMMMM^          ?T"^       .:
#       )           ............  ?TWMMMMNNNNgggggggggggggNNMMMMMMBY7`...                     .:
#       )           WMMMMMMMMMMMMMNgJ,   (dMMMM#"TYYYYYYMMMMMMMNgMMMMMMMMMMMNNgJ-,            .:
#       )            -?TMNmuuuuUWMMMMMMNNMM@g@MMmJ--_(JMMg@g@@gMMMMMMWUXXXUWMMMMMMNe.         .:

import sys
input = sys.stdin.readline

def f(x,y):
    if x in dic:
        dic[x] = min(dic[x],y)
        return
    dic[x] = y

dic = {}
dic[0] = 0
n,k = list(map(int,input().split()))
h = tuple(map(int,input().split()))

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

print((dic[n-1]))


# Optimized 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]))
# Original solution
def main():
    n, k = list(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):
        for j in range(1, min(k+1, n-i)):
            num0 = h[i]-h[i+j]
            if num0 < 0:
                num0 *= -1
            num = dp[i]+num0
            if num < dp[i+j]:
                dp[i+j] = num
    print((dp[-1]))

main()


# Optimized solution
def main():
    n, k = list(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):
        a = min(k+1, n-i)
        for j in range(1, a):
            num = dp[i]+abs(h[i]-h[i+j])
            if num < dp[i+j]:
                dp[i+j] = num
    print((dp[-1]))

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

main()


# Optimized solution
def main():
    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(1, min(k+1, n-i)):
            num = dp[i]+abs(h[i]-h[i+j])
            if num < dp[i+j]:
                dp[i+j] = num
    print((dp[-1]))

main()
# Original solution
n, k = list(map(int, input().split()))
step = list(map(int, input().split()))

ans = [float('inf')]*(10**6)

ans[0] = 0
def dp(n):
  for i in range(n):
    for j in range(k+1):
      if i+j < n:
        ans[i+j] = min(ans[i+j], ans[i] + abs(step[i+j] - step[i]))
  return ans[n-1]

print((dp(n)))

# Optimized solution
n, k = list(map(int, input().split()))
lst = list(map(int, input().split())) + [0]*k
ans = [float('inf')]*(n+k)
ans[0] = 0

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

dp()

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

def chmin(a,b):
    if a>b:
        a=b
        return True
    return False

for i in range(1,n):
    for j in range(1,k+1):
        if i-j<0:
            break
        else:
            step= abs(h[i]-h[i-j])
            dp[i] = min(dp[i],dp[i-j]+step)


print((dp[n-1]))

# Optimized solution
n,k = list(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]))
# Original solution
import functools
import threading
import sys


class SearchProblem(object):
    def __init__(self, N, H, K):
        self.N = N
        self.H = H
        self.K = K

    def isEnd(self, i):
        return i == self.N

    def succ(self, i):
        return [
            (i + k, abs(self.H[i + k] - self.H[i]))
            for k in range(1, self.K + 1)
            if i + k <= self.N
        ]


def solve(problem: SearchProblem):
    @functools.lru_cache(maxsize=None)
    def future_cost(s):
        if problem.isEnd(s):
            return 0
        else:
            return min(
                cost + future_cost(next_state) for next_state, cost in problem.succ(s)
            )

    return future_cost(1)


def main():
    N, K = [int(x) for x in input().split(" ")]
    H = [None] + [int(x) for x in input().split(" ")]
    problem = SearchProblem(N, H, K)
    print((solve(problem)))


if __name__ == "__main__":

    sys.setrecursionlimit(1024 * 1024 * 2)
    threading.stack_size(128 * 1024 * 1024 * 2)
    threading.Thread(target=main).start()


# Optimized solution
import functools
import threading
import sys


def main():
    @functools.lru_cache(maxsize=None)
    def solve(n):
        if n == 1:
            return 0
        else:
            return min(
                solve(n - k) + abs(H[n] - H[n - k])
                for k in range(1, K + 1)
                if n - k >= 1
            )

    N, K = [int(x) for x in input().split(" ")]
    H = [None] + [int(x) for x in input().split(" ")]
    print((solve(N)))


if __name__ == "__main__":

    sys.setrecursionlimit(1024 * 1024 * 2)
    threading.stack_size(128 * 1024 * 1024 * 2)
    threading.Thread(target=main).start()
# Original solution
from sys import stdin , stdout
def fun(a,k): ## k different steps can be taken
    if len(a)==1:
        return 0
    dp = [float('inf')]*len(a)
    dp[0]=0
    dp[1]=dp[0]+abs(a[0]-a[1])
    for i in range(2,len(a)):
        t = 1
        for j in range(1,k+1):
            if i-j<0:
                break
            dp[i]=min(dp[i] , dp[i-j]+abs(a[i]-a[i-j]))
    return dp[-1]
  

n,k = list(map(int , stdin.readline().split()))
a = list(map(int , stdin.readline().split()))
print((fun(a,k)))

# Optimized solution
def fun(a,k,n): ## k different steps can be taken
    if n==1:
        return 0
    dp = [0]*n
    dp[1]=dp[0]+abs(a[0]-a[1])
    for i in range(2,len(a)):
        t = 0
        min_ = float('inf')
        for j in range(i-1,-1,-1):
            if t==k:
                break
            else:
                t+=1
            if dp[j]+abs(a[i]-a[j]) < min_ :
                   min_ = dp[j]+abs(a[i]-a[j])
        dp[i]=min_
    return dp[-1]
from sys import stdin , stdout
n,k = list(map(int , stdin.readline().split()))
a = list(map(int , stdin.readline().split()))
print((fun(a,k,n)))
# Original solution
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

def g(n, k, h):
    gk = deque([0, abs(h[1] - h[0])], k)
    g = abs(h[1] - h[0])
    for i in range(2, n):
        h_i = h[i]
        j = max(i - k, 0)
        g = min(abs(h_i - h_j) + h_k for h_j, h_k in zip(h[j:], gk))
        if i >= k:
            gk.popleft()
        gk.append(g)
    return g

print((g(N, K, h)))


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

def g(n, k, h):
    g = abs(h[1] - h[0])
    gk = deque([0, g], k)
    for i in range(2, n):
        h_i = h[i]
        j = max(i - k, 0)
        g = min(abs(h_i - h_j) + h_k for h_j, h_k in zip(h[j:i], gk))
        if i >= k:
            gk.popleft()
        gk.append(g)
    return g

print((g(N, K, h)))
# Original solution
N, K = list(map(int, input().split()))
hList = list(map(int, input().split()))
ans = 0
dp = [0] * N
dp[0] = 0

for i in range(1, N):
    minT = 1000000000
    for j in range(i - 1, i - K - 1, -1):
        tmp = dp[j] + abs(hList[j] - hList[i])
        minT = min(minT, tmp)
        if j == 0:
            break
    dp[i] = minT

print((dp[N - 1]))

# Optimized solution
INF = 10 ** 9
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):
    for j in range(K):
        if i + j < N:
            dp[i + j] = min(dp[i + j], dp[i - 1] + abs(h[i - 1] - h[i + j]))

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

# Optimized solution
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, K = (int(i) for i in input().split())
    h = [0] + [int(i) for i in input().split()]
    memo = [float('inf')] * (N+1)

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

    print((memo[N]))


if __name__ == '__main__':
    main()
# Original solution
# coding: utf-8

n, k = list(map(int, input().rstrip().split()))    # 2 <= n <= 10^5, 1 <= k <= 100
h = list(map(int, input().rstrip().split()))

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


# Optimized solution
# coding: utf-8

def f(n, k, h):
    dp = [0 for _ in range(n)]
    for i in range(1, n):
        js = max(i - k, 0)
        dp[i] = min(dp[j] + abs(h[i] - h[j]) \
            for j in range(js, i))
        # print(dp)
    return(dp[i])

n, k = list(map(int, input().rstrip().split()))    # 2 <= n <= 10^5, 1 <= k <= 100
h = list(map(int, input().rstrip().split()))

print((f(n, k, h)))
# Original solution
# coding: utf-8

def f(n, k, h):
    dp = [float('inf') for _ in range(n)]
    dp[0] = 0
    for i in range(1, n):
        js = max(i - k, 0)
        for j in range(js, i):
            dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))
    return(dp[i])

n, k = list(map(int, input().rstrip().split()))    # 2 <= n <= 10^5, 1 <= k <= 100
h = list(map(int, input().rstrip().split()))

print((f(n, k, h)))


# Optimized solution
# coding: utf-8

def f(n, k, h):
    dp = [float('inf') for _ in range(n)]
    dp[0] = 0
    for i in range(1, n):
        js = max(i - k, 0)
        dp[i] = min(dp[j] + abs(h[i] - h[j]) \
            for j in range(js, i))
    return(dp[i])

n, k = list(map(int, input().rstrip().split()))    # 2 <= n <= 10^5, 1 <= k <= 100
h = list(map(int, input().rstrip().split()))

print((f(n, k, h)))
# Original solution
# coding: utf-8

n, k = list(map(int, input().rstrip().split()))    # 2 <= n <= 10^5, 1 <= k <= 100
h = list(map(int, input().rstrip().split()))

dp = [float('inf') for _ in range(n)]
dp[0] = 0
for i in range(1, n):
    js = max(i - k, 0)
    dp[i] = min(dp[j] + abs(h[i] - h[j]) \
        for j in range(js, i))
print((dp[i]))


# Optimized solution
# coding: utf-8
import sys
input = sys.stdin.readline

def f(n, k, h):
    dp = [0] * n
    for i in range(1, n):
        js = max(i - k, 0)
        dp[i] = min(dp[j] + abs(h[i] - h[j]) \
            for j in range(js, i))
    return(dp[i])

n, k = list(map(int, input().rstrip().split()))    # 2 <= n <= 10^5, 1 <= k <= 100
h = list(map(int, input().rstrip().split()))

print((f(n, k, h)))
# Original solution
#!/usr/bin/env python3
# vim: set fileencoding=utf-8

# pylint: disable=unused-import, invalid-name, missing-docstring, bad-continuation


"""Module docstring
"""

import sys
from collections import deque


def solve_samiz(values, nb, max_jump_size):
    dp = [10 ** 9] * nb
    dp[0] = 0
    for i in range(1, nb):
        j = max(0, i - max_jump_size)
        hi = values[i]
        dp[i] = min(dpj + abs(hi - hj) for dpj, hj in zip(dp[j:i], values[j:i]))
    return dp[-1]


def solve(values, nb, max_jump_size):
    max_jump_size = min(nb, max_jump_size)
    cost_to = [0]
    for i in range(1, nb):
        j = min(i, max_jump_size)
        value_i = values[i]
        new_cost = min(
            cost_to[i - k] + abs(value_i - values[i - k]) for k in range(1, j + 1)
        )
        cost_to.append(new_cost)
    return new_cost


def do_job():
    "Do the work"
    # first line is number of test cases
    N, K = list(map(int, input().split()))
    values = list(map(int, input().split()))
    result = solve(values, N, K)
    print(result)


def print_output(testcase, result) -> None:
    "Formats and print result"
    if result is None:
        result = "IMPOSSIBLE"
    print(("Case #{}: {}".format(testcase + 1, result)))


def main(argv=None):
    "Program wrapper."
    if argv is None:
        argv = sys.argv[1:]
    do_job()
    return 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    sys.exit(main())


# Optimized solution
#!/usr/bin/env python3
# vim: set fileencoding=utf-8

# pylint: disable=unused-import, invalid-name, missing-docstring, bad-continuation


"""Module docstring
"""

import sys


def solve(values, nb, max_jump_size):
    max_jump_size = min(nb, max_jump_size)
    cost_to = [0]
    for i in range(1, nb):
        j = max(0, i - max_jump_size)
        value_i = values[i]
        new_cost = min(
            prev_cost + abs(value_i - prev_value)
            for prev_cost, prev_value in zip(cost_to[j:i], values[j:i])
        )
        cost_to.append(new_cost)
    return new_cost


def do_job():
    "Do the work"
    # first line is number of test cases
    N, K = list(map(int, input().split()))
    values = list(map(int, input().split()))
    result = solve(values, N, K)
    print(result)


def print_output(testcase, result) -> None:
    "Formats and print result"
    if result is None:
        result = "IMPOSSIBLE"
    print(("Case #{}: {}".format(testcase + 1, result)))


def main(argv=None):
    "Program wrapper."
    if argv is None:
        argv = sys.argv[1:]
    do_job()
    return 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    sys.exit(main())
# Original solution
N,k = list(map(int,input().split()))
l = list(map(int,input().split()))
cost = [0] * N

for i in range(N):
    temp = []
    for j in range(max(0,i-k),i):
        delta = cost[j]+abs(l[i]-l[j])
        temp.append(delta)
        cost[i] = min(temp)
print((cost[N-1]))

# Optimized solution
N,k = list(map(int,input().split()))
l = list(map(int,input().split()))
cost = [0] * N

for i in range(1,N):
    temp = []
    cost[i] = min([cost[j]+abs(l[i]-l[j]) for j in range(max(0,i-k),i)])
print((cost[N-1]))
# Original solution
#import numpy
[n,k] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
dp = [0]
i_ls = []
now = 0
#dpのi番目の要素がi+1番目の足場までの最小コスト
for i in range(1,n):
    #i番目の足場
    if i < k:
        for j in range(i):
            i_ls.append(abs(a[i]-a[j])+dp[j])
        #print("i<k")
    else:
        for j in range(i-k,i):
            i_ls.append(abs(a[i]-a[j])+dp[j])
        #print("i>=k")
    dp.append(min(i_ls))
    #print(i_ls)
    i_ls = []

print((dp[n-1]))


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

"""
初期条件
dp[0] = 0
dp[1] = h[1] - h[0]
"""

dp = [10 ** 9 + 7] * n
dp[0] = 0
dp[1] = abs(h[1] - h[0])
if n > 2:
    for i in range(2,n):
        for j in range(1,min(i - 0,k)+1):
            dp[i] =min(dp[i - j] + abs(h[i]-h[i - j]),dp[i])
    print((dp[n-1]))
else:
    print((dp[1]))
# Original solution
[n,k] = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

"""
初期条件
dp[0] = 0
dp[1] = h[1] - h[0]
"""

dp = [10 ** 9 + 7] * n
dp[0] = 0
dp[1] = abs(h[1] - h[0])
if n > 2:
    for i in range(2,n):
        for j in range(1,min(i - 0,k)+1):
            dp[i] =min(dp[i - j] + abs(h[i]-h[i - j]),dp[i])
    print((dp[n-1]))
else:
    print((dp[1]))


# Optimized solution
[n,k] = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
dp = [1000000000] * n
dp[0] = 0
for i in range(1,n):
    for j in range(1,min(k,i)+1):
        dp[i] = min(dp[i],dp[i-j] + abs(h[i] - h[i-j]))
print((dp[n-1]))
#print(dp)
# Original solution
import math
import sys
input = sys.stdin.readline
number, jump =input().rstrip().split()
number = int(number)
jump = int(jump)
scaffolding = input().rstrip().split()
scaffolding = list(map(int, scaffolding))
res = [math.inf for i in range(number)]
res[0] =0 
test = [math.inf for k in range(jump)]
test[0] = 0

for i in range(number):
    for j in range(1,jump+1):
        if i-j < 0:break
        cost = abs(scaffolding[i]-scaffolding[i-j])
        test[j-1] = res[i-j] + cost
    
    res[i] = min(test)
    

print((res[number-1]))




# Optimized solution
import math
import sys
input = sys.stdin.readline
number, jump =input().rstrip().split()
number = int(number)
jump = int(jump)
scaffolding = input().rstrip().split()
scaffolding = list(map(int, scaffolding))
res = [0 for i in range(number)]
res[1] = abs(scaffolding[1]-scaffolding[0])
#test = [math.inf for k in range(jump)]
#test[0] = 0

for i in range(2, number):
    h=max(0,i-jump)
    res[i]=min(res[k]+abs(scaffolding[i]-scaffolding[k]) for k in range(h,i))   
    #res[i] = min(test)
    

print((res[number-1]))
# Original solution
n, k = list(map(int, input().split()))
h = list(map(int,input().split()))

if k > n:
    k = n-1

'''
n番目の場所に到達する時
n-1番目から、1つ飛んでくる
n-2番目から、２つ飛んでくる
...
n-K番目から、kつ飛んでくる

可能性がある　その時の最小値を考える

初期条件
dp[0] = 0
dp[1~Kまで]を計算

*** n-j
dp[n] = dp[n-j] + abs(h[n] - h[n-j])

漸化式
n >= 2
dp[n] = min(jump_list)

1~K
jump_list = [1 ~ j で dp[n] = dp[n-j] + abs(h[n] - h[n-j])]

k~N
jump_list = [1 ~ k で dp[n] = dp[n-j] + abs(h[n] - h[n-j])]

'''

#初期値
dp = [0]

def create_jump_list(j,i):
    alist = []
    for x in range(j, i):
        alist.append(
            dp[x] + abs(h[i] - h[x])
        )
    return alist

# kまで
for i in range(1, k+1):
    jump_list = create_jump_list(0,i)
    dp.append(
        min(jump_list)
    )

for i in range(k+1,n):
    jump_list = create_jump_list(i-k, i)
    dp.append(
        min(jump_list)
    )

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


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

if k > n:
    k = n-1

'''
n番目の場所に到達する時
n-1番目から、1つ飛んでくる
n-2番目から、２つ飛んでくる
...
n-K番目から、kつ飛んでくる

可能性がある　その時の最小値を考える

初期条件
dp[0] = 0
dp[1~Kまで]を計算

*** n-j
dp[n] = dp[n-j] + abs(h[n] - h[n-j])

漸化式
n >= 2
dp[n] = min(jump_list)

1~K
jump_list = [1 ~ j で dp[n] = dp[n-j] + abs(h[n] - h[n-j])]

k~N
jump_list = [j-k ~ j で dp[n] = dp[n-j] + abs(h[n] - h[n-j])]

'''

#初期値
dp = [0]

def create_jump_list(j,i):
    alist = []
    for x in range(j, i):
        alist.append(
            dp[x] + abs(h[i] - h[x])
        )
    return alist

# kまで
for i in range(1, k+1):
    jump_list = create_jump_list(0,i)
    dp.append(
        min(jump_list)
    )

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

# Optimized solution
import sys


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


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):
        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()
# Original solution
N,K = list(map(int,input().split(" ")))
H = list(map(int,input().split(" ")))
#DPテーブル作成
dp = [10**10]*(len(H)+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(H[i]-H[i+j]))

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

# Optimized solution
N,K = list(map(int,input().split(" ")))
H = list(map(int,input().split(" ")))
#DPテーブル作成
dp = [10**10]*(N)

dp[0]=0

for i,hi in enumerate(H):
    if i ==0:
        continue
    s = i-K if i-K>=0 else 0
    dp[i] = min([dpk+abs(hi-hk) for dpk,hk in zip(dp[s:i],H[s:i])])
print((dp[-1]))
#print(dp)
# 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
# DP(貰うDP)
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[N - 1]))


main()


# Optimized solution
# DP(貰うDP)
def main():
    from builtins import abs
    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[N - 1]))


main()
# Original solution
# DP(貰うDP)
def main():
    from builtins import abs, max, min, range
    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[N - 1]))


main()


# Optimized solution
def main():
    from builtins import abs
    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[N - 1]))


main()