Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split())
h = list(map(int,input().split()))
dp = [0]*(n)
for i in range(1, min(n, k+1)) :
    dp[i] = abs(h[0]-h[i])
for i in range(k+1, len(dp)) :
    minv = 10**9+7
    for j in range(1, k+1) :
        minv = min(minv, dp[i-j] + abs(h[i-j]-h[i]))
    dp[i] = minv

print(dp[n-1])


Fixed submission:
N,K = map(int,input().split())

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

dp = [0 for i in range(N)]
if N >= K :    
    for i in range(1,K+1) :
        dp[i] = abs(L[i]-L[0])
    for i in range(K+1,len(dp)) :
        state = float("inf")
        for j in range(i-K,i) :
            calc = abs(L[i]-L[j]) + dp[j]
            if state > calc :
                state = calc
        dp[i] = state
    print(dp[N-1])
else :
    print(abs(min(L[1:])-L[0]))

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

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

for i in range(1,K+1) :
    dp[i] = abs(H[0] - H[i])

for i in range(K+1,N) :
    cost = float("inf")
    for j in range(1,K+1) :
        cost = min(cost, dp[i-j] + abs(H[i-j] - H[i]))
    
    dp[i] = cost

ans = dp[N-1]

print(ans)


Fixed submission:
N,K = map(int,input().split())

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

dp = [0 for i in range(N)]
if N >= K :    
    for i in range(1,K+1) :
        dp[i] = abs(L[i]-L[0])
    for i in range(K+1,len(dp)) :
        state = float("inf")
        for j in range(i-K,i) :
            calc = abs(L[i]-L[j]) + dp[j]
            if state > calc :
                state = calc
        dp[i] = state
    print(dp[N-1])
else :
    print(abs(min(L[1:])-L[0]))

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

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

if N <= K :
    ans = abs(L[0] - L[-1])
else :
    for i in range(1,K+1) :
        dp[i] = abs(L[0] - L[i])
    for i in range(K+1,N) :
        cost = float("inf")
        for j in range(i-K,i) :
            calc = dp[j] + abs(L[j] - L[i])
            if cost > calc :
                cost = calc
        dp[i] = cost
    ans = dp[N-1]
print(ans)


Fixed submission:
N,K = map(int,input().split())

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

dp = [0 for i in range(N)]
if N >= K :    
    for i in range(1,K+1) :
        dp[i] = abs(L[i]-L[0])
    for i in range(K+1,len(dp)) :
        state = float("inf")
        for j in range(i-K,i) :
            calc = abs(L[i]-L[j]) + dp[j]
            if state > calc :
                state = calc
        dp[i] = state
    print(dp[N-1])
else :
    print(abs(min(L[1:])-L[0]))

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

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

dp = [0 for i in range(N)]
if N >= K :    
    for i in range(1,K+1) :
        dp[i] = abs(L[i]-L[0])
    for i in range(K+1,len(dp)) :
        state = float("inf")
        for j in range(i-K,i) :
            calc = abs(L[i]-L[j]) + dp[j]
            if state > calc :
                state = calc
        dp[i] = state
    print(dp[N-1])
else :
    print(abs(min(L[1:])-L[0]))


Fixed submission:
N,K = map(int,input().split())

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

dp = [0 for i in range(N)]
if N >= K :    
    for i in range(1,K+1) :
        dp[i] = abs(L[i]-L[0])
    for i in range(K+1,len(dp)) :
        state = float("inf")
        for j in range(i-K,i) :
            calc = abs(L[i]-L[j]) + dp[j]
            if state > calc :
                state = calc
        dp[i] = state
    print(dp[N-1])
else :
    print(abs(min(L[1:])-L[0]))