Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline
INF = 1 << 31


def main(n, k, heights):
    heights.extend([INF] * k)
    costs = [INF] * (n + k)
    costs[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            costs[i + j] = min(costs[i + j], abs(heights[i] - heights[i + j]) + costs[i])
    return costs[n - 1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))


Fixed submission:
import numpy as np


def main(n, k, heights):
    heights = np.array(heights, dtype=int)
    costs = np.zeros(n, dtype=int)
    for i in range(1, n):
        start = max(0, i - k)
        costs[i] = np.min(costs[start:i] + np.abs(heights[i] - heights[start:i]))
    return costs[-1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))

Buggy python submission (verdict: Time Limit Exceeded):
INF = 1 << 32


def main(n, k, heights):
    costs = [INF for i in range(n)]
    costs[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            if i + j >= n:
                break
            costs[i + j] = min(costs[i + j], abs(heights[i] - heights[i + j]) + costs[i])
    return costs[n - 1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))


Fixed submission:
import numpy as np


def main(n, k, heights):
    heights = np.array(heights, dtype=int)
    costs = np.zeros(n, dtype=int)
    for i in range(1, n):
        start = max(0, i - k)
        costs[i] = np.min(costs[start:i] + np.abs(heights[i] - heights[start:i]))
    return costs[-1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))

Buggy python submission (verdict: Time Limit Exceeded):
INF = 1 << 32


def main(n, k, heights):
    for i in range(k):
        heights.extend([INF for i in range(k)])
    costs = [INF for i in range(n + k)]
    costs[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            costs[i + j] = min(costs[i + j], abs(heights[i] - heights[i + j]) + costs[i])
    return costs[n - 1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))


Fixed submission:
import numpy as np


def main(n, k, heights):
    heights = np.array(heights, dtype=int)
    costs = np.zeros(n, dtype=int)
    for i in range(1, n):
        start = max(0, i - k)
        costs[i] = np.min(costs[start:i] + np.abs(heights[i] - heights[start:i]))
    return costs[-1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))

Buggy python submission (verdict: Time Limit Exceeded):
INF = 1 << 64


def calc_cost(height1, height2):
    return abs(height1 - height2)


def main(n, k, heights):
    for i in range(k):
        heights.append(INF)
    costs = [INF for i in range(n + k)]
    costs[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            costs[i + j] = min(costs[i + j], calc_cost(heights[i], heights[i + j]) + costs[i])
    return costs[n - 1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))


Fixed submission:
import numpy as np


def main(n, k, heights):
    heights = np.array(heights, dtype=int)
    costs = np.zeros(n, dtype=int)
    for i in range(1, n):
        start = max(0, i - k)
        costs[i] = np.min(costs[start:i] + np.abs(heights[i] - heights[start:i]))
    return costs[-1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))