Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0 for i in range(N)]
def calcmin(n,d):#インデックスがnのノードまでいくための最小コスト（最大移動距離はd）
  p=dp[n-d]+abs(H[n-d]-H[n])
  for q in range(d):
    if p>dp[n-(d-q)]+abs(H[n-(d-q)]-H[n]):
      p=dp[n-(d-q)]+abs(H[n-(d-q)]-H[n])
  return p
for i in range(min(K,N)):
  dp[i]=calcmin(i,i)
for i in range(K,N):
  dp[i]=calcmin(i,K)
print(dp[N-1]) 

Fixed submission:
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0 for i in range(N)]
def calcmin(n,d):#インデックスがnのノードまでいくための最小コスト（最大移動距離はd）
  p=dp[n-d]+abs(H[n-d]-H[n])
  for q in range(d):
    if p>dp[n-(d-q)]+abs(H[n-(d-q)]-H[n]):
      p=dp[n-(d-q)]+abs(H[n-(d-q)]-H[n])
  return p
for i in range(min(K,N)):
  dp[i]=calcmin(i,i)
for i in range(K,N):
  dp[i]=calcmin(i,K)
print(dp[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0 for i in range(N)]
def calcmin(n,d):
  p=dp[n-d]+abs(H[n-d]-H[n])
  for q in range(d):
    if p>dp[n-(d-q)]+abs(H[n-(d-q)]-H[n]):
      p=dp[n-(d-q)]+abs(H[n-(d-q)]-H[n])
  return p
for i in range(min(K,N)):
  dp[i]=dp[0]+abs(H[0]-H[i])
  for j in range(i-1):
    if dp[i]>dp[j]+abs(H[j]-H[i]):
      dp[i]=dp[j]+abs(H[j]-H[i])
for i in range(K,N):
  dp[i]=calcmin(i,K)
print(dp[N-1]) 

Fixed submission:
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0 for i in range(N)]
def calcmin(n,d):#インデックスがnのノードまでいくための最小コスト（最大移動距離はd）
  p=dp[n-d]+abs(H[n-d]-H[n])
  for q in range(d):
    if p>dp[n-(d-q)]+abs(H[n-(d-q)]-H[n]):
      p=dp[n-(d-q)]+abs(H[n-(d-q)]-H[n])
  return p
for i in range(min(K,N)):
  dp[i]=calcmin(i,i)
for i in range(K,N):
  dp[i]=calcmin(i,K)
print(dp[N-1])

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0 for i in range(N)]
def calcmin(n,d):
  dp=dp[n-d]+abs(H[n-d]-H[n])
  for q in range(d):
    if dp>dp[n-(d-q)]+abs(H[n-(d-q)]-H[n]):
      dp=dp[n-(d-q)]+abs(H[n-(d-q)]-H[n])
  return dp
for i in range(K):
  dp[i]=dp[0]+abs(H[0]-H[i])
  for j in range(i-1):
    if dp[i]>dp[j]+abs(H[j]-H[i]):
      dp[i]=dp[j]+abs(H[j]-H[i])
for i in range(K,N):
  dp[i]=calcmin(i,K)
print(dp[N-1])

Fixed submission:
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0 for i in range(N)]
def calcmin(n,d):#インデックスがnのノードまでいくための最小コスト（最大移動距離はd）
  p=dp[n-d]+abs(H[n-d]-H[n])
  for q in range(d):
    if p>dp[n-(d-q)]+abs(H[n-(d-q)]-H[n]):
      p=dp[n-(d-q)]+abs(H[n-(d-q)]-H[n])
  return p
for i in range(min(K,N)):
  dp[i]=calcmin(i,i)
for i in range(K,N):
  dp[i]=calcmin(i,K)
print(dp[N-1])

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0 for i in range(N)]
def calcmin(n,d):
  dp=dp[n-d]+abs(H[n-d]-H[n])
  for q in range(d):
    if dp>dp[n-(d-q)]+abs(H[n-(d-q)]-H[n]):
      dp=dp[n-(d-q)]+abs(H[n-(d-q)]-H[n])
  return dp
for i in range(K):
  dp[i]=dp[0]+abs(H[0]-H[i])
  for j in range(i-1):
    if dp[i]>dp[j]+abs(H[j]-H[i]):
      dp[i]=dp[j]+abs(H[j]-H[i])
for i in range(K,N):
  dp[i]=calcmin(i,K)
print(dp[N-1])

Fixed submission:
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0 for i in range(N)]
def calcmin(n,d):#インデックスがnのノードまでいくための最小コスト（最大移動距離はd）
  p=dp[n-d]+abs(H[n-d]-H[n])
  for q in range(d):
    if p>dp[n-(d-q)]+abs(H[n-(d-q)]-H[n]):
      p=dp[n-(d-q)]+abs(H[n-(d-q)]-H[n])
  return p
for i in range(min(K,N)):
  dp[i]=calcmin(i,i)
for i in range(K,N):
  dp[i]=calcmin(i,K)
print(dp[N-1])