Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
h = tuple(map(int,input().split()))

dp = [0] + [float("inf")]*(n-1)
for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i] = min(dp[i],dp[j]+abs(h[i]-h[j]))
print(dp[-1])

Fixed submission:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
h = tuple(map(int,input().split()))

dp = [0] + [float("inf")]*(n-1)
for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i] = min(dp[i],dp[j]+abs(h[i]-h[j]))
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#       )                         .MMdMMM;                    .M#(,dM]                        .:
#       )                        .M@d@d#Mb                    dMGMM(MN                        .:
#       )                        dM1MCdbMM_                  .M@MSN;dM-                       .:
#       )                       .M#(#?d#(Mb.. ...............JMIMIMLdM]                       .:
#       )                      ..M#?NHM# ?MMMMMMMMMH"""""""HMM@(Nad#(Mb                       .:
#       )                  .(MMMMH=..-(Jg[   `               daggmp  MNm,                     .:
#       )                ..MMY`     !qN;                       (J     ?MMN,                   .:
#       )               (MMD`       .MM@                      .MM{      .WMN,                 .:
#       )              dM@`         dMM%                      .MM}        (MMm                .:
#       )             .M#`          dMN!                      .MM}        ~(MMr               .:
#       )            .MM:           dM#                       .MM}        ..JM#               .:
#       )      ``    .M@            dMF                       .MM}        .~(M#   `  .        .:
#       )      ?MMMMMMMNgJ,         .7`                        T"!       .JJjMNgNNMMMB^       .:
#       )            dMr ?Mm,                                            7YYMM#!`             .:
#       )            dMb   ?`                  .,   ` ..                 ~.(MM%               .:
#       )         ..JgMMNNN)                  .Mn.(Mm-JM{               dHHMMNag.,            .:
#       )         ("!  ?MN,..,                (NM@"`~OMM}             ..~-(M@` _?"M[          .:
#       )               ,MMM#_                .M#    .M#`           .~.~JMMMNg-..             .:
#       )           ..gMB"?TMMNJ..              `     _`         .~~.(JdM#^  _"TMMNgJ-,       .:
#       )        .gMB=`      _7WMMNaJ...                 ....__-(JJgMMMM^          ?T"^       .:
#       )           ............  ?TWMMMMNNNNgggggggggggggNNMMMMMMBY7`...                     .:
#       )           WMMMMMMMMMMMMMNgJ,   (dMMMM#"TYYYYYYMMMMMMMNgMMMMMMMMMMMNNgJ-,            .:
#       )            -?TMNmuuuuUWMMMMMMNNMM@g@MMmJ--_(JMMg@g@@gMMMMMMWUXXXUWMMMMMMNe.         .:

import sys
input = sys.stdin.readline

def f(x,y):
    if x in dic:
        dic[x] = min(dic[x],y)
        return
    dic[x] = y

dic = {}
dic[0] = 0
n,k = map(int,input().split())
h = tuple(map(int,input().split()))

for i in range(1,n):
    for j in range(1,min(k,i)+1):
        f(i,dic[i-j]+abs(h[i]-h[i-j]))

print(dic[n-1])


Fixed submission:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
h = tuple(map(int,input().split()))

dp = [0] + [float("inf")]*(n-1)
for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i] = min(dp[i],dp[j]+abs(h[i]-h[j]))
print(dp[-1])

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

dp = [10**9]*n
dp[0] = 0
dp[1] = abs(h[1]-h[0])

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

print(dp[n-1])

Fixed submission:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
h = tuple(map(int,input().split()))

dp = [0] + [float("inf")]*(n-1)
for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i] = min(dp[i],dp[j]+abs(h[i]-h[j]))
print(dp[-1])