Buggy python submission (verdict: Time Limit Exceeded):
# N = int(input())


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]
INF = 10 ** 9
dp = [INF for i in range(N+1)]
dp[1] = 0
for j in range(1, N+1):
    for i in range(1, K+1):
        if j > i:
            dp[j] = min(dp[j], dp[j-i] + abs(h[j] - h[j-i]))

print(dp[N])




Fixed submission:
# N = int(input())


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]
INF = 10 ** 9
dp = [INF for i in range(N+1)]
dp[1] = 0
for j in range(1, N+1):
    for i in range(1, K+1):
        if j > i:
            dp[j] = min(dp[j], dp[j-i] + abs(h[j] - h[j-i]))

print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):
# N = int(input())
import sys
from functools import lru_cache

sys.setrecursionlimit(10000000)


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]


@lru_cache(maxsize=None)
def solve(n):
    if n <= 1:
        return 0
    min_cost = 10**9
    for i in range(1, K+1):
        if n > i:
            min_cost = min(min_cost, solve(n-i) + abs(h[n] - h[n-i]))
    return min_cost

print(solve(N))




Fixed submission:
# N = int(input())


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]
INF = 10 ** 9
dp = [INF for i in range(N+1)]
dp[1] = 0
for j in range(1, N+1):
    for i in range(1, K+1):
        if j > i:
            dp[j] = min(dp[j], dp[j-i] + abs(h[j] - h[j-i]))

print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):
# N = int(input())
import sys
from functools import lru_cache

sys.setrecursionlimit(10000000)


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]


@lru_cache(maxsize=None)
def solve(n):
    if n <= 1:
        return 0
    cost = []
    for i in range(1, K+1):
        if n > i:
            cost.append(solve(n-i) + abs(h[n] - h[n-i]))
    return min(cost)

print(solve(N))




Fixed submission:
# N = int(input())


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]
INF = 10 ** 9
dp = [INF for i in range(N+1)]
dp[1] = 0
for j in range(1, N+1):
    for i in range(1, K+1):
        if j > i:
            dp[j] = min(dp[j], dp[j-i] + abs(h[j] - h[j-i]))

print(dp[N])

Buggy python submission (verdict: Runtime Error):
# N = int(input())
import sys
from functools import lru_cache

sys.setrecursionlimit(100000)


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]


@lru_cache(maxsize=None)
def solve(n):
    if n <= 1:
        return 0
    cost = []
    for i in range(1, K+1):
        if n > i:
            cost.append(solve(n-i) + abs(h[n] - h[n-i]))
    return min(cost)

print(solve(N))




Fixed submission:
# N = int(input())


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]
INF = 10 ** 9
dp = [INF for i in range(N+1)]
dp[1] = 0
for j in range(1, N+1):
    for i in range(1, K+1):
        if j > i:
            dp[j] = min(dp[j], dp[j-i] + abs(h[j] - h[j-i]))

print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):
# N = int(input())
import sys
from functools import lru_cache

sys.setrecursionlimit(100000)


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]


@lru_cache(maxsize=None)
def solve(n):
    if n <= 1:
        return 0
    cost = []
    for i in range(1, K+1):
        if n > i:
            cost.append(solve(n-i) + abs(h[n] - h[n-i]))
    return min(cost)

print(solve(N))




Fixed submission:
# N = int(input())


N, K = [int(i) for i in input().split()]
h = [0] + [int(i) for i in input().split()]
INF = 10 ** 9
dp = [INF for i in range(N+1)]
dp[1] = 0
for j in range(1, N+1):
    for i in range(1, K+1):
        if j > i:
            dp[j] = min(dp[j], dp[j-i] + abs(h[j] - h[j-i]))

print(dp[N])