Buggy python submission (verdict: Runtime Error):
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        # dp[i] = min(dp[j] + abs(heights[i] - heights[j]) for j in range(max(i - k, 0), i))
        dp[i] = min(dp[i - j] + abs(heights[i] - heights[i - j]) for j in range(1, k + 1))

    print(dp[n - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min(dp[j] + abs(heights[i] - heights[j]) for j in range(max(i - k, 0), i))
        # for j in range(1, k + 1):
        #     if i - j < 0:
        #         break
        #     dp[i] = min(dp[i], dp[i - j] + abs(heights[i] - heights[i - j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min(dp[j] + abs(heights[i] - heights[j]) for j in range(max(i - k, 0), i))
        # for j in range(1, k + 1):
        #     if i - j < 0:
        #         break
        #     dp[i] = min(dp[i], dp[i - j] + abs(heights[i] - heights[i - j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(0, n):
        for j in range(1, k + 1):
            if i + j == len(heights):
                break
            dp[i + j] = min(dp[i + j], dp[i] + abs(heights[i] - heights[i + j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min(dp[j] + abs(heights[i] - heights[j]) for j in range(max(i - k, 0), i))
        # for j in range(1, k + 1):
        #     if i - j < 0:
        #         break
        #     dp[i] = min(dp[i], dp[i - j] + abs(heights[i] - heights[i - j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()

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

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(0, n):
        for j in range(1, k + 1):
            if i + j == len(heights):
                break
            dp[i + j] = min(dp[i + j], dp[i] + abs(heights[i] - heights[i + j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min(dp[j] + abs(heights[i] - heights[j]) for j in range(max(i - k, 0), i))
        # for j in range(1, k + 1):
        #     if i - j < 0:
        #         break
        #     dp[i] = min(dp[i], dp[i - j] + abs(heights[i] - heights[i - j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        for j in range(1, k + 1):
            # if i - j < 0:
            #     break
            dp[i] = min(dp[i + j], dp[i] + abs(heights[i] - heights[i + j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min(dp[j] + abs(heights[i] - heights[j]) for j in range(max(i - k, 0), i))
        # for j in range(1, k + 1):
        #     if i - j < 0:
        #         break
        #     dp[i] = min(dp[i], dp[i - j] + abs(heights[i] - heights[i - j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()

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

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        for j in range(1, k + 1):
            if i - j < 0:
                break
            dp[i] = min(dp[i], dp[i - j] + abs(heights[i] - heights[i - j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    heights = list(map(int, input().split()))

    dp = [float('inf') for i in range(n)]
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min(dp[j] + abs(heights[i] - heights[j]) for j in range(max(i - k, 0), i))
        # for j in range(1, k + 1):
        #     if i - j < 0:
        #         break
        #     dp[i] = min(dp[i], dp[i - j] + abs(heights[i] - heights[i - j]))

    print(dp[n - 1])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
dp = [float("inf") for _ in range(n)]
dp[0] = 0
for i in range(1,n):
    for j in range(1,min(k,i)+1):
        dp[i] = min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
print(dp[-1])

Fixed submission:
n,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
dp = [float("inf") for _ in range(n)]
dp[0] = 0
for i in range(1,n):
    for j in range(1,min(k,i)+1):
        dp[i] = min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
print(dp[-1])