Buggy python submission (verdict: Time Limit Exceeded):
def knapsack(nums, k):
    n = len(nums)

    # dp[i] is min cost when reaching stone i
    dp = [0] * n
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min([
            dp[i-j] + abs(nums[i] - nums[i-j])
            for j in range(1, k+1) if i - j >= 0]
        )
    return dp[-1]


if __name__ == "__main__":
    _, k = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, k))


Fixed submission:
def knapsack(nums, k):
    n = len(nums)

    # dp[i] is min cost when reaching stone i
    dp = [float('Inf')] * n
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min([
            dp[i-j] + abs(nums[i] - nums[i-j])
            for j in range(1, k+1) if i - j >= 0]
        )
    return dp[-1]


if __name__ == "__main__":
    _, k = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, k))

Buggy python submission (verdict: Runtime Error):
def knapsack(nums, KLeap):
    n = len(nums)
    if n == 0 or n == 1:
        return 0
 
    if n < KLeap:
        return abs(nums[0] - nums[-1])
    # dp[i] is min cost when reaching stone i
    dp = [0] * n
    for i in range(1, KLeap):
        dp[i] = abs(nums[0] - nums[i])
 
    for i in range(K+1, n):
        dp[i] = min([dp[i - k] + abs(nums[i - k] - nums[i]) for k in range(1, KLeap)])
 
    return dp[-1]
 
 
if __name__ == "__main__":
    _, K = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, K))

Fixed submission:
def knapsack(nums, k):
    n = len(nums)

    # dp[i] is min cost when reaching stone i
    dp = [float('Inf')] * n
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min([
            dp[i-j] + abs(nums[i] - nums[i-j])
            for j in range(1, k+1) if i - j >= 0]
        )
    return dp[-1]


if __name__ == "__main__":
    _, k = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, k))

Buggy python submission (verdict: Runtime Error):
ef knapsack(nums, KLeap):
    n = len(nums)
    if n == 0 or n == 1:
        return 0
 
    if n < KLeap:
        return abs(nums[0] - nums[-1])
    # dp[i] is min cost when reaching stone i
    dp = [0] * n
    for i in range(1, KLeap):
        dp[i] = abs(nums[0] - nums[i])
 
    for i in range(K+1, n):
        dp[i] = min([dp[i - k] + abs(nums[i - k] - nums[i]) for k in range(1, KLeap)])
 
    return dp[-1]
 
 
if __name__ == "__main__":
    _, K = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, K))

Fixed submission:
def knapsack(nums, k):
    n = len(nums)

    # dp[i] is min cost when reaching stone i
    dp = [float('Inf')] * n
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min([
            dp[i-j] + abs(nums[i] - nums[i-j])
            for j in range(1, k+1) if i - j >= 0]
        )
    return dp[-1]


if __name__ == "__main__":
    _, k = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, k))

Buggy python submission (verdict: Runtime Error):
def knapsack(nums, KLeap):
    n = len(nums)
    if n == 0 or n == 1:
        return 0

    if n < KLeap:
        return abs(nums[0] - nums[-1])
    # dp[i] is min cost when reaching stone i
    dp = [0] * n
    for i in range(1, KLeap):
        dp[i] = abs(nums[0] - nums[i])

    for i in range(K+1, n):
        dp[i] = min([dp[i - k] + abs(nums[i - k] - nums[i]) for k in range(1, KLeap)])

    return dp[-1]


if __name__ == "__main__":
    _, K = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, K))


Fixed submission:
def knapsack(nums, k):
    n = len(nums)

    # dp[i] is min cost when reaching stone i
    dp = [float('Inf')] * n
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min([
            dp[i-j] + abs(nums[i] - nums[i-j])
            for j in range(1, k+1) if i - j >= 0]
        )
    return dp[-1]


if __name__ == "__main__":
    _, k = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, k))

Buggy python submission (verdict: Runtime Error):
def knapsack(nums, KLeap):
    n = len(nums)
    if n == 0 or n == 1:
        return 0

    if n == 2:
        return abs(nums[0] - nums[1])

    # dp[i] is min cost when reaching stone i
    dp = [0] * n
    for i in range(1, KLeap):
        dp[i] = abs(nums[0] - nums[i])

    for i in range(K+1, n):
        dp[i] = min([dp[i - k] + abs(nums[i - k] - nums[i]) for k in range(1, KLeap)])

    return dp[-1]


if __name__ == "__main__":
    _, K = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, K))


Fixed submission:
def knapsack(nums, k):
    n = len(nums)

    # dp[i] is min cost when reaching stone i
    dp = [float('Inf')] * n
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min([
            dp[i-j] + abs(nums[i] - nums[i-j])
            for j in range(1, k+1) if i - j >= 0]
        )
    return dp[-1]


if __name__ == "__main__":
    _, k = [int(x) for x in input().split()]
    nums = [int(x) for x in input().split()]
    print(knapsack(nums, k))