Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
l = IL()

dp = [0]*N
dp[1] = abs(l[0]-l[1])

for i in range(2,N):
  num = int(10e10)
  for j in range(1,K+1):
    if i-j < 0:
      dp[i] = num
      break
    else:
      tmp = dp[i-j]+abs(l[i]-l[i-j])
      num = min(tmp,num)
  dp[i] = num

print(dp[N-1])

Fixed submission:
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
l = np.array(IL())

dp = np.zeros(N,dtype=int)
dp[0] = 0

for i in range(1,N):
    if i-K < 0:
        dp[i] = np.min(dp[:i]+np.abs(l[i]-l[:i]))
    elif i-K >= 0:
        dp[i] = np.min(dp[i-K:i]+np.abs(l[i]-l[i-K:i]))

print(dp[-1])

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

dp = [0]*N
dp[1] = abs(l[0]-l[1])

for i in range(2,N):
  dp[i] = int(10e10)
  for j in range(1,K+1):
    if i-j >= 0:
      dp[i] = min(dp[i],dp[i-j]+abs(l[i]-l[i-j]))

print(dp[N-1])

Fixed submission:
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
l = np.array(IL())

dp = np.zeros(N,dtype=int)
dp[0] = 0

for i in range(1,N):
    if i-K < 0:
        dp[i] = np.min(dp[:i]+np.abs(l[i]-l[:i]))
    elif i-K >= 0:
        dp[i] = np.min(dp[i-K:i]+np.abs(l[i]-l[i-K:i]))

print(dp[-1])

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

dp = [0]*N
dp[1] = abs(l[0]-l[1])

for i in range(2,N):
  num = int(10e10)
  for j in range(1,K+1):
    if i-j < 0:
      dp[i] = num
      break
    else:
      if l[i] > l[i-j]:
        tmp = dp[i-j]+l[i]-l[i-j]
        num = min(tmp,num)
      else:
        tmp = dp[i-j]+l[i-j]-l[i]
        num = min(tmp,num)    
  dp[i] = num

print(dp[N-1])

Fixed submission:
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
l = np.array(IL())

dp = np.zeros(N,dtype=int)
dp[0] = 0

for i in range(1,N):
    if i-K < 0:
        dp[i] = np.min(dp[:i]+np.abs(l[i]-l[:i]))
    elif i-K >= 0:
        dp[i] = np.min(dp[i-K:i]+np.abs(l[i]-l[i-K:i]))

print(dp[-1])

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

dp = [0]*N
dp[1] = abs(l[0]-l[1])

for i in range(2,N):
  num = int(10e10)
  for j in range(1,K+1):
    if i-j < 0:
      dp[i] = num
      break
    else:
      tmp = dp[i-j]+abs(l[i]-l[i-j])
      num = min(tmp,num)
  dp[i] = num

print(dp[N-1])


Fixed submission:
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
l = np.array(IL())

dp = np.zeros(N,dtype=int)
dp[0] = 0

for i in range(1,N):
    if i-K < 0:
        dp[i] = np.min(dp[:i]+np.abs(l[i]-l[:i]))
    elif i-K >= 0:
        dp[i] = np.min(dp[i-K:i]+np.abs(l[i]-l[i-K:i]))

print(dp[-1])