Buggy python submission (verdict: Time Limit Exceeded):
'''
                                    *             * * * * * * * *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    * * * * * * * * * * * * * * *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                    * * * * * * * *             *

'''
import sys
sys.setrecursionlimit(10**9)
dp=[-1]*(10**5+2)
dp[0]=0
def frog2(arr,N,K):
    if N==0:
        return dp[0]
    if dp[N]!=-1:
       return dp[N]
    cnt=1000000000
    for i in range(N-1,max(N-K-1,-1),-1):
        cnt=min(cnt,frog2(arr,i,K)+abs(arr[N]-arr[i]))
    dp[N]=cnt
    #print(N,dp[N])
    return dp[N]
if __name__ == "__main__":
    N,K=map(int,input().split())
    h=[int(i) for i in input().split()][:N]
    frog2(h,N-1,K)
    #print(dp)
    print(dp[N-1])

Fixed submission:
from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf') for i in range(N)]
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
'''
                                    *             * * * * * * * *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    * * * * * * * * * * * * * * *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                    * * * * * * * *             *

'''
import sys
sys.setrecursionlimit(10**7)
dp=[-1]*(10**5+2)
dp[0]=0
def frog2(arr,N,K):
    if N==0:
        return dp[0]
    if dp[N]!=-1:
       return dp[N]
    cnt=10**9
    for i in range(N-1,max(N-K-1,-1),-1):
        cnt=min(cnt,frog2(arr,i,K)+abs(arr[N]-arr[i]))
    dp[N]=cnt
    #print(N,dp[N])
    return dp[N]
if __name__ == "__main__":
    N,K=map(int,input().split())
    h=[int(i) for i in input().split()][:N]
    frog2(h,N-1,K)
    #print(dp)
    print(dp[N-1])

Fixed submission:
from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf') for i in range(N)]
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

Buggy python submission (verdict: Runtime Error):
'''
                                    *             * * * * * * * *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    *             *
                                    * * * * * * * * * * * * * * *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                                  *             *
                                    * * * * * * * *             *

'''
import sys
sys.setrecursionlimit(10**9)
dp=[-1]*(10**5+2)
dp[0]=0
def frog2(arr,N,K):
    if N==0:
        return dp[0]
    if dp[N]!=-1:
        return dp[N]
    cnt=10**8
    for i in range(N-1,max(N-K-1,-1),-1):
        cnt=min(cnt,frog2(arr,i,K)+abs(arr[N]-arr[i]))
    dp[N]=cnt
    return cnt
if __name__ == "__main__":
    N,K=map(int,input().split())
    h=[int(i) for i in input().split()][:N]
    frog2(h,N-1,K)
    #print(dp)
    print(dp[N-1])

Fixed submission:
from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf') for i in range(N)]
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf')]*N
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

Fixed submission:
from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf') for i in range(N)]
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
N,K=map(int,input().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf')]*N
dp[0]=0
for i in range(max(N,N-K+1)):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

Fixed submission:
from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf') for i in range(N)]
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
h=[int(i) for i in input().split()][:N]
dp=[float('inf')]*N
dp[0]=0
for i in range(max(N,N-K+1)):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

Fixed submission:
from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf') for i in range(N)]
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])