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


N, K = map(int, input().split())
h = list(map(int, input().split()))
dp = [float('inf')] * N
dp[0] = 0


def rec(i):

    if dp[i] < float('inf'):
        return dp[i]

    res = float('inf')
    for j in range(1, K+1):
        if i >= j:
            res = min(res, rec(i - j) + abs(h[i] - h[i - j]))

    dp[i] = res
    return res


print(rec(N-1))

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

dp = [float('inf') for _ in range(N)]
dp[0] = 0

for i in range(N):
    for j in range(1, K+1):
        if i - j >= 0:
            if dp[i - j] + abs(h[i] - h[i - j]) < dp[i]:
                dp[i] = dp[i - j] + abs(h[i] - h[i - j])
print(dp[N-1])

Buggy python submission (verdict: Runtime Error):
import sys


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

sys.setrecursionlimit(N+2)
dp = [float('inf')] * N
dp[0] = 0


def rec(i):

    if dp[i] < float('inf'):
        return dp[i]

    res = float('inf')
    for j in range(1, K+1):
        if i >= j:
            res = min(res, rec(i - j) + abs(h[i] - h[i - j]))

    dp[i] = res
    return res


print(rec(N-1))


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

dp = [float('inf') for _ in range(N)]
dp[0] = 0

for i in range(N):
    for j in range(1, K+1):
        if i - j >= 0:
            if dp[i - j] + abs(h[i] - h[i - j]) < dp[i]:
                dp[i] = dp[i - j] + abs(h[i] - h[i - j])
print(dp[N-1])

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


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

sys.setrecursionlimit(N+2)
dp = [float('inf')] * N
dp[0] = 0


def rec(i):

    if dp[i] < float('inf'):
        return dp[i]

    res = float('inf')
    for j in range(1, K+1):
        if i >= j:
            res = min(res, rec(i - j) + abs(h[i] - h[i - j]))

    dp[i] = res
    return res


print(rec(N-1))

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

dp = [float('inf') for _ in range(N)]
dp[0] = 0

for i in range(N):
    for j in range(1, K+1):
        if i - j >= 0:
            if dp[i - j] + abs(h[i] - h[i - j]) < dp[i]:
                dp[i] = dp[i - j] + abs(h[i] - h[i - j])
print(dp[N-1])

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


N, K = map(int, input().split())
h = list(map(int, input().split()))
dp = [float('inf')] * N
dp[0] = 0


def rec(i):

    if dp[i] < float('inf'):
        return dp[i]

    res = float('inf')
    for j in range(1, K+1):
        if i >= j:
            res = min(res, rec(i - j) + abs(h[i] - h[i - j]))

    dp[i] = res
    return res


print(rec(N-1))

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

dp = [float('inf') for _ in range(N)]
dp[0] = 0

for i in range(N):
    for j in range(1, K+1):
        if i - j >= 0:
            if dp[i - j] + abs(h[i] - h[i - j]) < dp[i]:
                dp[i] = dp[i - j] + abs(h[i] - h[i - j])
print(dp[N-1])

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


N, K = map(int, input().split())
h = list(map(int, input().split()))
dp = [float('inf')] * N
dp[0] = 0


def rec(i):

    if dp[i] < float('inf'):
        return dp[i]

    res = float('inf')
    for j in range(1, K+1):
        if i >= j:
            res = min(res, rec(i - j) + abs(h[i] - h[i - j]))

    dp[i] = res
    return res


print(rec(N-1))

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

dp = [float('inf') for _ in range(N)]
dp[0] = 0

for i in range(N):
    for j in range(1, K+1):
        if i - j >= 0:
            if dp[i - j] + abs(h[i] - h[i - j]) < dp[i]:
                dp[i] = dp[i - j] + abs(h[i] - h[i - j])
print(dp[N-1])

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

dp = [float('inf') for _ in range(N)]
dp[0] = 0

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

dp = [float('inf') for _ in range(N)]
dp[0] = 0

for i in range(N):
    for j in range(1, K+1):
        if i - j >= 0:
            if dp[i - j] + abs(h[i] - h[i - j]) < dp[i]:
                dp[i] = dp[i - j] + abs(h[i] - h[i - j])
print(dp[N-1])