Buggy python submission (verdict: Time Limit Exceeded):
# 配るDPで解く
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [float('inf')]*N

dp[0] = 0
# 配っていく
for i in range(N):
    for j in range(i+1, min(i+1+K, N)):
        dp[j] = min(dp[j], dp[i]+abs(H[j]-H[i]))
print(dp[N-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]


def frog(n, k, h):
    d = [float('inf')] * n
    d[0] = 0

    for i in range(1, n):
        d[i] = min(
            d[j] + abs(h[i] - h[j]) for j in range(max(i - k, 0), i)
        )
    return d[n - 1]


print(frog(n, k, h))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())  # N個の足場, K段飛ぶことができる

H = list(map(int, input().split()))

dp = [1e+9]*N
dp[0] = 0
dp[1] = abs(H[1]-H[0])

for i in range(2, N):
    # Kステップ前まで考慮する必要
    for j in range(1, min(i, K)+1):
        dp[i] = min(dp[i], dp[i-j]+abs(H[i]-H[i-j]))

print(dp[-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]


def frog(n, k, h):
    d = [float('inf')] * n
    d[0] = 0

    for i in range(1, n):
        d[i] = min(
            d[j] + abs(h[i] - h[j]) for j in range(max(i - k, 0), i)
        )
    return d[n - 1]


print(frog(n, k, h))

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

dp = [1000000000] * (N+K+2)
HHH.extend([0]*(K+2))

dp[0] = 0
"配るdpでやる？"

for i in range(0, N):
    if N-i > K:  # もうK段残ってないなら
        K = N-i-K
    for j in range(1, K+1):  # 1段~K段飛ぶ
        dp[i+j] = min(dp[i+j], dp[i]+abs(HHH[i+j]-HHH[i]))

print(dp[N-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]


def frog(n, k, h):
    d = [float('inf')] * n
    d[0] = 0

    for i in range(1, n):
        d[i] = min(
            d[j] + abs(h[i] - h[j]) for j in range(max(i - k, 0), i)
        )
    return d[n - 1]


print(frog(n, k, h))

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

dp = [1000000000] * (N+K+2)
HHH.extend([0]*(K+2))

dp[0] = 0
"配るdpでやる？"

for i in range(0, N):
    if N-i > K:  # もうK段残ってないなら
        K = N-1-K
    for j in range(1, K+1):  # 1段~K段飛ぶ
        dp[i+j] = min(dp[i+j], dp[i]+abs(HHH[i+j]-HHH[i]))

print(dp[N-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]


def frog(n, k, h):
    d = [float('inf')] * n
    d[0] = 0

    for i in range(1, n):
        d[i] = min(
            d[j] + abs(h[i] - h[j]) for j in range(max(i - k, 0), i)
        )
    return d[n - 1]


print(frog(n, k, h))

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

dp = [1000000000] * (N+K+2)
HHH.extend([0]*(K+2))

dp[0] = 0
"配るdpでやる？"

for i in range(0, N):
    for j in range(1, K+1):  # 1段~K段飛ぶ
        dp[i+j] = min(dp[i+j], dp[i]+abs(HHH[i+j]-HHH[i]))

print(dp[N-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]


def frog(n, k, h):
    d = [float('inf')] * n
    d[0] = 0

    for i in range(1, n):
        d[i] = min(
            d[j] + abs(h[i] - h[j]) for j in range(max(i - k, 0), i)
        )
    return d[n - 1]


print(frog(n, k, h))