Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

n, k = map(int, input().split())
h = list(map(int, input().split()))

# dp = [float('inf') for i in range(len(h))]
dp = [0] * n
dp[0] = 0
# dp[1] = abs(h[0] - h[1])
# print(dp)

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

Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    a = [0] * n
    a[0] = 0
    for i in range(1, n):
        a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)])
    print(a[-1])
 
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

n, k = map(int, input().split())
h = list(map(int, input().split()))

# dp = [float('inf') for i in range(len(h))]
dp = [float('inf')] * n
dp[0] = 0
# dp[1] = abs(h[0] - h[1])
# print(dp)

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

Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    a = [0] * n
    a[0] = 0
    for i in range(1, n):
        a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)])
    print(a[-1])
 
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

n, k = map(int, input().split())
h = list(map(int, input().split()))

# dp = [float('inf') for i in range(len(h))]
dp = [float('inf')] * n
dp[0] = 0
# dp[1] = abs(h[0] - h[1])
# print(dp)

for i in range(len(h)):
    for j in range(1, min(k + 1, n - i)):
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
            
        # print(dp)
        
print(dp[len(dp) - 1])

Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    a = [0] * n
    a[0] = 0
    for i in range(1, n):
        a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)])
    print(a[-1])
 
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [-1 for i in range(len(h))]
dp[0] = 0

# print(dp)

for i in range(len(h)):
    # print(i)
    for j in range(1, k + 1):
        if i + j < len(dp):
            if dp[i + j] == -1:
                dp[i + j] = dp[i] + abs(h[i] - h[i + j])
            else:
                dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
            # print(dp)
        
print(dp[len(dp) - 1])

Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    a = [0] * n
    a[0] = 0
    for i in range(1, n):
        a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)])
    print(a[-1])
 
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [9999999999 for i in range(len(h))]
dp[0] = 0

# print(dp)

for i in range(len(h)):
    # print(i)
    for j in range(1, k + 1):
        if i + j < len(dp):
            dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
            # print(dp)
        
print(dp[len(dp) - 1])

Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    a = [0] * n
    a[0] = 0
    for i in range(1, n):
        a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)])
    print(a[-1])
 
if __name__ == '__main__':
    main()