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

costs = [0] * N
costs[1] = abs(h[1]-h[0])
for i in range(2,N):
  s = max(i-K,0)
  mn = float('inf')
  for a,b in zip(costs[s:i],h[s:i]):
    cur = a + abs(h[i]-b)
    if cur < mn:
      mn = cur
 
  costs[i] = mn
print(costs[-1])

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

def aa (n,k,ll):
  dp = [0] * n
  for i in range(1,n):
    s = max(i-k,0)
    mn = float('inf')
    for a,b in zip(dp[s:i],ll[s:i]):
      cur = a + abs(ll[i]-b)
      if cur < mn:
        mn = cur
  
    dp[i] = mn
  print(dp[-1])

aa(N,K,h)

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

costs = [0] * N
for i in range(1,N):
  _h = h[i]
  s = 0
  if i > K:
    s = i-K
  mn = float('inf')
  for a,b in zip(costs[s:i],h[s:i]):
    cur = a + abs(_h-b)
    if cur < mn:
      mn = cur
 
  costs[i] = mn
print(costs[-1])

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

def aa (n,k,ll):
  dp = [0] * n
  for i in range(1,n):
    s = max(i-k,0)
    mn = float('inf')
    for a,b in zip(dp[s:i],ll[s:i]):
      cur = a + abs(ll[i]-b)
      if cur < mn:
        mn = cur
  
    dp[i] = mn
  print(dp[-1])

aa(N,K,h)

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

costs = [0] * N
for i in range(1,N):
  s = max(i-K, 0)
  mn = float('inf')
  for a,b in zip(costs[s:i],h[s:i]):
    cur = a + abs(h[i]-b)
    if cur < mn:
      mn = cur
 
  costs[i] = mn
print(costs[-1])

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

def aa (n,k,ll):
  dp = [0] * n
  for i in range(1,n):
    s = max(i-k,0)
    mn = float('inf')
    for a,b in zip(dp[s:i],ll[s:i]):
      cur = a + abs(ll[i]-b)
      if cur < mn:
        mn = cur
  
    dp[i] = mn
  print(dp[-1])

aa(N,K,h)

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

costs = [0] * N
for i in range(1,N):
  s = max(i-K, 0)
  mn = float('inf')
  for a,b in zip(costs[s:i],h[s:i]):
    cur = a + abs(h[i]-b)
    mn = min(mn,cur)
 
  costs[i] = mn
print(costs[-1])

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

def aa (n,k,ll):
  dp = [0] * n
  for i in range(1,n):
    s = max(i-k,0)
    mn = float('inf')
    for a,b in zip(dp[s:i],ll[s:i]):
      cur = a + abs(ll[i]-b)
      if cur < mn:
        mn = cur
  
    dp[i] = mn
  print(dp[-1])

aa(N,K,h)

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

costs = [float('inf')] * N
costs[0] = 0
for i in range(1,N):
  s = max(i-K, 0)
  mn = float('inf')
  for j in range(s,i):
    cur = costs[j] + abs(h[i] - h[j])
    mn = min(mn,cur)
  costs[i] = mn
print(costs[-1])

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

def aa (n,k,ll):
  dp = [0] * n
  for i in range(1,n):
    s = max(i-k,0)
    mn = float('inf')
    for a,b in zip(dp[s:i],ll[s:i]):
      cur = a + abs(ll[i]-b)
      if cur < mn:
        mn = cur
  
    dp[i] = mn
  print(dp[-1])

aa(N,K,h)

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

costs = [float('inf')] * N
costs[0] = 0
for i in range(0,N):
  for j in range(1,K+1):
    if i+j >= N:
      break
    costs[i+j] = min(costs[i+j], costs[i] + abs(h[i] - h[i+j]))
print(costs[N-1])

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

def aa (n,k,ll):
  dp = [0] * n
  for i in range(1,n):
    s = max(i-k,0)
    mn = float('inf')
    for a,b in zip(dp[s:i],ll[s:i]):
      cur = a + abs(ll[i]-b)
      if cur < mn:
        mn = cur
  
    dp[i] = mn
  print(dp[-1])

aa(N,K,h)

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

costs = [0] * N
for i in range(1,N):
  s = max(i-K, 0)
  tmp = [float('inf') for _ in range(K)]
  for j in range(s,i):
    tmp[j-s] = costs[j] + abs(h[i] - h[j])
  costs[i] = min(tmp)
print(costs[N-1])

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

def aa (n,k,ll):
  dp = [0] * n
  for i in range(1,n):
    s = max(i-k,0)
    mn = float('inf')
    for a,b in zip(dp[s:i],ll[s:i]):
      cur = a + abs(ll[i]-b)
      if cur < mn:
        mn = cur
  
    dp[i] = mn
  print(dp[-1])

aa(N,K,h)