Buggy python submission (verdict: Time Limit Exceeded):
#atcoder https://atcoder.jp/contests/dp/tasks/dp_b

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


dp = [0] * N
dp[0] = 0

for i in range(1, N):

    dp[i] = min(
        dp[k] + abs(h[i]-h[k]) for k in range(max(i-K,0), i)
    )

print(dp[N-1])


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

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print(dp[N-1])
main()

Buggy python submission (verdict: Runtime Error):
#atcoder https://atcoder.jp/contests/dp/tasks/dp_b

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


dp = [0] * N
dp[0] = 0

for i in range(1, N):

    dp[i] = min(
        dp[k] + abs(h[i]-h[k]) for k in range(max(i-K,0), i)
    )

print(dp[N-1])


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

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print(dp[N-1])
main()

Buggy python submission (verdict: Time Limit Exceeded):
#atcoder https://atcoder.jp/contests/dp/tasks/dp_b

N, K = map(int, input().split())
h = [int(i) for i in input().split()]


dp = [0] * N
dp[0] = 0

for i in range(1, N):

    dp[i] = min(
        dp[k] + abs(h[i]-h[k]) for k in range(max(i-K,0), i)
    )

print(dp[N-1])


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

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print(dp[N-1])
main()

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = [int(i) for i in input().split()]


dp = [float("inf")] * N
dp[0] = 0

for i in range(1, N):

    dp[i] = min(
        [dp[i-k]+abs(h[i]-h[i-k]) for k in range(min(i+1, K+1))]
    )

print(dp[N-1])


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

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print(dp[N-1])
main()

Buggy python submission (verdict: Time Limit Exceeded):
#atcoder https://atcoder.jp/contests/dp/tasks/dp_b

N, K = map(int, input().split())
h = [int(i) for i in input().split()]


dp = [float("inf") for i in range(N)]
dp[0] = 0

for i in range(1, N):

    dp[i] = min(
        [dp[i-k]+abs(h[i]-h[i-k]) for k in range(min(i+1, K+1))]
    )

print(dp[N-1])


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

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print(dp[N-1])
main()

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = [int(i) for i in input().split()]


dp = [float("inf") for i in range(N)]
dp[0] = 0

for i in range(1, N):
    for k in range(1, min(i+1, K+1)):


        dp[i] = min(dp[i], dp[i-k]+abs(h[i]-h[i-k]))


print(dp[N-1])

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

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print(dp[N-1])
main()

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = [int(i) for i in input().split()]


dp = [float("inf") for i in range(N)]
dp[0] = 0

for i in range(1, N):
    for k in range(1, K+1):
        if i < k:
            continue

        dp[i] = min(dp[i], dp[i-k]+abs(h[i]-h[i-k]))


print(dp[N-1])

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

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print(dp[N-1])
main()