Buggy python submission (verdict: Runtime Error):
import math

class Input():
	def readInt(self):
		return int(input())

	def readIntArray(self):
		return [int(x) for x in input().split(" ")]

	def readString(self):
		return input()

	def readStringArray(self):
		return input().split(" ")

	def readFloat(self):
		return float(input())

	def readFloatArray(self):
		return [float(x) for x in input().split(" ")]

def main():
	inp = Input()
	n, k = inp.readIntArray()
	height = inp.readIntArray()

	# cost = [1000000002 for i in range(n)]
	# cost[0] = 0

	# for i in range(n):
	# 	for j in range(1, k+1):
	# 		if (i + j) < n:
	# 			cost[i+j] = min(cost[i+j], cost[i] + abs(height[i+j] - height[i]))

	cost = [-1 for i in range(n)]

	def findAns(i):

		if i == 0:
			return 0

		if cost[i] != -1:
			return cost[i]

		start = max(0, i-k)
		ans = 1000000002
		for j in range(start, i):
			ans = min(ans, findAns(j)+abs(height[i] - height[j]))

		cost[i] = ans
		return ans


	print(findAns(n-1))


	# print(cost[n-1])

if __name__ == "__main__":
   main()


Fixed submission:
from sys import stdin
n, k = [int(i) for i in stdin.readline().split(' ')]
h = [int(i) for i in stdin.readline().split(' ')]

minCosts = [0, abs(h[1]-h[0])]
for i in range(2, len(h)):
    minCosts.append(min([abs(h[i]-h[i-j])+minCosts[i-j] for j in range(1, min(i, k)+1)]))

print(minCosts[-1])

Buggy python submission (verdict: Runtime Error):
def woo(x, y):
    res = [0 for i in range(1, x)]
    z = list(map(int, input().split()))
    for i in range(1, x):
        res[i] = min(abs(z[i] - z[j]) + res[j] for j in range(max(0, i - y), i))
    return res

if __name__ == '__main__':
    x, y = map(int, input().split())
    r = woo(x, y)
    f = len(r)-1
    print(r[f])



Fixed submission:
from sys import stdin
n, k = [int(i) for i in stdin.readline().split(' ')]
h = [int(i) for i in stdin.readline().split(' ')]

minCosts = [0, abs(h[1]-h[0])]
for i in range(2, len(h)):
    minCosts.append(min([abs(h[i]-h[i-j])+minCosts[i-j] for j in range(1, min(i, k)+1)]))

print(minCosts[-1])

Buggy python submission (verdict: Time Limit Exceeded):
def woo(x, y):
    res = [0 for i in range(0, x)]
    z = list(map(int, input().split()))
    for i in range(1, x):
        res[i] = min(abs(z[i] - z[j]) + res[j] for j in range(max(0, i - y), i))
    return res

if __name__ == '__main__':
    x, y = map(int, input().split())
    r = woo(x, y)
    f = len(r)-1
    print(r[f])



Fixed submission:
from sys import stdin
n, k = [int(i) for i in stdin.readline().split(' ')]
h = [int(i) for i in stdin.readline().split(' ')]

minCosts = [0, abs(h[1]-h[0])]
for i in range(2, len(h)):
    minCosts.append(min([abs(h[i]-h[i-j])+minCosts[i-j] for j in range(1, min(i, k)+1)]))

print(minCosts[-1])

Buggy python submission (verdict: Time Limit Exceeded):
linha1 = input()
n, k = [int(item) for item in linha1.split()]
linha2 = input()
alturas = [int(item) for item in linha2.split()]
matriz = [[abs(alturas[0] - alturas[1])]]
#matriz[0].append(abs(alturas[0] - alturas[1]))
if n > 2:
    for i in range(1,n-1):
        matriz.append([])
        for j in range(0,min(k, i+1, n-1)):
            if i-j == 0:
                custo_atual = abs(alturas[i+1] - alturas[0])
            else:
                custo_atual = abs(alturas[i+1] - alturas[i-j]) + min(matriz[i-j-1])
            matriz[i].append(custo_atual)
print(min(matriz[-1]))

Fixed submission:
from sys import stdin
n, k = [int(i) for i in stdin.readline().split(' ')]
h = [int(i) for i in stdin.readline().split(' ')]

minCosts = [0, abs(h[1]-h[0])]
for i in range(2, len(h)):
    minCosts.append(min([abs(h[i]-h[i-j])+minCosts[i-j] for j in range(1, min(i, k)+1)]))

print(minCosts[-1])