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

INF = float('inf')
DP = [INF for i in range(N)]
#DP[0] = 0


for i in range(N):
    candidate = [INF for i in range(K)]
    if i == 1:
        DP[i] = abs(stage[i] - stage[i-1])
    elif i > 1:
        for j in range(min(K+1,i+1)):
            if i == j:
                candidate[0] = (abs(stage[i]-stage[0]))
            else:
                candidate[K-1-j]=(DP[i-j] + abs(stage[i]-stage[i-j]))
        DP[i] = min(candidate)

print(DP[N-1])

Fixed submission:
N, K = map(int,input().split(' '))
stage = list(map(int, input().split(" ")))

INF = float('inf')
DP = [INF for i in range(N)]
#DP[0] = 0


for i in range(N):
    candidate = [INF for i in range(K)]
    if i == 1:
        DP[i] = abs(stage[i] - stage[i-1])
    elif i > 1:
        for j in range(min(K+1,i+1)):
            if i == j:
                candidate[0] = (abs(stage[i]-stage[0]))
            else:
                candidate[K-1-j]=(DP[i-j] + abs(stage[i]-stage[i-j]))
        DP[i] = min(candidate)

print(DP[N-1])

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

INF = float('inf')
DP = [INF for i in range(N)]
#DP[0] = 0
candidate = []

for i in range(N):
    if i == 1:
        DP[i] = abs(stage[i] - stage[i-1])
    elif i > 1:
        for j in range(min(K+1,i+1)):
            if i == j:
                candidate.append(abs(stage[i]-stage[0]))
            else:
                candidate.append(DP[i-j] + abs(stage[i]-stage[i-j]))
        DP[i] = min(candidate)
        candidate.clear()

print(DP[N-1])

Fixed submission:
N, K = map(int,input().split(' '))
stage = list(map(int, input().split(" ")))

INF = float('inf')
DP = [INF for i in range(N)]
#DP[0] = 0


for i in range(N):
    candidate = [INF for i in range(K)]
    if i == 1:
        DP[i] = abs(stage[i] - stage[i-1])
    elif i > 1:
        for j in range(min(K+1,i+1)):
            if i == j:
                candidate[0] = (abs(stage[i]-stage[0]))
            else:
                candidate[K-1-j]=(DP[i-j] + abs(stage[i]-stage[i-j]))
        DP[i] = min(candidate)

print(DP[N-1])

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

INF = float('inf')
DP = [INF for i in range(N)]
#DP[0] = 0
candidate = []

for i in range(N):
    if i == 1:
        DP[i] = abs(stage[i] - stage[i-1])
    elif i > 1:
        for j in range(min(K+1,i+1)):
            if i == j:
                candidate.append(abs(stage[i]-stage[0]))
            else:
                candidate.append(DP[i-j] + abs(stage[i]-stage[i-j]))
        DP[i] = min(candidate)
        candidate.clear()

print(DP[N-1])

Fixed submission:
N, K = map(int,input().split(' '))
stage = list(map(int, input().split(" ")))

INF = float('inf')
DP = [INF for i in range(N)]
#DP[0] = 0


for i in range(N):
    candidate = [INF for i in range(K)]
    if i == 1:
        DP[i] = abs(stage[i] - stage[i-1])
    elif i > 1:
        for j in range(min(K+1,i+1)):
            if i == j:
                candidate[0] = (abs(stage[i]-stage[0]))
            else:
                candidate[K-1-j]=(DP[i-j] + abs(stage[i]-stage[i-j]))
        DP[i] = min(candidate)

print(DP[N-1])