Buggy python submission (verdict: Time Limit Exceeded):
import sys
n,k = map(int,input().split( ))
if k>n:
    flag =1
else:
    flag =0
    
k = min(k,n)
h = list(map(int, input().split( )))
dp =[abs(h[i]-h[0]) for i in range(k)]
for i in range(2,k):
    tmp =[]
    for j in range(i+1):
        #tmp =[]
        #print(*dp)
        #print(dp[j])
        #print(*h)
        #print(h[i],h[j],i,j)
        tmp.append(dp[j]+abs(h[i]-h[j]))#こっちはdp[j],h[j]
    dp[i]= min(tmp)
if flag:
    print(dp[-1])
    sys.exit()
for i in range(k,n):
    tmp =[]
    for j in range(1,k+1):#+1
        tmp.append(dp[i-j]+abs(h[i]-h[i-j]))#こっちはdp[i-j],h[i-j]
    dp.append(min(tmp))
#print(*dp)
print(dp[n-1])


Fixed submission:
import sys
n,k = map(int,input().split( ))
if k>n:
    flag =1
else:
    flag =0
    
k = min(k,n)
h = list(map(int, input().split( )))
dp =[abs(h[i]-h[0]) for i in range(k)]
for i in range(2,k):
    tmp =[]
    for j in range(i+1):
        #tmp =[]
        #print(*dp)
        #print(dp[j])
        #print(*h)
        #print(h[i],h[j],i,j)
        tmp.append(dp[j]+abs(h[i]-h[j]))#こっちはdp[j],h[j]
    dp[i]= min(tmp)
if flag:
    print(dp[-1])
    sys.exit()
for i in range(k,n):
    tmp =[]
    for j in range(1,k+1):#+1
        tmp.append(dp[i-j]+abs(h[i]-h[i-j]))#こっちはdp[i-j],h[i-j]
    dp.append(min(tmp))
#print(*dp)
print(dp[n-1])

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split( ))
h = list(map(int, input().split( )))
dp =[abs(h[i]-h[0]) for i in range(k)]
for i in range(2,k):
    tmp =[]
    for j in range(i+1):
        #tmp =[]
        #print(*dp)
        #print(dp[j])
        #print(*h)
        #print(h[i],h[j],i,j)
        tmp.append(dp[j]+abs(h[i]-h[j]))#こっちはdp[j],h[j]
    dp[i]= min(tmp)
for i in range(k,n):
    tmp =[]
    for j in range(1,k+1):#+1
        tmp.append(dp[i-j]+abs(h[i]-h[i-j]))#こっちはdp[i-j],h[i-j]
    dp.append(min(tmp))
#print(*dp)
print(dp[n-1])


Fixed submission:
import sys
n,k = map(int,input().split( ))
if k>n:
    flag =1
else:
    flag =0
    
k = min(k,n)
h = list(map(int, input().split( )))
dp =[abs(h[i]-h[0]) for i in range(k)]
for i in range(2,k):
    tmp =[]
    for j in range(i+1):
        #tmp =[]
        #print(*dp)
        #print(dp[j])
        #print(*h)
        #print(h[i],h[j],i,j)
        tmp.append(dp[j]+abs(h[i]-h[j]))#こっちはdp[j],h[j]
    dp[i]= min(tmp)
if flag:
    print(dp[-1])
    sys.exit()
for i in range(k,n):
    tmp =[]
    for j in range(1,k+1):#+1
        tmp.append(dp[i-j]+abs(h[i]-h[i-j]))#こっちはdp[i-j],h[i-j]
    dp.append(min(tmp))
#print(*dp)
print(dp[n-1])

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split( ))
h = list(map(int, input().split( )))
dp =[abs(h[i]-h[0]) for i in range(k)]
for i in range(2,k):
    tmp =[]
    for j in range(i+1):
        #tmp =[]
        #print(*dp)
        #print(dp[j])
        #print(*h)
        #print(h[i],h[j],i,j)
        tmp.append(dp[j]+abs(h[i]-h[j]))#こっちはdp[j],h[j]
    dp[i]= min(tmp)
for i in range(k,n):
    tmp =[]
    for j in range(1,k+1):#+1
        tmp.append(dp[i-j]+abs(h[i]-h[i-j]))#こっちはdp[i-j],h[i-j]
    dp.append(min(tmp))
print(*dp)
print(dp[n-1])


Fixed submission:
import sys
n,k = map(int,input().split( ))
if k>n:
    flag =1
else:
    flag =0
    
k = min(k,n)
h = list(map(int, input().split( )))
dp =[abs(h[i]-h[0]) for i in range(k)]
for i in range(2,k):
    tmp =[]
    for j in range(i+1):
        #tmp =[]
        #print(*dp)
        #print(dp[j])
        #print(*h)
        #print(h[i],h[j],i,j)
        tmp.append(dp[j]+abs(h[i]-h[j]))#こっちはdp[j],h[j]
    dp[i]= min(tmp)
if flag:
    print(dp[-1])
    sys.exit()
for i in range(k,n):
    tmp =[]
    for j in range(1,k+1):#+1
        tmp.append(dp[i-j]+abs(h[i]-h[i-j]))#こっちはdp[i-j],h[i-j]
    dp.append(min(tmp))
#print(*dp)
print(dp[n-1])