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(N-1):
    if i >= K:
        x = dp[i]+abs(h[i+1]-h[i])
        for j in range(1,K):
            x = min(x,dp[i-j]+abs(h[i+1]-h[i-j]))
            
    else:
        x = dp[i]+abs(h[i+1]-h[i])
        for j in range(1,i+1):
            x = min(x,dp[i-j]+abs(h[i+1]-h[i-j]))
    
    dp[i+1] = x            



print(dp[-1])

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

dp = [0]*N

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



print(dp[-1])

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(N-1):
    if i >= K:
        x = dp[i]+abs(h[i+1]-h[i])
        for j in range(1,K):
            x = min(x,dp[i-j]+abs(h[i+1]-h[i-j]))
            
    else:
        x = dp[i]+abs(h[i+1]-h[i])
        for j in range(1,i+1):
            x = min(x,dp[i-j]+abs(h[i+1]-h[i-j]))
    
    dp[i+1] = x            



print(dp[-1])

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

dp = [0]*N

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



print(dp[-1])

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(N-1):
    if i >= K:
        for j in range(K):
            x = dp[i-j]+abs(h[i+1]-h[i-j])
            if dp[i+1] == 0:
                dp[i+1] = x
            elif x < dp[i+1]:
                dp[i+1] = x
    else:
        for j in range(i+1):
            x = dp[i-j]+abs(h[i+1]-h[i-j])
            if dp[i+1] == 0:
                dp[i+1] = x
            elif x < dp[i+1]:
                dp[i+1] = x            



print(dp[-1])

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

dp = [0]*N

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



print(dp[-1])

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(N-1):
    dp[i+1] = []
    if i >= K:
        for j in range(K):
            dp[i+1].append(dp[i-j]+abs(h[i+1]-h[i-j]))
    else:
        for j in range(i+1):
            dp[i+1].append(dp[i-j]+abs(h[i+1]-h[i-j]))

    dp[i+1] = min(dp[i+1])

print(dp[-1])

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

dp = [0]*N

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



print(dp[-1])