Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
dp = [float('inf')] * n
dp[0] = 0

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

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 = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

d = [0] * n

if k >= n:
    k = n-1


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

for i in range(n-k):
    d[i+k] = min(d[i+j]+abs(h[i+k]-h[i+j]) for j in range(k))

print(d[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())
h = list(map(int, input().split()))


d = [0] * n

if k >= n:
    k = (n - 1)





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

for i in range(n-k):
    d[i+k] = min([d[i+j]+abs(h[i+k]-h[i+j]) for j in range(k)])

print(d[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())
h = list(map(int, input().split()))

import numpy as np

d = [0] * n

if k >= n:
    k = (n - 1)

for i in range(1, k):
    d[i] = min([d[j]+np.abs(h[i]-h[j]) for j in range(i)])

for i in range(n-k):
    d[i+k] = min([d[i+j]+np.abs(h[i+k]-h[i+j]) for j in range(k)])

print(d[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())
h = list(map(int, input().split()))

import numpy as np

d = [0] * n

if k >= n:
    k = (n - 1)

for i in range(1, k):
    x = []
    for j in range(i):
        x.append(d[j]+np.abs(h[i]-h[j]))
    d[i] = min(x)

for i in range(n-k):
    x = []
    for j in range(k):
        x.append(d[i+j]+np.abs(h[i+k]-h[i+j]))
    d[i+k] = min(x)


print(d[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))