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

def main():
    global n, peso, dp, k

    n, k = [int(x) for x in input().split()]
    peso = [int(x) for x in input().split()]
    dp = [-1]*(10**5 + 1)
    
    peso.append(10**9)
    peso.append(10**9)
    
    print(frog(0))
    
def frog(i):
    global n, peso, dp, k
    
    if i >= n - 1:
        return 0

    if dp[i] != -1:
        return dp[i]
    
    dp[i] = 10**9
    for j in range(1, k + 1):
        if i + j < len(peso):
            dp[i] = min(dp[i], abs(peso[i + j] - peso[i]) + frog(i + j))
    
    return dp[i]

main()

Fixed submission:
n, k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

def abs(x):
    if x >= 0:
        return x
    return -x

c = [0]
c.append(abs(h[1] - h[0]))
for i in range(2, n):
    cmin = None
    for cur in range(i-1, i-1-k, -1):
        if cur < 0:
            break
        val = abs(h[i] - h[cur]) + c[cur]
        if cmin is None or val < cmin:
            cmin = val
    c.append(cmin)

print(c[-1])

Buggy python submission (verdict: Runtime Error):
    n = stdin.readline()
    n, K = n.split(' ')
    n = int(n)
    K = int(K)
    arr = stdin.readline()
    arr = [int(a) for a in arr.split(' ')]
    dp = [float('inf')]*n
    dp[0] = 0
    for i in range(n):
        for k in range(1, K+1):
            if i - k >= 0:
                dp[i] = min(dp[i], dp[i-k]+abs(arr[i]-arr[i-k]))
    print(dp[-1])

Fixed submission:
n, k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

def abs(x):
    if x >= 0:
        return x
    return -x

c = [0]
c.append(abs(h[1] - h[0]))
for i in range(2, n):
    cmin = None
    for cur in range(i-1, i-1-k, -1):
        if cur < 0:
            break
        val = abs(h[i] - h[cur]) + c[cur]
        if cmin is None or val < cmin:
            cmin = val
    c.append(cmin)

print(c[-1])

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 = [math.inf 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]))

	print(cost[n-1])
	

if __name__ == "__main__":
   main()


Fixed submission:
n, k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

def abs(x):
    if x >= 0:
        return x
    return -x

c = [0]
c.append(abs(h[1] - h[0]))
for i in range(2, n):
    cmin = None
    for cur in range(i-1, i-1-k, -1):
        if cur < 0:
            break
        val = abs(h[i] - h[cur]) + c[cur]
        if cmin is None or val < cmin:
            cmin = val
    c.append(cmin)

print(c[-1])

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 = [math.inf for i in range(n)]
	cost[0] = 0

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

	print(cost[n-1])
	

if __name__ == "__main__":
   main()


Fixed submission:
n, k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

def abs(x):
    if x >= 0:
        return x
    return -x

c = [0]
c.append(abs(h[1] - h[0]))
for i in range(2, n):
    cmin = None
    for cur in range(i-1, i-1-k, -1):
        if cur < 0:
            break
        val = abs(h[i] - h[cur]) + c[cur]
        if cmin is None or val < cmin:
            cmin = val
    c.append(cmin)

print(c[-1])

Buggy python submission (verdict: Runtime Error):
res = [0]*x

def woo(x, y):
    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:
n, k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

def abs(x):
    if x >= 0:
        return x
    return -x

c = [0]
c.append(abs(h[1] - h[0]))
for i in range(2, n):
    cmin = None
    for cur in range(i-1, i-1-k, -1):
        if cur < 0:
            break
        val = abs(h[i] - h[cur]) + c[cur]
        if cmin is None or val < cmin:
            cmin = val
    c.append(cmin)

print(c[-1])

Buggy python submission (verdict: Time Limit Exceeded):
def test():
  N, K = map(int, input().split())
  H = list(map(int, input().split()))
  dp = [float('inf')]*N
  dp[0] = 0

  for i in range(1, N):
    dp[i] = min(dp[j] + abs(H[i] - H[j]) 
    for j in range(max(i-K, 0), i))

  print(dp[-1])
 
if __name__ == '__main__':
  test()

Fixed submission:
n, k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

def abs(x):
    if x >= 0:
        return x
    return -x

c = [0]
c.append(abs(h[1] - h[0]))
for i in range(2, n):
    cmin = None
    for cur in range(i-1, i-1-k, -1):
        if cur < 0:
            break
        val = abs(h[i] - h[cur]) + c[cur]
        if cmin is None or val < cmin:
            cmin = val
    c.append(cmin)

print(c[-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 = []
for i in range(0,n-1):
    matriz.append([])

matriz[0].append(abs(alturas[0] - alturas[1]))
if n > 2:
    for i in range(1,n-1):
        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:
n, k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

def abs(x):
    if x >= 0:
        return x
    return -x

c = [0]
c.append(abs(h[1] - h[0]))
for i in range(2, n):
    cmin = None
    for cur in range(i-1, i-1-k, -1):
        if cur < 0:
            break
        val = abs(h[i] - h[cur]) + c[cur]
        if cmin is None or val < cmin:
            cmin = val
    c.append(cmin)

print(c[-1])

Buggy python submission (verdict: Time Limit Exceeded):
# Problem A - Frog 2 - Algoritmos e Grafos - MATA52
def frog2(n, h, k_max):
    global stones
    for i in range(n - 1):
        for j in range(k_max):
            if i + j + 1 <= n - 1:
                w1 = stones[i + j + 1]
                w2 = abs(h[i + j + 1] - h[i])
                stones[i + j + 1] = min(w1, stones[i] + w2)
            else:
                break
    return stones[n - 1]

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

stones = [0] + [float("inf") for x in range(n + k_max)]
print(frog2(n, h, k_max))

Fixed submission:
n, k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]

def abs(x):
    if x >= 0:
        return x
    return -x

c = [0]
c.append(abs(h[1] - h[0]))
for i in range(2, n):
    cmin = None
    for cur in range(i-1, i-1-k, -1):
        if cur < 0:
            break
        val = abs(h[i] - h[cur]) + c[cur]
        if cmin is None or val < cmin:
            cmin = val
    c.append(cmin)

print(c[-1])