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(0,i-K),i)])
print(dp[-1])

Fixed submission:
def main():
  n, k = map(int, input().split())
  h = [int(n) for n in 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(0,i-k),i)])  
  print(dp[-1])

if __name__ == "__main__":
  main()

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

if __name__ == "__main__":
  main()

Fixed submission:
def main():
  n, k = map(int, input().split())
  h = [int(n) for n in 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(0,i-k),i)])  
  print(dp[-1])

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = [int(n) for n in input().split()]
dp = [0]*n
def a():  
  dp[1] = abs(h[1]-h[0])
  for i in range(2, n):
    dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1,min(i+1,k+1))])  
  return dp[-1]

if __name__ == "__main__":
  print(a())

Fixed submission:
def main():
  n, k = map(int, input().split())
  h = [int(n) for n in 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(0,i-k),i)])  
  print(dp[-1])

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(n) for n in input().split()]
h = [int(n) for n in input().split()]
dp = [0]*n

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

Fixed submission:
def main():
  n, k = map(int, input().split())
  h = [int(n) for n in 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(0,i-k),i)])  
  print(dp[-1])

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = [int(n) for n in input().split()]
dp = [0]*n

dp[1] = abs(h[1]-h[0])
for i in range(2, n):
  dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1,min(i+1,k+1))])
  #for j in range(1,min(i+1,k+1)):
   # if j == 1:
    #  dp[i] = dp[i-1]+abs(h[i]-h[i-1])
    #print([i,j], dp[i-j]+abs(h[i]-h[i-j]), dp[i])
    #dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
#print(h)
print(dp[-1])

Fixed submission:
def main():
  n, k = map(int, input().split())
  h = [int(n) for n in 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(0,i-k),i)])  
  print(dp[-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[1] = abs(h[1]-h[0])
for i in range(2, n):
  dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1,min(i+1,k+1))])
  #for j in range(1,min(i+1,k+1)):
   # if j == 1:
    #  dp[i] = dp[i-1]+abs(h[i]-h[i-1])
    #print([i,j], dp[i-j]+abs(h[i]-h[i-j]), dp[i])
    #dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
#print(h)
print(dp[-1])

Fixed submission:
def main():
  n, k = map(int, input().split())
  h = [int(n) for n in 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(0,i-k),i)])  
  print(dp[-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[1] = abs(h[1]-h[0])
for i in range(2, n):
  for j in range(1,min(i+1,k+1)):
    if j == 1:
      dp[i] = dp[i-1]+abs(h[i]-h[i-1])
    #print([i,j], dp[i-j]+abs(h[i]-h[i-j]), dp[i])
    dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
#print(h)
print(dp[-1])

Fixed submission:
def main():
  n, k = map(int, input().split())
  h = [int(n) for n in 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(0,i-k),i)])  
  print(dp[-1])

if __name__ == "__main__":
  main()