Buggy python submission (verdict: Time Limit Exceeded):
n,k=[int(i) for i in input().split()]
h=[int(i) for i in input().split()]
d=[0]*n
for i in range(1,n):
  d[i]=min([d[j] + abs(h[i]-h[j]) for j in range(max(i-k,0),i)])
print(d[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
n,k=[int(i) for i in input().split()]
step=[int(i) for i in input().split()]
dp=[0]*n
for i in range(1,n):
  dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
n,k=[int(i) for i in input().split()]
step=[int(i) for i in input().split()]
dp=[0]*n
for i in range(1,n):
  cost=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  dp[i]=cost
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
for i in range(1,n):
  cost=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  dp[i]=cost
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
for i in range(1,n):
  cost=[dp[j] + abs(step[i]-step[j]) for j range(max(i-k,0),i)].min()
  dp[i]=cost
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
if n>=k:
  for i in range(1,n):
    min=10**7
    for j in range(1,min(i+1,k+1)):
      tmp=dp[i-j]+abs(step[i]-step[i-j])
      if tmp<min:
        min=tmp
    dp[i]=min
else:
  for i in range(1,n):
    min=10**7
    for j in range(1,i+1):
      tmp=dp[i-j]+abs(step[i]-step[i-j])
      if tmp<min:
        min=tmp
    dp[i]=min
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline

n,k=map(int,input().split())
step=list(map(int,input().split()))
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
if n>=k:
  for i in range(1,k):
    min=10**7
    for j in range(1,i+1):
      if dp[i-j]+abs(step[i]-step[i-j])<min:
        min=dp[i-j]+abs(step[i]-step[i-j])
    dp[i]=min
  for i in range(k,n):
    min=10**7
    for j in range(1,k+1):
      if dp[i-j]+abs(step[i]-step[i-j])<min:
        min=dp[i-j]+abs(step[i]-step[i-j])
    dp[i]=min
else:
  for i in range(1,n):
    min=10**7
    for j in range(1,i+1):
      if dp[i-j]+abs(step[i]-step[i-j])<min:
        min=dp[i-j]+abs(step[i]-step[i-j])
    dp[i]=min
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
if n>=k:
  for i in range(1,k):
    min=10**7
    for j in range(1,i+1):
      if dp[i-j]+abs(step[i]-step[i-j])<min:
        min=dp[i-j]+abs(step[i]-step[i-j])
    dp[i]=min
  for i in range(k,n):
    min=10**7
    count=k
    for j in step[i-k:i]:
      if dp[i-count]+abs(step[i]-j)<min:
        min=dp[i-j]+abs(step[i]-step[i-j])
      count-=1
    dp[i]=min
else:
  for i in range(1,n):
    min=10**7
    for j in range(1,i+1):
      if dp[i-j]+abs(step[i]-step[i-j])<min:
        min=dp[i-j]+abs(step[i]-step[i-j])
    dp[i]=min
print(dp[n-1])


Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
for i in range(1,k):
  min=10**7
  for j in range(1,i+1):
    if dp[i-j]+abs(step[i]-step[i-j])<min:
      min=dp[i-j]+abs(step[i]-step[i-j])
  dp[i]=min
for i in range(k,n):
  min=10**7
  for j in range(1,k+1):
    if dp[i-j]+abs(step[i]-step[i-j])<min:
      min=dp[i-j]+abs(step[i]-step[i-j])
  dp[i]=min
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
for i in range(1,k+1):
  min=10**7
  for j in range(1,i+1):
    if abs(step[i]-step[i-j])<min:
      min=abs(step[i]-step[i-j])
  dp[i]=min
for i in range(k+1,n):
  min=10**7
  for j in range(1,k+1):
    if abs(step[i]-step[i-j])<min:
      min=abs(step[i]-step[i-j]
  dp[i]=min
print(dp[n-1])


Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
for i in range(1,k):
  min=10**7
  for j in range(1,i+1):
    if abs(step[i]-step[i-j])<min:
      dp[i]=abs(step[i]-step[i-j])
for i in range(k,n):
  min=10**7
  for j in range(1,k+1):
    if abs(step[i]-step[i-j])<min:
      dp[i]=abs(step[i]-step[i-j])
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
step=list(map(int,input().split()))
dp=[0]*n
for i in range(1,k):
  min=10**7
  for j in range(1,i):
    if abs(step[i]-step[i-j])<min:
      dp[i]=abs(step[i]-step[i-j])
for i in range(k,n):
  min=10**7
  for j in range(1,k):
    if abs(step[i]-step[i-j])<min:
      dp[i]=abs(step[i]-step[i-j])
print(dp[n-1])

Fixed submission:
def solve():
  n,k=[int(i) for i in input().split()]
  step=[int(i) for i in input().split()]
  dp=[0]*n
  for i in range(1,n):
    dp[i]=min([dp[j] + abs(step[i]-step[j]) for j in range(max(i-k,0),i)])
  return dp[n-1]
print(solve())