Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)

def input():
  return sys.stdin.readline()

def get_cost(arr, n, k, dp):
  if dp[n] is not None:
    return dp[n]
  elif n-k <= 0:
    dp[n] = abs(arr[n] - arr[0])
    return dp[n]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  elif n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  else:
    dp[n] = min(((abs(arr[n] - arr[n-i]) + get_cost(arr, n-i, k, dp)) for i in range(1, k+1)))
    return dp[n]

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [None]*N
print(get_cost(arr, N-1, K, dp))

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  min_ = -1
  for i in range(2, n):
    dp[i] = min([(abs(arr[s] - arr[i]) + dp[s]) for s in range(max(0, i-k), i)])
  return dp[-1]
    

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [0]*N
dp[1] = abs(arr[1] - arr[0])
print(get_cost(arr, N, K, dp))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  if dp[n] is not None:
    return dp[n]
  elif n-k <= 0:
    dp[n] = abs(arr[n] - arr[0])
    return dp[n]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  elif n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  else:
    dp[n] = min(((abs(arr[n] - arr[n-i]) + get_cost(arr, n-i, k, dp)) for i in range(1, k+1)))
    return dp[n]

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [None]*N
print(get_cost(arr, N-1, K, dp))

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  min_ = -1
  for i in range(2, n):
    dp[i] = min([(abs(arr[s] - arr[i]) + dp[s]) for s in range(max(0, i-k), i)])
  return dp[-1]
    

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [0]*N
dp[1] = abs(arr[1] - arr[0])
print(get_cost(arr, N, K, dp))

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

sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  if dp[n] != 0:
    return dp[n]
  elif n-k <= 0:
    dp[n] = abs(arr[n] - arr[0])
    return dp[n]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  elif n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  else:
    dp[n] = min(((np.abs(arr[n] - arr[n-i]) + get_cost(arr, n-i, k, dp)) for i in range(1, k+1)))
    return dp[n]

N, K = input().split()
N = int(N)
K = int(K)
arr = np.array(input().split(), dtype=np.int64)

dp = np.zeros(N, dtype=np.int64)
print(get_cost(arr, N-1, K, dp))

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  min_ = -1
  for i in range(2, n):
    dp[i] = min([(abs(arr[s] - arr[i]) + dp[s]) for s in range(max(0, i-k), i)])
  return dp[-1]
    

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [0]*N
dp[1] = abs(arr[1] - arr[0])
print(get_cost(arr, N, K, dp))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  if dp[n] is not None:
    return dp[n]
  elif n-k <= 0:
    dp[n] = abs(arr[n] - arr[0])
    return dp[n]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  elif n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  else:
    dp[n] = min(((abs(arr[n] - arr[n-i]) + get_cost(arr, n-i, k, dp)) for i in range(1, k+1)))
    return dp[n]

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [None]*N
print(get_cost(arr, N-1, K, dp))

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  min_ = -1
  for i in range(2, n):
    dp[i] = min([(abs(arr[s] - arr[i]) + dp[s]) for s in range(max(0, i-k), i)])
  return dp[-1]
    

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [0]*N
dp[1] = abs(arr[1] - arr[0])
print(get_cost(arr, N, K, dp))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  if dp[n] is not None:
    return dp[n]
  elif n-k <= 0:
    dp[n] = abs(arr[n] - arr[0])
    return dp[n]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  elif n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  else:
    dp[n] = min([(abs(arr[n] - arr[n-i]) + get_cost(arr, n-i, k, dp)) for i in range(1, k+1)])
    return dp[n]

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [None]*N
print(get_cost(arr, N-1, K, dp))

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  min_ = -1
  for i in range(2, n):
    dp[i] = min([(abs(arr[s] - arr[i]) + dp[s]) for s in range(max(0, i-k), i)])
  return dp[-1]
    

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [0]*N
dp[1] = abs(arr[1] - arr[0])
print(get_cost(arr, N, K, dp))

Buggy python submission (verdict: Runtime Error):
def get_cost(arr, n, k, dp):
  if dp[n] is not None:
    return dp[n]
  elif k >= n-1:
    return abs(arr[-1] - arr[0])
  elif n == 0:
    dp[0] = 0
    return dp[0]
  elif n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]

N, K = input().split()
N = int(N)
K = int(K)

dp = [None]*N
print(get_cost(arr, N-1, K, dp)


Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def get_cost(arr, n, k, dp):
  min_ = -1
  for i in range(2, n):
    dp[i] = min([(abs(arr[s] - arr[i]) + dp[s]) for s in range(max(0, i-k), i)])
  return dp[-1]
    

N, K = input().split()
N = int(N)
K = int(K)
arr = [int(num) for num in input().split()]

dp = [0]*N
dp[1] = abs(arr[1] - arr[0])
print(get_cost(arr, N, K, dp))