Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
S = [0] * N
S[0] = 0
 
for i in range(1, N):
    value = S[i - 1] + abs(h[i] - h[i - 1])
    maxStep = min(K, i)
    for step in range(1, maxStep + 1):
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
    S[i] = value
print(S[N - 1])

Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
import math
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
S = [0] * N
S[0] = 0
 
for i in range(1, N):
    value = math.inf
    maxStep = min(K, i)
    for step in range(1, maxStep + 1):
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
    S[i] = value
print(S[N - 1])

Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])

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

S = [0] * N
S[0] = 0

for i in range(1, N):
    value = math.inf
    for step in range(1, K + 1):
        if i - step < 0:
            break
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
    S[i] = value
print(S[N - 1])


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])

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

    S = [0] * N
    S[0] = 0

    for i in range(1, N):
        value = S[i - 1] + abs(h[i] - h[i - 1])
        for step in range(1, K + 1):
            # print("i, k:", i, k)
            if i - step < 0:
                break
            value = min(value, S[i - step] + abs(h[i] - h[i - step]))
            #print("value", value)
        S[i] = value
        # print(S)
    # print(S)
    print(S[N - 1])

if __name__ == "__main__":
    main()


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])

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

S = [0] * N
S[0] = 0

for i in range(1, N):
    value = S[i - 1] + abs(h[i] - h[i - 1])
    for step in range(1, K + 1):
        # print("i, k:", i, k)
        if i - step < 0:
            break
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
        #print("value", value)
    S[i] = value
    # print(S)
# print(S)
print(S[N - 1])


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])

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

S = [0] * N
S[0] = 0

for i in range(1, N):
    value = S[i - 1] + abs(h[i] - h[i - 1])
    for step in range(1, K + 1):
        # print("i, k:", i, k)
        if i - step < 0:
            break
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
        #print("value", value)
    S[i] = value
    # print(S)
# print(S)
print(S[N - 1])


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])

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

S = [0] * N
S[0] = 0

for i in range(1, N):
    value = math.inf
    for step in range(1, K + 1):
        # print("i, k:", i, k)
        if i - step < 0:
            break
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
        #print("value", value)
    S[i] = value
    # print(S)
# print(S)
print(S[N - 1])


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])

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

S = [0] * N
S[0] = 0

for i in range(1, N):
    value = math.inf
    for k in range(1, K + 1):
        # print("i, k:", i, k)
        if i - k < 0:
            break
        value = min(value, S[i - k] + abs(h[i] - h[i - k]))
        #print("value", value)
    S[i] = value
    # print(S)
# print(S)
print(S[N - 1])


Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])