CodeEff/ECCO huggingface dataset:
# Original solution
INF = float("inf") #const

def main():
    n,k = list(map(int,input().split()))
    h = list(map(int,input().split()))
    dp = [INF]*n
    dp[0] = 0

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

    print((dp[n-1]))


if __name__ == "__main__":
    main()


# Optimized solution
INF = float("inf") #const

def main():
    n,k = list(map(int,input().split()))
    h = list(map(int,input().split()))
    dp = [INF]*n
    dp[0] = 0

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

    print((dp[n-1]))


if __name__ == "__main__":
    main()
# Original solution
INF = float("inf") #const

def main():
    n,k = list(map(int,input().split()))
    h = list(map(int,input().split()))
    dp = [INF]*n
    dp[0] = 0

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

    print((dp[n-1]))


if __name__ == "__main__":
    main()


# Optimized solution
INF = float("inf") #const
MOD = 10**9+7 #const
MAX = 510000 #const

def main():
    n,k = list(map(int,input().split()))
    h = list(map(int,input().split()))
    dp = [INF]*(n+1)
    dp[0] = 0

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


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

    for j in range(1, n):
        buf = [dp[i] + abs(s[j]-s[i]) for i in range(max(0, j - k), j)]
        dp[j] = min(buf)
    print((dp[-1]))

if __name__ == "__main__":
    main()


# Optimized solution
n, k = list(map(int, input().split()))
s = list(map(int, input().split()))
inf = 10 ** 8
dp = [inf] * n
dp[0] = 0

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

def dp(cost):
    for x in range(1,N):
        cost[x] =  min( [ cost[x-k-1] + abs( h[x] - h[x-k-1] ) for k in range(0,min(x,K)) ] ) 
    return 0

def solve():
    cost = [float('inf')] * N
    cost[0] = 0
    dp(cost)
    print((cost[N-1]))
    return 0

if __name__ == "__main__":
    solve()

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

def dp(cost):
    for x in range(1,N):
        cost[x] =  min( [ cost[k] + abs( h[x] - h[k] ) for k in range(max(x-K,0),x) ] ) 
    return 0

def solve():
    cost = [float('inf')] * N
    cost[0] = 0
    dp(cost)
    print((cost[N-1]))
    return 0

if __name__ == "__main__":
    solve()
# Original solution
n,k = list(map(int,input().split()))
h_ls = 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,k+1):
        if i + j < n:
            dp[i+j] = min(dp[i+j], dp[i] + abs(h_ls[i+j] - h_ls[i]))
print((dp[-1]))

# Optimized solution
n,k = list(map(int,input().split()))
h_ls = list(map(int, input().split()))
mincost_ls = [float('inf')] * n
mincost_ls[0] = 0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j < n:
            mincost_ls[j] = min(mincost_ls[j], mincost_ls[i] + abs(h_ls[j]-h_ls[i]))
print((mincost_ls[-1]))
# Original solution
n,k = list(map(int,input().split()))
h_ls = list(map(int, input().split()))
mincost_ls = [float('inf')] * n
mincost_ls[0] = 0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j < n:
            mincost_ls[j] = min(mincost_ls[j], mincost_ls[i] + abs(h_ls[j]-h_ls[i]))
print((mincost_ls[-1]))



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

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

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

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

INF = 100100100100100
dp = [INF]*N

dp[0] = 0

for i in range(1,N):
    for k in range(1,K+1):
        if i >= k:
            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()))

INF = 100100100100100
dp = [INF]*N

dp[0] = 0

for i in range(1,N):
    for k in range(1,K+1):
        if i < k:
            break
        dp[i] = min(dp[i], dp[i-k] + abs(h[i] - h[i-k]))
        
print((dp[N-1]))
# Original solution
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, K: int, h: "List[int]"):
    dp = [float('inf')] * N
    dp[0] = 0

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


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]"
    result = solve(N, K, h)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, K: int, h: "List[int]"):
    dp = [float('inf')] * N
    dp[0] = 0

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


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]"
    result = solve(N, K, h)
    print(result)

if __name__ == '__main__':
    main()
# Original solution
import math
import string
import collections
from collections import Counter
from collections import deque


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

n, k = list(map(int, input().split()))
h = readints()
h.append(-1)
dp = [10**9]*(2*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], abs(h[i]-h[i+j])+dp[i])
print((dp[n-1]))


# Optimized solution
import math
import string
import collections
from collections import Counter
from collections import deque


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

n, k = list(map(int, input().split()))
h = readints()
# h.append(-1)
dp = [10**9]*(2*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], abs(h[i]-h[i+j])+dp[i])
print((dp[n-1]))
# Original solution
import sys


