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

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

  for i in range(2,N):
    index = i - K
    if index < 0: index = 0
    ans = 10**9
    for j in range(index,i):
      ans = min(ans,dp[j] + abs(h[i] - h[j]))
    dp[i] = ans
  print(dp[N-1])

if __name__ == '__main__':
  main()

Fixed submission:
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
  print(dp[N-1])

if __name__ == '__main__':
  main()

Buggy python submission (verdict: Runtime Error):
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [-1]*N
  dp[0] = 0
  for i in range(1,N):
    tmp = i - K
    if tmp < 0: tmp = 0
    cost_min = 10**9
    for j in range(index,i):
      ans = dp[j] + abs(h[i] - h[j])
      if cost_min > ans: cost_min = ans
    dp[i] = cost_min
  print(dp[N-1])

if __name__ == '__main__':
  main()


Fixed submission:
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
  print(dp[N-1])

if __name__ == '__main__':
  main()

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

dp = [0]*N

for i in range(1,N):
  dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
print(dp[N-1])


Fixed submission:
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
  print(dp[N-1])

if __name__ == '__main__':
  main()

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

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

for i in range(2,N):
  dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
print(dp[N-1])

Fixed submission:
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
  print(dp[N-1])

if __name__ == '__main__':
  main()

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

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

for i in range(2,N):
  index = i - K
  if index < 0: index = 0

  dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(index,i))
print(dp[N-1])

Fixed submission:
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
  print(dp[N-1])

if __name__ == '__main__':
  main()

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

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

for i in range(2,N):
  index = i - K
  if index < 0: index = 0
  cost_min = 10**9
  for j in range(index,i):
    ans = dp[j] + abs(h[i] - h[j])
    if cost_min > ans: cost_min = ans
  dp[i] = cost_min
print(dp[N-1])

Fixed submission:
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
  print(dp[N-1])

if __name__ == '__main__':
  main()

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

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

for i in range(2,N):
  index = i - K
  if index < 0: index = 0
  ans = 10**9
  for j in range(index,i):
    ans = min(ans,dp[j] + abs(h[i] - h[j]))
  dp[i] = ans
print(dp[N-1])

Fixed submission:
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
  print(dp[N-1])

if __name__ == '__main__':
  main()

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

if N<K:K=N
dp = [-1]*N
dp[0] = 0
for i in range(1,K):
  dp[i] = dp[i-1] + abs(h[i-1]-h[i])
  for j in range(2,i+1):
    dp[i] = min(dp[i],dp[i-j]+abs(h[i-j]-h[i]))
for i in range(K,N):
  cost_ = 10**9
  for j in range(1,K+1):
    cost_ = min(cost_,dp[i-j] + abs(h[i]-h[i-j]))
  dp[i] = cost_
print(dp[N-1])

Fixed submission:
def main():
  N,K= map(int,input().split())
  h = list(map(int,input().split()))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i-K,0),i))
  print(dp[N-1])

if __name__ == '__main__':
  main()