Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [0] * N

for i in range(1, N):
    if i - K < 0:
        dp[i] = min(dpk + abs(h[i]-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(h[i]-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Fixed submission:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [0] * N

for i, hi in enumerate(h):
    if i == 0:
        continue
    if i - K < 0:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
init = 10**9
dp = [init] * N
dp[0] = 0

for i in range(1, N):
    if i - K < 0:
        dp[i] = min(dpk + abs(h[i]-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(h[i]-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])


Fixed submission:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [0] * N

for i, hi in enumerate(h):
    if i == 0:
        continue
    if i - K < 0:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for _ in input().split()]
init = 10**9
dp = [init] * N
dp[0] = 0

for i in range(1, N):
    if i - K < 0:
        dp[i] = min(dpk + abs(h[i]-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(h[i]-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Fixed submission:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [0] * N

for i, hi in enumerate(h):
    if i == 0:
        continue
    if i - K < 0:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = list(map(int, input().split()))
init = 10**9
dp = [init] * N
dp[0] = 0

for i in range(1, N):
    if i - K < 0:
        dp[i] = min(dpk + abs(h[i]-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(h[i]-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Fixed submission:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [0] * N

for i, hi in enumerate(h):
    if i == 0:
        continue
    if i - K < 0:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = list(map(int, input().split()))
init = (10 ** 4) * N
dp = [init] * N
dp[0] = 0
 
for i, h_i in enumerate(h):
    if i == 0:
        continue
    Resmin = init
    for j in range(1, K+1):
        if i - j < 0:
            break
        tmp = dp[i-j] + abs(h_i - h[i-j])
        if tmp < Resmin:
            Resmin = tmp
    dp[i] = Resmin
     
print(dp[-1])

Fixed submission:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [0] * N

for i, hi in enumerate(h):
    if i == 0:
        continue
    if i - K < 0:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = list(map(int, input().split()))
init = (10 ** 4) * N
dp = [init] * N
dp[0] = 0
 
for i in range(1, N):
    min = init
    for j in range(1, K+1):
        if i - j < 0:
            break
        tmp = dp[i-j] + abs(h[i] - h[i-j])
        if tmp < min:
            min = tmp
    dp[i] = min
     
print(dp[N-1])

Fixed submission:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [0] * N

for i, hi in enumerate(h):
    if i == 0:
        continue
    if i - K < 0:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int, input().split()))
init = (10 ** 4) * N
dp = [init] * N
dp[0] = 0

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

print(dp[N-1])

Fixed submission:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(x) for x in input().split()]
dp = [0] * N

for i, hi in enumerate(h):
    if i == 0:
        continue
    if i - K < 0:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[:i], h[:i]))
    else:
        dp[i] = min(dpk + abs(hi-hk) for dpk, hk in zip(dp[i-K:i], h[i-K:i]))

print(dp[-1])