Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
h = list(map(int, input().split()))
c = [0] * n
for i in range(1, n):
    x=h[i]
    c[i] = min([c[j]+abs(x-h[j]) for j in range(max(0, i-k), i)])
print(c[n-1])


Fixed submission:
n = int(input())
h = list(map(int, input().split()))

cost = [0] * (n + 1)
cost[0] = 0
cost[1] = abs(h[1] - h[0])

for i in range(2, n):
  cost1 = cost[i-2] + abs(h[i] - h[i-2])
  cost2 = cost[i-1] + abs(h[i] - h[i-1])
  cost[i] = min(cost1, cost2)
print(cost[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
n = int(input())
h = list(map(int, input().split()))

memo = [-1] * n
def cost(i):
    if i == 0:
        return 0
    elif i == 1:
        return abs(h[i]-h[i-1])
    else:
        if memo[i-1] == -1:
            return min(cost(i-1)+abs(h[i-1]-h[i]), cost(i-2)+abs(h[i-2]-h[i]))
print(cost(n-1))

Fixed submission:
n = int(input())
h = list(map(int, input().split()))

cost = [0] * (n + 1)
cost[0] = 0
cost[1] = abs(h[1] - h[0])

for i in range(2, n):
  cost1 = cost[i-2] + abs(h[i] - h[i-2])
  cost2 = cost[i-1] + abs(h[i] - h[i-1])
  cost[i] = min(cost1, cost2)
print(cost[n-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

memo = [-1] * n
def cost(i):
    if i == 0:
        return 0
    elif i == 1:
        return abs(h[i]-h[i-1])
    else:
        if memo[i-1] == -1:
            return min(cost(i-1)+abs(h[i-1]-h[i]), cost(i-2)+abs(h[i-2]-h[i]))
print(cost(n-1))

Fixed submission:
n = int(input())
h = list(map(int, input().split()))

cost = [0] * (n + 1)
cost[0] = 0
cost[1] = abs(h[1] - h[0])

for i in range(2, n):
  cost1 = cost[i-2] + abs(h[i] - h[i-2])
  cost2 = cost[i-1] + abs(h[i] - h[i-1])
  cost[i] = min(cost1, cost2)
print(cost[n-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

memo = [-1] * n
def cost(i):
  if i == 0:
    memo[i] = 0
  elif i == 1:
    memo[i] = abs(h[1] - h[0])
  else:
    if memo[i] == -1:
      cost1 = cost(i-2) + abs(h[i] - h[i-2])
      cost2 = cost(i-1) + abs(h[i] - h[i-1])
      memo[i] = min(cost1, cost2)
  return memo[i]
print(cost(n-1))

Fixed submission:
n = int(input())
h = list(map(int, input().split()))

cost = [0] * (n + 1)
cost[0] = 0
cost[1] = abs(h[1] - h[0])

for i in range(2, n):
  cost1 = cost[i-2] + abs(h[i] - h[i-2])
  cost2 = cost[i-1] + abs(h[i] - h[i-1])
  cost[i] = min(cost1, cost2)
print(cost[n-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

memo = [-1] * n
def cost(i):
  if i == 0:
    return 0
  elif i == 1:
    return abs(h[1] - h[0])
  else:
    if memo[i] == -1:
      cost1 = cost(i-2) + abs(h[i] - h[i-2])
      cost2 = cost(i-1) + abs(h[i] - h[i-1])
      memo[i] = min(cost1, cost2)
    return memo[i]
print(cost(n-1))

Fixed submission:
n = int(input())
h = list(map(int, input().split()))

cost = [0] * (n + 1)
cost[0] = 0
cost[1] = abs(h[1] - h[0])

for i in range(2, n):
  cost1 = cost[i-2] + abs(h[i] - h[i-2])
  cost2 = cost[i-1] + abs(h[i] - h[i-1])
  cost[i] = min(cost1, cost2)
print(cost[n-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

memo = [-1] * (n + 1)
def cost(i):
  if i == 0:
    return 0
  elif i == 1:
    return abs(h[1] - h[0])
  else:
    if memo[i] == -1:
      cost1 = cost(i-2) + abs(h[i] - h[i-2])
      cost2 = cost(i-1) + abs(h[i] - h[i-1])
      memo[i] = min(cost1, cost2)
    return memo[i]
print(cost(n-1))

Fixed submission:
n = int(input())
h = list(map(int, input().split()))

cost = [0] * (n + 1)
cost[0] = 0
cost[1] = abs(h[1] - h[0])

for i in range(2, n):
  cost1 = cost[i-2] + abs(h[i] - h[i-2])
  cost2 = cost[i-1] + abs(h[i] - h[i-1])
  cost[i] = min(cost1, cost2)
print(cost[n-1])