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):
    hi=h[i]
    dp[i] = min(dp[ii] + abs(hi - h[ii]) for ii in range(max(0, i - k), i))
print(dp[-1])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(k)]
for i, hi in enumerate(h):
    if i == 0: continue
    (cs, hs) = (k - i, 0) if k > i else (0, i - k)
    new_cost = min([ck + abs(hi - hk) for ck, hk in zip(cost[cs:], h[hs:])])
    cost = cost[1:] + [new_cost]
print(cost[k - 1])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(k)]
for i in range(1, n):
    (cs, hs) = (k - i, 0) if k > i else (0, i - k)
    new_cost = min([ck + abs(h[i] - hk) for ck, hk in zip(cost[cs:], h[hs:])])
    cost = cost[1:] + [new_cost]
print(cost[k - 1])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
k = min(n - 1, k)
h = [0] + list(map(int, input().split())) + [0 for _ in range(k - 2)]
cost = [0 for _ in range(n + 1)]
for i in range(2, n + 1):
    cost_i = []
    for di in range(1, k + 1):
        cost_i += [cost[i - di] + abs(h[i] - h[i - di])]
    cost[i] = min(cost_i)

print(cost[n])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])

Buggy python submission (verdict: Time Limit Exceeded):
inf = 10 ** 9
n, k = map(int, input().split())
h = [0] + list(map(int, input().split()))
cost = [inf for _ in range(n + 1)]
cost[1] = 0
for i in range(1, n):
    for di in range(1, k + 1):
        if i + di > n: break
        cost[i + di] = min(cost[i] + abs(h[i + di] - h[i]), cost[i + di])
print(cost[n])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k,*h = map(int, open(0).read().split())
h=[0]+h
cost = {1: 0, 2: abs(h[2] - h[1])}
for i in range(1, n):
    for di in range(1, k + 1):
        if i + di > n: break
        nc = cost[i] + abs(h[i + di] - h[i])
        if i + di in cost:
            if nc < cost[i + di]:
                cost[i + di] = nc
        else:
            cost[i + di] = nc
print(cost[n])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])

Buggy python submission (verdict: Runtime Error):
n, k,*h = map(int, open(0).read().split())
cost = {1: 0, 2: abs(h[2] - h[1])}
for i in range(1, n):
    for di in range(1, k + 1):
        if i + di > n: break
        nc = cost[i] + abs(h[i + di] - h[i])
        if i + di in cost:
            if nc < cost[i + di]:
                cost[i + di] = nc
        else:
            cost[i + di] = nc
print(cost[n])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = [0] + list(map(int, input().split()))
cost = {1: 0, 2: abs(h[2] - h[1])}
for i in range(1, n):
    for di in range(1, k + 1):
        if i + di > n: break
        nc = cost[i] + abs(h[i + di] - h[i])
        if i + di in cost:
            if nc < cost[i + di]:
                cost[i + di] = nc
        else:
            cost[i + di] = nc
print(cost[n])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = [0] + list(map(int, input().split()))
cost = {1: 0, 2: abs(h[2] - h[1])}
for i in range(1, n):
    for di in range(1, k + 1):
        if i + di > n: break
        if i + di in cost:
            cost[i + di] = min(cost[i + di], cost[i] + abs(h[i + di] - h[i]))
        else:
            cost[i + di] = cost[i] + abs(h[i + di] - h[i])
print(cost[n])


Fixed submission:
n, k = map(int, input().split())
k = min(n - 1, k)
h = list(map(int, input().split()))
cost = [0 for _ in range(n)]
for i, hi in enumerate(h):
    if i == 0: continue
    st = i - k if i > k else 0
    cost[i] = min([ck + abs(hi - hk) for ck, hk in zip(cost[st:i], h[st:i])])
print(cost[-1])