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):
  dp[i] = dp[i-1]+abs(h[i]-h[i-1])
  L = min(K, i)
  for j in range(2, L+1):
    dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))

print(dp[N-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

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

memo = [0, abs(h[1]-h[0])]
def dp(n):
  for i in range(len(memo), n+1):
    costs = [memo[i-j]+abs(h[i]-h[i-j]) for j in range(1, min(i, K)+1)]
    memo.append(min(costs))
  return memo[n]

if __name__ == '__main__':
  print(dp(N-1))

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

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

memo = [0, abs(h[1]-h[0])]
def dp(n):
  for i in range(len(memo), n+1):
    costs = [memo[i-j]+abs(h[i]-h[i-j]) for j in range(1, min(i, K)+1)]
    memo.append(min(costs))
  return memo[n]
 
print(dp(N-1))

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
H = list(map(int, input().split()))
C = [0]*N
for i in range(1, N):
  	j = max(0, i-K)
    C[i] = min([c + abs(H[i]-h) for c, h in zip(C[j:i], H[j:i])])
print(C[N-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

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

memo = [-1 for _ in range(N)]

def dp(n):
  if memo[n] != -1:
    return memo[n]
  elif n != 0:
    m = min([dp(n-i)+abs(h[n]-h[n-i]) for i in range(1, min(K, n)+1)])
    memo[n] = m
    return m
  else:
    memo[n] = 0
    return memo[n]

for n in range(N-1):
  dp(n)
print(dp(N-1))

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
h = list(map(int, input().split()))

memo = [-1 for _ in range(N)]

def dp(n):
  if memo[n] != -1:
    return memo[n]
  elif n != 0:
    m = min([dp(n-i)+abs(h[n]-h[n-i]) for i in range(1, min(K, n)+1)])
    memo[n] = m
    return m
  else:
    memo[n] = 0
    return memo[n]

print(dp(N-1))

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
c = [0]*N
for i in range(1, N):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])