Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    readline = sys.stdin.readline

    inf = 10 ** 9

    n, k = map(int, readline().split())
    *h, = map(int, readline().split())
    h = [inf] + h + [inf] * (k - 1)  # 1-indexed化

    dp = [inf] * (n + k)
    dp[0] = 0
    dp[1] = 0

    for i in range(1, n):  # ジャンプ元,1-indexed
        for j in range(1, k + 1):
            dp[i + j] = min(
                dp[i + j],
                dp[i] + abs(h[i + j] - h[i]))

    print(dp[n])


if __name__ == '__main__':
    main()


Fixed submission:
INF = 10 ** 10

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

dp = [INF for _ in range(N)]
dp[0] = 0
# 0-indexed

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

print(dp[N - 1])

Buggy python submission (verdict: Time Limit Exceeded):
inf = float('inf')

n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [inf] * n
dp[0] = 0

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

print(dp[n - 1])


Fixed submission:
INF = 10 ** 10

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

dp = [INF for _ in range(N)]
dp[0] = 0
# 0-indexed

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

print(dp[N - 1])

Buggy python submission (verdict: Time Limit Exceeded):
INF = float('inf')

N, K = map(int, input().split())
h = list(map(int, input().split()))
h = [0] + h

dp = [INF for i in range(N + 1)]
# 1-indexed, 1 -> N
dp[1] = 0

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


Fixed submission:
INF = 10 ** 10

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

dp = [INF for _ in range(N)]
dp[0] = 0
# 0-indexed

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

print(dp[N - 1])

Buggy python submission (verdict: Time Limit Exceeded):
INF = 10 ** 10

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

dp = [INF for _ in range(N)]
dp[0] = 0
# 0-indexed

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

print(dp[N - 1])


Fixed submission:
INF = 10 ** 10

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

dp = [INF for _ in range(N)]
dp[0] = 0
# 0-indexed

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

print(dp[N - 1])

Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
#include <algorithm>
#include <cstdlib>
#include <math.h>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }

  int INF = pow(10, 9);

  vector<int> dp(N, INF);
  dp[0] = 0;
  
  for (int j = 1; j < N; j++) {
  	for (int i = max(0, j - K); i < j; i++) {
        dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
    }
  }
  cout << dp[N - 1] << endl;
}


Fixed submission:
INF = 10 ** 10

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

dp = [INF for _ in range(N)]
dp[0] = 0
# 0-indexed

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

print(dp[N - 1])

Buggy python submission (verdict: Time Limit Exceeded):
INF = float('inf')

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

dp = [INF for _ in range(N)]  # dp[足場index - 1] = 最小コスト
dp[0] = 0

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

print(dp[N - 1])


Fixed submission:
INF = 10 ** 10

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

dp = [INF for _ in range(N)]
dp[0] = 0
# 0-indexed

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

print(dp[N - 1])