def resolve():
    readline = sys.stdin.readline    # 1行だけ文字列にする

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

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

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

resolve()


# Optimized solution
def resolve():
    import sys
    
    readline = sys.stdin.readline

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

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

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

    print((dp[-1]))
            

resolve()
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
mem = [[-1 for _ in range(N)] for _ in range(N)]

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


for i in range(2, N):
    l = []
    for k in range(1, 1+K):
        if i-k >= 0:
            if mem[i-k][i] != -1:
                d = mem[i-k][i]
            else:
                d = abs(H[i - k] - H[i])
                mem[i-k][i] = d
            l.append(dp[i-k] + d)
    if len(l) > 0:
        dp[i] = min(l)        
    else:
        dp[i] = 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[1]-H[0])

for i in range(2, N):
    dp[i] = min([dp[i-k] + abs(H[i-k]-H[i]) for k in range(1, 1+K) if 0 <= i-k])

print((dp[-1]))
# Original solution
inf = 10**9 + 7
n, k = list(map(int, input().split()))

nodes = list(map(int, input().split()))
dp = [inf] * n

dp[0] = 0

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


# Optimized solution
inf = 10**9 + 7
n, k = list(map(int, input().split()))

nodes = list(map(int, input().split()))
dp = [inf] * n

dp[0] = 0

for i in range(n):
    for j in range(i+1, min(i+k+1, n)):
        dp[j] = min(dp[j], dp[i] + abs(nodes[i] - nodes[j]))
# for i in range(n):
#     for j in range(1, min(k+1, n-i)):
#         dp[i+j] = min(dp[i+j], dp[i]+abs(nodes[i]-nodes[i+j]))
print((dp[n-1]))
# Original solution
import math

def solve(a,n,k):
  dp = [0]*(n)
  dp[0]=0
  inf = 10**9
  for i in range(1,n):
    dp[i]=inf
    r=max(0,i-k)
    for j in range(r,i):
      dp[i]=min(dp[i],abs(a[i]-a[j])+dp[j])

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


# Optimized solution
import math
 
def solve(a,n,k):
  dp = [0]*(n)
  dp[0]=0
  inf = 10**9
  for i in range(1,n):
    dp[i]=inf
    r=max(0,i-k)
    dp[i]=min([abs(a[i]-a[j])+dp[j] for j in range(r,i)])
 
  #print(dp)
  return dp[-1]
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
print((solve(a,n,k)))
# Original solution
n,k = list(map(int,input().split()))
h = list(map(int, input().split()))

cnt = [float('inf')]*n
cnt[0], cnt[1] = 0, abs(h[0]-h[1])
for i in range(2, n):
    cnt[i] = min(abs(h[m]-h[i])+cnt[m] for m in range(max(0,i-k),i))
print((cnt[n-1]))


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

cnt = [0]*n
for i in range(1, n):
    j = max(0,i-k)
    cnt[i] = min(abs(h[m]-h[i])+cnt[m] for m in range(j,i))
print((cnt[n-1]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
 
dp = [1000000000] * N
dp[0] = 0
 
for i in range(N - 1):
    for j in range(1, K + 1):
        if i + j < N:
            if dp[i + j] > abs(h[i + j] - h[i]) + dp[i]:
                dp[i + j] = abs(h[i + j] - h[i]) + dp[i]
        else:
            break
 
print((dp[N - 1]))

# Optimized solution
N,K = list(map(int,input().split()))
h = list(map(int,input().split()))
dp = [10**15+1 for i in range(N)]
dp[0] = 0
for i in range(N):
    for j in range(1,K+1):
        if i+j<N:
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i]-h[i+j]))
        
print((dp[-1]))
# Original solution
import sys
input=sys.stdin.readline
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
dp=[0]*n
for i in range(1,n):
  kk=max(i-k,0)
  dp[i]=min(d+abs(hh-h[i])for d,hh in zip(dp[kk:i],h[kk:i]))
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]=dp[i-1]+abs(h[i-1]-h[i])
  for j in range(2,min(k+1,i+1)):
    dp[i]=min(dp[i],dp[i-j]+abs(h[i-j]-h[i]))
print((dp[-1]))
# Original solution
import sys
input=sys.stdin.readline
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
dp=[0]*n
for i in range(1,n):
  kk=max(i-k,0)
  dp[i]=min(d+abs(hh-h[i])for d,hh in zip(dp[kk:i],h[kk:i]))
print((dp[n-1]))


# Optimized solution
import sys
input=sys.stdin.readline
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
dp=[0]*n
for i in range(1,n):
  kk=i-k
  if kk<0:kk=0
  dp[i]=min(d+abs(hh-h[i])for d,hh in zip(dp[kk:i],h[kk:i]))
