Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000000)


N,K=map(int,input().split())
h=list(map(int,input().split()))

INF=float("inf")

DP=[INF]*(N+1)
DP[0]=0

def rec(n):
    if DP[n]!=INF:return DP[n]
    res=INF
    for i in range(1,K+1):
        if n-i<0:break
        res=min(res,rec(n-i)+abs(h[n-i]-h[n]))
    DP[n]=res
    return res

ans=rec(N-1)

print(ans)


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))

DP=[10000000000]*N

DP[0]=0
for i in range(1,N):
    temp=[]
    if i<K:
        for j in range(i):
            temp.append(DP[j]+abs(h[j]-h[i]))
    else:
        for j in range(1,K+1):
            temp.append(DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=min(temp)

print(DP[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000000)


N,K=map(int,input().split())
h=list(map(int,input().split()))

INF=float("inf")

DP=[INF]*(N+1)
DP[0]=0

def rec(n):
    if DP[n]!=INF:return DP[n]
    res=INF
    for i in range(1,K+1):
        if n-i<0:break
        res=min(res,rec(n-i)+abs(h[n-i]-h[n]))
    DP[n]=res
    return res

ans=rec(N-1)

print(ans)


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))

DP=[10000000000]*N

DP[0]=0
for i in range(1,N):
    temp=[]
    if i<K:
        for j in range(i):
            temp.append(DP[j]+abs(h[j]-h[i]))
    else:
        for j in range(1,K+1):
            temp.append(DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=min(temp)

print(DP[N-1])

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000000)


N,K=map(int,input().split())
h=list(map(int,input().split()))

INF=float("inf")

DP=[INF]*(N+1)
DP[0]=0

def rec(n):
    if DP[n]!=INF:return DP[n]
    res=INF
    for i in range(1,K+1):
        if n-i<0:break
        res=min(res,rec(n-i)+abs(h[n-i]-h[n]))
    DP[n]=res
    return res

ans=rec(N-1)

print(ans)


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))

DP=[10000000000]*N

DP[0]=0
for i in range(1,N):
    temp=[]
    if i<K:
        for j in range(i):
            temp.append(DP[j]+abs(h[j]-h[i]))
    else:
        for j in range(1,K+1):
            temp.append(DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=min(temp)

print(DP[N-1])

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


N,K=map(int,input().split())
h=list(map(int,input().split()))

DP=[10000000000]*N

DP[0]=0
for i in range(1,N):
    temp=[]
    if i<K:
        for j in range(i):
            temp.append(DP[j]+abs(h[j]-h[i]))
    else:
        for j in range(1,K+1):
            temp.append(DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=min(temp)

print(DP[N-1])

Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))

DP=[10000000000]*N

DP[0]=0
for i in range(1,N):
    temp=[]
    if i<K:
        for j in range(i):
            temp.append(DP[j]+abs(h[j]-h[i]))
    else:
        for j in range(1,K+1):
            temp.append(DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=min(temp)

print(DP[N-1])

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

DP=[10000000000]*N

DP[0]=0
for i in range(1,N):
    temp=[]
    if i<K:
        for j in range(i):
            temp.append(DP[j]+abs(h[j]-h[i]))
    else:
        for j in range(1,K+1):
            temp.append(DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=min(temp)

print(DP[N-1])

Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))

DP=[10000000000]*N

DP[0]=0
for i in range(1,N):
    temp=[]
    if i<K:
        for j in range(i):
            temp.append(DP[j]+abs(h[j]-h[i]))
    else:
        for j in range(1,K+1):
            temp.append(DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=min(temp)

print(DP[N-1])