Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))

dp = [0 for _ in range(N)]
dp[1] = abs(H[1] - H[0])
for i in range(2, N):
    r = max(0, i - K)
    dp[i] = min(d + abs(H[i] - h) for d, h in zip(dp[r:i], H[r:i]))
print(dp[N-1])

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

dp = [0 for _ in range(N)]
for i, hi in enumerate(H):
    if i == 0:
        continue
    r = max(0, i - K)
    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))
print(dp[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))

dp = [0 for _ in range(N)]
for i in range(1, N):
    r = max(0, i - K)
    dp[i] = min(d + abs(H[i] - h) for d, h in zip(dp[r:i], H[r:i]))
print(dp[N-1])

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

dp = [0 for _ in range(N)]
for i, hi in enumerate(H):
    if i == 0:
        continue
    r = max(0, i - K)
    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))
print(dp[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))

dp = [0] * N
for i in range(1, N):
    r = max(0, i - K)
    dp[i] = min(d + abs(H[i] - h) for h, d in zip(H[r:i], dp[r:i]))
print(dp[N-1])

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

dp = [0 for _ in range(N)]
for i, hi in enumerate(H):
    if i == 0:
        continue
    r = max(0, i - K)
    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))
print(dp[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))

dp = [0 for _ in range(N)]
for i in range(1, N):
    r = max(0, i - K)
    dp[i] = min(d + abs(H[i] - h) for h, d in zip(H[r:i], dp[r:i]))
print(dp[N-1])

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

dp = [0 for _ in range(N)]
for i, hi in enumerate(H):
    if i == 0:
        continue
    r = max(0, i - K)
    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))
print(dp[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))

dp = [0]
for i in range(1, N):
    min_v = 1050000000
    for j in range(1, K+1):
        if i - j >= 0:
            min_v = min(min_v, dp[i - j] + abs(H[i] - H[i - j]))
    dp.append(min_v)
print(dp[N-1])

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

dp = [0 for _ in range(N)]
for i, hi in enumerate(H):
    if i == 0:
        continue
    r = max(0, i - K)
    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))
print(dp[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))

dp = [0 for _ in range(N)]
for i in range(1, N):
    min_v = 1050000000
    for j in range(1, K+1):
        if i - j >= 0:
            min_v = min(min_v, dp[i - j] + abs(H[i] - H[i - j]))
    dp[i] = min_v
print(dp[N-1])

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

dp = [0 for _ in range(N)]
for i, hi in enumerate(H):
    if i == 0:
        continue
    r = max(0, i - K)
    dp[i] = min(d + abs(hi - h) for d, h in zip(dp[r:i], H[r:i]))
print(dp[N-1])