Buggy python submission (verdict: Time Limit Exceeded):
N, K = 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])

Fixed submission:
N, K = 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])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = 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])

Fixed submission:
N, K = 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])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = 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])
            l.append(dp[i-k] + d)
    if len(l) > 0:
        dp[i] = min(l)
    else:
        dp[i] = 0

print(dp[-1])

Fixed submission:
N, K = 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])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = 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):
    l = [dp[i-k] + abs(H[i-k]-H[i]) for k in range(1, 1+K) if 0 <= i-k]
    if len(l) > 0:
        dp[i] = min(l)
    else:
        dp[i] = 0

print(dp[-1])

Fixed submission:
N, K = 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])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)

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

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

def func(i):
    if i == N:
        return 0
    else:
        dp[i] = min([dp[i-k]+abs(H[i-k]-H[i]) for k in range(1, K+1) if i-k>=0])
        func(i+1)

func(2)
print(dp[N-1])

Fixed submission:
N, K = 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])