Buggy python submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

def g(n, k, h):
    g = abs(h[1] - h[0])
    gk = deque([0, g], k)
    for i in range(2, n):
        h_i = h[i]
        j = max(i - k, 0)
        g = min(abs(h_i - h_j) + h_k for h_j, h_k in zip(h[j:i], gk))
        if i >= k:
            gk.popleft()
        gk.append(g)
    return g

print(g(N, K, h))


Fixed submission:
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
def g(n, k, h):
    gk = deque([abs(h[1] - h[0]), 0])
    g = gk[0]
    for i in range(2, n):
        g = min(abs(h[i] - h[i-j]) + gk[j-1] for j in range(1, min(i, k)+1))
        if i >= k:
            gk.pop()
        gk.appendleft(g)
    return g

print(g(N, K, h))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

def g(n, k, h):
    gk = deque([0, abs(h[1] - h[0])], k)
    g = abs(h[1] - h[0])
    for i in range(2, n):
        h_i = h[i]
        j = max(i - k, 0)
        g = min(abs(h_i - h_j) + h_k for h_j, h_k in zip(h[j:], gk))
        if i >= k:
            gk.popleft()
        gk.append(g)
    return g

print(g(N, K, h))


Fixed submission:
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
def g(n, k, h):
    gk = deque([abs(h[1] - h[0]), 0])
    g = gk[0]
    for i in range(2, n):
        g = min(abs(h[i] - h[i-j]) + gk[j-1] for j in range(1, min(i, k)+1))
        if i >= k:
            gk.pop()
        gk.appendleft(g)
    return g

print(g(N, K, h))

Buggy python submission (verdict: Runtime Error):
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

def g(n, k, h):
    gk = deque([0, abs(h[1] - h[0])], k)
    g = gk[1]
    for i in range(2, n):
        h_i = h[i]
        j = max(i - k, 0)
        g = min(abs(h_i - h_j) + h_k for h_j, h_k in zip(h[j:], gk))
        if i >= k:
            gk.popleft()
        gk.append(g)
    return g

print(g(N, K, h))


Fixed submission:
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
def g(n, k, h):
    gk = deque([abs(h[1] - h[0]), 0])
    g = gk[0]
    for i in range(2, n):
        g = min(abs(h[i] - h[i-j]) + gk[j-1] for j in range(1, min(i, k)+1))
        if i >= k:
            gk.pop()
        gk.appendleft(g)
    return g

print(g(N, K, h))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

def g(n, k, h):
    gk = [0] * n
    g = gk[1] = abs(h[1] - h[0])
    for i in range(2, n):
        g = min(abs(h[i] - h[i-j]) + gk[i-j] for j in range(1, min(i, k)+1))
        gk[i] = g
    return g

print(g(N, K, h))


Fixed submission:
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
def g(n, k, h):
    gk = deque([abs(h[1] - h[0]), 0])
    g = gk[0]
    for i in range(2, n):
        g = min(abs(h[i] - h[i-j]) + gk[j-1] for j in range(1, min(i, k)+1))
        if i >= k:
            gk.pop()
        gk.appendleft(g)
    return g

print(g(N, K, h))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

def g(n, k, h):
    gk = deque([abs(h[1] - h[0]), 0])
    g = gk[0]
    for i in range(2, n):
        g = min(abs(h[i] - h[i-j]) + gk[j-1] for j in range(1, min(i, k)+1))
        if i >= k:
            gk.pop()
        gk.appendleft(g)
    return g

print(g(N, K, h))


Fixed submission:
from collections import deque
N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
def g(n, k, h):
    gk = deque([abs(h[1] - h[0]), 0])
    g = gk[0]
    for i in range(2, n):
        g = min(abs(h[i] - h[i-j]) + gk[j-1] for j in range(1, min(i, k)+1))
        if i >= k:
            gk.pop()
        gk.appendleft(g)
    return g

print(g(N, K, h))