Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
def solve():
    global h, dp, K
    N, K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [float('inf')] * N

    dp[0] = 0
    print(rec(N-1))

def rec(i):
    if dp[i] != float('inf'):
        return dp[i]
    if i == 0:
        return 0
    
    res = float('inf')
    for j in range(1,K+1):
        if i-j < 0:
            break
        res = min(res, rec(i-j) + abs(h[i] - h[i-j]))
    
    dp[i] = res
    return res

if __name__ == '__main__':
    solve()


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

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, N):
        lst = []
        for k in range(1,i+1):
            if k > K:
                break
            lst.append(abs(h[i] - h[i-k]) + dp[i-k])
        
        lst.append(dp[i])
        dp[i] = min(*lst)
    
    print(dp[N-1])

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
def solve():
    global h, dp, K
    N, K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [float('inf')] * N

    dp[0] = 0
    print(rec(N-1))

def rec(i):
    if dp[i] != float('inf'):
        return dp[i]
    if i == 0:
        return 0
    
    res = float('inf')
    for j in range(1,K+1):
        if i-j < 0:
            break
        res = min(res, rec(i-j) + abs(h[i] - h[i-j]))
    
    dp[i] = res
    return res

if __name__ == '__main__':
    solve()


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

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, N):
        lst = []
        for k in range(1,i+1):
            if k > K:
                break
            lst.append(abs(h[i] - h[i-k]) + dp[i-k])
        
        lst.append(dp[i])
        dp[i] = min(*lst)
    
    print(dp[N-1])

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Runtime Error):
def solve():
    global h, dp, K
    N, K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [float('inf')] * N

    dp[0] = 0
    print(rec(N-1))

def rec(i):
    if dp[i] != float('inf'):
        return dp[i]
    if i == 0:
        return 0
    
    res = float('inf')
    for j in range(1,K+1):
        if i-j < 0:
            break
        res = min(res, rec(i-j) + abs(h[i] - h[i-j]))
    
    dp[i] = res
    return res

if __name__ == '__main__':
    solve()


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

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, N):
        lst = []
        for k in range(1,i+1):
            if k > K:
                break
            lst.append(abs(h[i] - h[i-k]) + dp[i-k])
        
        lst.append(dp[i])
        dp[i] = min(*lst)
    
    print(dp[N-1])

if __name__ == '__main__':
    solve()

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

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, N):
        lst = []
        for k in range(1,i+1):
            if k > K:
                break
            lst.append(abs(h[i] - h[i-k]) + dp[i-k])
        
        dp[i] = min(*lst, dp[i])
    
    print(dp[N-1])

if __name__ == '__main__':
    solve()


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

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, N):
        lst = []
        for k in range(1,i+1):
            if k > K:
                break
            lst.append(abs(h[i] - h[i-k]) + dp[i-k])
        
        lst.append(dp[i])
        dp[i] = min(*lst)
    
    print(dp[N-1])

if __name__ == '__main__':
    solve()