Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 9)


def dp_func(i):

    #if dp[i] < float('inf'):return dp[i]

    if i==0 :return 0
    #print(i)
    if dp[i] < float('inf'):return dp[i]

    for _ in range(1,K+1):
        if i==1:
            #a[i].append(abs(h[i]-h[i-1]))
            dp[i] = abs(h[i]-h[i-1])
            break

        if((i-_) >= 0):
            #print(i,_,i-_,h[i],h[i-_])
            b = (dp_func(i-_) + abs(h[i]-h[i-_]))
            dp[i] = min(dp[i],b)
            #a[i].append((dp_func(i-_) + abs(h[i]-h[i-_])))


    #print(i,a[i])
    #dp[i] = min(a[i])
    return dp[i]



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

dp = [float('inf')]*N
dp[0] = 0
a = [[] for _ in range(N)]

print(dp_func(N-1))
#print(dp)


Fixed submission:
import sys
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

n, k = map(int, input().split())
h = [int(i) for i in input().split()]
h = np.array(h)

dp = np.zeros(n, dtype=int)

#print(dp)
#print(h)

for i in range(1,n):
    start = max(0,i-k)
    dp[i] = min(dp[start: i] + np.abs(h[i] - h[start: i]))

print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 9)


def dp_func(i):

    if dp[i] < float('inf'):return dp[i]

    if i==0 :return 0
    #print(i)
    if dp[i] < float('inf'):return dp[i]

    for _ in range(1,K+1):
        if i==1:
            a[i].append(abs(h[i]-h[i-1]))
            break

        if((i-_) >= 0):
            #print(i,_,i-_,h[i],h[i-_])
            a[i].append((dp_func(i-_) + abs(h[i]-h[i-_])))


    #print(i,a[i])
    dp[i] = min(a[i])
    return dp[i]



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

dp = [float('inf')]*N
dp[0] = 0
a = [[] for _ in range(N)]

print(dp_func(N-1))
#print(dp)


Fixed submission:
import sys
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

n, k = map(int, input().split())
h = [int(i) for i in input().split()]
h = np.array(h)

dp = np.zeros(n, dtype=int)

#print(dp)
#print(h)

for i in range(1,n):
    start = max(0,i-k)
    dp[i] = min(dp[start: i] + np.abs(h[i] - h[start: i]))

print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 9)


def dp_func(i):

    if dp[i] < float('inf'):return dp[i]

    if i==0 :return 0
    #print(i)

    for _ in range(1,K+1):
        if i==1:
            a[i].append(abs(h[i]-h[i-1]))
            break

        if((i-_) >= 0):
            #print(i,_,i-_,h[i],h[i-_])
            a[i].append((dp_func(i-_) + abs(h[i]-h[i-_])))


    #print(i,a[i])
    dp[i] = min(a[i])
    return dp[i]



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

dp = [float('inf')]*N
dp[0] = 0
a = [[] for _ in range(N)]

print(dp_func(N-1))
#print(dp)


Fixed submission:
import sys
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

n, k = map(int, input().split())
h = [int(i) for i in input().split()]
h = np.array(h)

dp = np.zeros(n, dtype=int)

#print(dp)
#print(h)

for i in range(1,n):
    start = max(0,i-k)
    dp[i] = min(dp[start: i] + np.abs(h[i] - h[start: i]))

print(dp[n-1])