print((dp[n-1]))
# Original solution
n,k=list(map(int,input().split()))
c=list(map(int,input().split()))
dp=[0 for i in range(n)]
for i in range(1,n):
    dp[i]=min([dp[j]+abs(c[i]-c[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()))
    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
def main():
    n,k=list(map(int,input().split()))
    c=list(map(int,input().split()))
    dp=[0]*n
    dp[0]=0
    for i in range(1,n):
        dp[i]=min([dp[j]+abs(c[i]-c[j]) for j in range(max(0,i-k),i)])
    print((dp[-1]))
if __name__=='__main__':
    main()

# Optimized solution
n,k=list(map(int,input().split()))
c=list(map(int,input().split()))
dp=[0]*n
dp[0]=0
for i in range(1,n):
    dp[i]=min([dp[j]+abs(c[i]-c[j]) for j in range(max(0,i-k),i)])
print((dp[-1]))
# Original solution
n, k, *h = list(map(int, open(0).read().split()))
dp = [float('inf')] * n
dp[0] = 0
for i in range(1, n):
    min_i = max(0, i-k)
    dp[i] = min([dp[j] + abs(h[i] - h[j]) for j in range(min_i, i)])
print((dp[n-1]))

# Optimized solution
def main():
    n, k, *h = list(map(int, open(0).read().split()))
    dp = [float('inf')] * n
    dp[0] = 0
    for i in range(1, n):
        min_i = max(0, i-k)
        dp[i] = min([dp[j] + abs(h[i] - h[j]) for j in range(min_i, i)])
    print((dp[n-1]))
if __name__ == '__main__':
    main()
# Original solution
a,aa=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[0]*a
c[0]=0
for i in range(1,a):
  c[i] = min(c[j] + abs(b[i] - b[j]) for j in range(max(0,i-aa),i))
print((c[-1]))

# Optimized solution
def solve():
  a,aa=list(map(int,input().split()))
  b=list(map(int,input().split()))
  c=[0]*a
  c[0]=0
  for i in range(1,a):
    c[i] = min(c[j] + abs(b[i] - b[j]) for j in range(max(0,i-aa),i))
  print((c[-1]))
  
solve()
# Original solution
n,k = list(map(int, input().split()))
h = list(map(int, input().split()))

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

for i in range(n-1):
  for j in range(1,k+1):
    if i+j==n:
      break
    if dp[i+j] == -1:
      dp[i+j] = dp[i] + abs(h[i]-h[i+j])
    else:
      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 = list(map(int, input().split()))

dp = [0]*(n)
dp[0] = 0

for i in range(1, n):
  j = max(0, i-k)
  dp[i] = min([dpk + abs(h[i]-hk) for dpk, hk in zip(dp[j:i], h[j:i])])
  
print((dp[n-1]))
# Original solution
def main():
    import sys
    input = sys.stdin.readline

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

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

    dp[0] = 0
    steps = [inf] * K

    for i in range(1, N):
        steps = [inf] * K
        for j in range(1, min(i, K) + 1):
            steps[j-1] = abs(H[i-j]-H[i]) + dp[i-j]
        step = min(steps)

        dp[i] = min(dp[i], step)
    print((dp[-1]))


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    import sys
    input = sys.stdin.readline

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

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

    dp[0] = 0

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


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

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

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

edu_dp_a_frog2()

# Optimized solution
def edu_dp_a_frog2():
    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):
        dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1, min(i+1, k+1))])
        # 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]))

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

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

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

def main(N,K,H):
    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]))
main(N,K,H)
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

dp = [INF]*N
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)
print((dp[-1]))

# Optimized solution
import sys, re

def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')

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

dp = [INF]*N
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[-1]))
# Original solution
def main():
    n, k = list(map(int, input().split()))
    stones = tuple(map(int, input().split()))
    inf = 10**9
    dp = [inf]*n
    dp[0] = 0
    for i in range(n):
        for t in range(1, k+1):
            if i+t < n:
                v = dp[i]+abs(stones[i]-stones[i+t])
                if dp[i+t] > v:
                    dp[i+t] = v
    print((dp[-1]))


if __name__ == '__main__':    
    main()


# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    h = tuple(map(int, input().split()))
    INF = 10 ** 9
    dp = [INF] * n
    dp[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            i_j = i + j
            if i_j < n:
                x = dp[i] + abs(h[i] - h[i_j])
                if dp[i_j] > x:
                    dp[i_j] = x
    print((dp[-1]))


if __name__ == "__main__":
    main()