Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def main():
  N, K = list(map(int, input().split()))
  h = np.asarray(list(map(int, input().split())))

  if N == 2:
    print(abs(h[1] - h[0]))
    exit()
  
  DP = np.full(N, int(10e10), dtype = np.int32)

  mi = min(N, K + 1)
  
  DP[1: mi] = np.abs(h[1: mi] - h[0])
  
  for i in range(1, N - 1):
    t = i + K + 1
    T = np.abs(h[i + 1: t] - h[i]) + DP[i]
    DP[i + 1: t] = np.where(T < DP[i + 1: t], T, DP[i + 1: t])
  
  print(DP[N-1])
main()


Fixed submission:
def main():
  N, K = list(map(int, input().split()))
  h = tuple(map(int, input().split()))

  if N == 2:
    print(abs(h[1] - h[0]))
    exit()

  DP = [0 for i in range(N)]

  mi = min(N, K + 1)
  for i in range(1, mi):
    DP[i] = abs(h[i] - h[0])
  for i in range(1, N - 1):
    mi = min(N, i + K)
    for j in range(i, mi):
      t = DP[i] + abs(h[j] - h[i])
      if DP[j] > t:
        DP[j] = t
    if i + K <= N - 1:
      DP[i+K] = DP[i] + abs(h[i+K] - h[i])
  print(DP[N-1])
  
main()

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def main():
  N, K = list(map(int, input().split()))
  h = np.asarray(list(map(int, input().split())))

  if N == 2:
    print(abs(h[1] - h[0]))
    exit()
  
  DP = np.full(N, int(10e10), dtype = np.int32)

  mi = min(N, K + 1)
  DP[1: mi] = np.abs(h[1: mi] - h[0])
  for i in range(1, N - 1):
    mi = min(N - 1, i + K + 1)
    T = np.asarray(np.abs(h[i + 1: mi + 1] - h[i]) + DP[i])
    DP[i + 1: mi + 1] = np.where(T < DP[i + 1: mi + 1], T, DP[i + 1: mi + 1])
  print(DP[N-1])
  
main()


Fixed submission:
def main():
  N, K = list(map(int, input().split()))
  h = tuple(map(int, input().split()))

  if N == 2:
    print(abs(h[1] - h[0]))
    exit()

  DP = [0 for i in range(N)]

  mi = min(N, K + 1)
  for i in range(1, mi):
    DP[i] = abs(h[i] - h[0])
  for i in range(1, N - 1):
    mi = min(N, i + K)
    for j in range(i, mi):
      t = DP[i] + abs(h[j] - h[i])
      if DP[j] > t:
        DP[j] = t
    if i + K <= N - 1:
      DP[i+K] = DP[i] + abs(h[i+K] - h[i])
  print(DP[N-1])
  
main()

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

  if N == 2:
    print(abs(h[1] - h[0]))
    exit()

  mi = min(N, K + 1)
  
  DP = [abs(h[i] - h[0]) for i in range(1, mi)]
  for i in range(1, N - 1):
    mi = min(N - i, K)
    t = DP[0]
    DP = [min(DP[j], abs(h[i + j] - h[i]) + t) for j in range(1, mi)]
    if i + K <= N - 1:
      DP.append(abs(h[i + K] - h[i]) + t)
  print(DP[0])
  
main()

Fixed submission:
def main():
  N, K = list(map(int, input().split()))
  h = tuple(map(int, input().split()))

  if N == 2:
    print(abs(h[1] - h[0]))
    exit()

  DP = [0 for i in range(N)]

  mi = min(N, K + 1)
  for i in range(1, mi):
    DP[i] = abs(h[i] - h[0])
  for i in range(1, N - 1):
    mi = min(N, i + K)
    for j in range(i, mi):
      t = DP[i] + abs(h[j] - h[i])
      if DP[j] > t:
        DP[j] = t
    if i + K <= N - 1:
      DP[i+K] = DP[i] + abs(h[i+K] - h[i])
  print(DP[N-1])
  
main()

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

  if N == 2:
    print(abs(h[1] - h[0]))
    exit()

  DP = [0 for i in range(N)]

  mi = min(N, K + 1)
  for i in range(1, mi):
    DP[i] = abs(h[i] - h[0])
  for i in range(1, N - 1):
    mi = min(N, i + K)
    for j in range(i, mi):
      t = DP[i] + abs(h[j] - h[i])
      if DP[j] > t:
        DP[j] = t
    if i + K <= N - 1:
      DP[i+K] = DP[i] + abs(h[i+K] - h[i])
  print(DP[N-1])
  
main()

Fixed submission:
def main():
  N, K = list(map(int, input().split()))
  h = tuple(map(int, input().split()))

  if N == 2:
    print(abs(h[1] - h[0]))
    exit()

  DP = [0 for i in range(N)]

  mi = min(N, K + 1)
  for i in range(1, mi):
    DP[i] = abs(h[i] - h[0])
  for i in range(1, N - 1):
    mi = min(N, i + K)
    for j in range(i, mi):
      t = DP[i] + abs(h[j] - h[i])
      if DP[j] > t:
        DP[j] = t
    if i + K <= N - 1:
      DP[i+K] = DP[i] + abs(h[i+K] - h[i])
  print(DP[N-1])
  
main()