Buggy python submission (verdict: Time Limit Exceeded):
def main():
    
    n,k=map(int,input().split())
    h=list(map(int,input().split()))

    # dp=[9999999999]*n
    # dp[0]=0
    # dp[1]=abs(h[1]-h[0])

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

    # print(dp[n-1])


    #part2

    dp=[9999999999]*n
    dp[0]=0

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

    print(dp[n-1])




if __name__ == "__main__":
    main()

Fixed submission:
def main():
    
    n,k=map(int,input().split())
    h=list(map(int,input().split()))
 
    # dp=[9999999999]*n
    # dp[0]=0
    # dp[1]=abs(h[1]-h[0])
 
    # for i in range(2,n):
    #     for j in range(1,k+1):
    #         if i-j>=0:
    #             dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
    #         else:
    #             break
 
    # print(dp[n-1])
 
 
    #part2
 
    dp=[9999999999]*n
    dp[0]=0
 
    for i in range(n):
        for j in range(1,k+1):
            if i+j<n:
                dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
            else:
                break
 
    print(dp[n-1])
 
 
 
 
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    
    n,k=map(int,input().split())
    h=list(map(int,input().split()))

    dp=[9999999999]*n
    dp[0]=0
    dp[1]=abs(h[1]-h[0])

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

    print(dp[n-1])


if __name__ == "__main__":
    main()

Fixed submission:
def main():
    
    n,k=map(int,input().split())
    h=list(map(int,input().split()))
 
    # dp=[9999999999]*n
    # dp[0]=0
    # dp[1]=abs(h[1]-h[0])
 
    # for i in range(2,n):
    #     for j in range(1,k+1):
    #         if i-j>=0:
    #             dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
    #         else:
    #             break
 
    # print(dp[n-1])
 
 
    #part2
 
    dp=[9999999999]*n
    dp[0]=0
 
    for i in range(n):
        for j in range(1,k+1):
            if i+j<n:
                dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
            else:
                break
 
    print(dp[n-1])
 
 
 
 
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    
    n,k=map(int,input().split())
    h=list(map(int,input().split()))

    dp=[9999999999]*n
    dp[0]=0
    dp[1]=abs(h[1]-h[0])

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

    print(dp[n-1])


if __name__ == "__main__":
    main()

Fixed submission:
def main():
    
    n,k=map(int,input().split())
    h=list(map(int,input().split()))
 
    # dp=[9999999999]*n
    # dp[0]=0
    # dp[1]=abs(h[1]-h[0])
 
    # for i in range(2,n):
    #     for j in range(1,k+1):
    #         if i-j>=0:
    #             dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
    #         else:
    #             break
 
    # print(dp[n-1])
 
 
    #part2
 
    dp=[9999999999]*n
    dp[0]=0
 
    for i in range(n):
        for j in range(1,k+1):
            if i+j<n:
                dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
            else:
                break
 
    print(dp[n-1])
 
 
 
 
if __name__ == "__main__":
    main()