Buggy python submission (verdict: Time Limit Exceeded):
def test():
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    dp = [float('inf')]*N
    dp[0] = 0
 
    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[-1])
 
if __name__ == '__main__':
    test()

Fixed submission:
n,k=map(int,input().split())
H=list(map(int,input().split()))
 
dp=[float('inf') for _ in range(n)]
dp[0]=0
for i,hi in enumerate(H[1:],1):
  s=max(0,i-k)
  dp[i]=min([aj + abs(hi-hj) for hj,aj in zip(H[s:i],dp[s:i])])
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input  = sys.stdin.readline
 
N,K = map(int,input().split())
hn = list(map(int,input().split()))
rtn = [10**12]*N
K = min(N,K)
if N==K:
  print(abs(hn[0]-hn[N-1]))
else:
  rtn[0]=0
  for i in range(1,N,1):
    sj=min(i,K)
    rtn[i]=min([rtn[i-j]+abs(hn[i]-hn[i-j]) for j in range(sj,0,-1)])
  print(rtn[N-1])

Fixed submission:
n,k=map(int,input().split())
H=list(map(int,input().split()))
 
dp=[float('inf') for _ in range(n)]
dp[0]=0
for i,hi in enumerate(H[1:],1):
  s=max(0,i-k)
  dp[i]=min([aj + abs(hi-hj) for hj,aj in zip(H[s:i],dp[s:i])])
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input  = sys.stdin.readline
 
N,K = map(int,input().split())
hn = list(map(int,input().split()))
rtn = [10**12]*N
K = min(N,K)
if N==K:
  print(abs(hn[0]-hn[N-1]))
else:
  rtn[0]=0
  for i in range(1,N,1):
    sj=min(i,K)
    for j in range(sj,0,-1):
      sub = abs(hn[i]-hn[i-j])
      if sub==0:
        rtn[i]=rtn[i-j]
        break
      else:
        rtn[i]=min(rtn[i],rtn[i-j]+sub)
  print(rtn[N-1])

Fixed submission:
n,k=map(int,input().split())
H=list(map(int,input().split()))
 
dp=[float('inf') for _ in range(n)]
dp[0]=0
for i,hi in enumerate(H[1:],1):
  s=max(0,i-k)
  dp[i]=min([aj + abs(hi-hj) for hj,aj in zip(H[s:i],dp[s:i])])
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input  = sys.stdin.readline

N,K = map(int,input().split())
hn = list(map(int,input().split()))
rtn = [10**12]*N
for i in range(N):
  if i==0:
    rtn[i]=0
  else:
    rtn[i]=rtn[i-1]+abs(hn[i]-hn[i-1])
    for j in range(K,0,-1):
      if rtn[i]==0:
        break
      if i<j:
        continue
      else:
        rtn[i]=min(rtn[i], rtn[i-j]+abs(hn[i]-hn[i-j]))
print(rtn[N-1])

Fixed submission:
n,k=map(int,input().split())
H=list(map(int,input().split()))
 
dp=[float('inf') for _ in range(n)]
dp[0]=0
for i,hi in enumerate(H[1:],1):
  s=max(0,i-k)
  dp[i]=min([aj + abs(hi-hj) for hj,aj in zip(H[s:i],dp[s:i])])
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input  = sys.stdin.readline

N,K = map(int,input().split())
hn = list(map(int,input().split()))
rtn = [0]*N
for i in range(N):
  if i==0:
    rtn[i]=0
  else:
    rtn[i]=rtn[i-1]+abs(hn[i]-hn[i-1])
    for j in range(K):
      if i>j:
        rtn[i]=min(rtn[i], rtn[i-j-1]+abs(hn[i]-hn[i-j-1]))
      else:
        break
print(rtn[N-1])

Fixed submission:
n,k=map(int,input().split())
H=list(map(int,input().split()))
 
dp=[float('inf') for _ in range(n)]
dp[0]=0
for i,hi in enumerate(H[1:],1):
  s=max(0,i-k)
  dp[i]=min([aj + abs(hi-hj) for hj,aj in zip(H[s:i],dp[s:i])])
print(dp[-1])