Problem p03160 (python)

Fixed submission:
import sys

sys.setrecursionlimit(1000000)

N = int(input())
h = list(map(int, input().split()))

memo = [10**10 for _ in range(N+1)]

def dp(i):
    if i == 1:
        return 0
    elif i == 2:
        memo[i] = abs(h[i-2]-h[i-1])
        return memo[i]
    elif memo[i] < 10**10:
        return memo[i]
    else:
        memo[i] = min(
            dp(i-1) + abs(h[i-2]-h[i-1]),
            dp(i-2) + abs(h[i-3]-h[i-1]),
            dp(i-2) + abs(h[i-3]-h[i-2]) + abs(h[i-2]-h[i-1])
        )
        return memo[i]
print(dp(N))

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

cost = [0, abs(h[1]-h[0])]
def dp(i):
  if i > len(cost):
    cost.append(min(dp(i-1)+abs(h[i]-h[i-1]), dp(i-2)+abs(h[i]-h[i-2])))
  return cost[i]

print(dp(N-1))

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

cost = [0, abs(h[1]-h[0])]
def dp(i):
  if i >= len(cost):
    cost.append(min(dp(i-1)+abs(h[i]-h[i-1]), dp(i-2)+abs(h[i]-h[i-2])))
  return cost[i]

print(dp(N-1))

Buggy 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):
    c[i] = min([c[j] + abs(h[i]-h[j]) for j in range(max(0, i-K), i)])
print(c[N-1])

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

memo = [-1 for _ in range(N)]

def dp(n):
  if memo[n] != -1:
    return memo[n]
  elif n != 0:
    m = min([dp(n-i)+abs(h[n]-h[n-i]) for i in range(1, min(K, n)+1)])
    memo[n] = m
    return m
  else:
    memo[n] = 0
    return memo[n]

for n in range(N-1):
  dp(n)
print(dp(N-1))

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

memo = [-1 for _ in range(N+1)]

def dp(i):
    if i == 1:
        return 0
    elif i == 2:
        memo[i] = abs(h[i-2]-h[i-1])
        return memo[i]
    elif memo[i] >= 0:
        return memo[i]
    else:
        memo[i] = min(
            dp(i-1) + abs(h[i-2]-h[i-1]),
            dp(i-2) + abs(h[i-3]-h[i-1]),
        )
        return memo[i]
print(dp(N))

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

memo = [10**10 for _ in range(N+1)]

def dp(i):
    if i == 1:
        return 0
    elif i == 2:
        memo[i] = abs(h[i-2]-h[i-1])
        return memo[i]
    elif memo[i] < 10**10:
        return memo[i]
    else:
        memo[i] = min(
            dp(i-1) + abs(h[i-2]-h[i-1]),
            dp(i-2) + abs(h[i-3]-h[i-1]),
            dp(i-2) + abs(h[i-3]-h[i-2]) + abs(h[i-2]-h[i-1])
        )
        return memo[i]
try:
    print(dp(N))
except IndexError:
    print(0)

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

memo = [10**10 for _ in range(N+1)]

def dp(i):
    if i == 1:
        return 0
    elif i == 2:
        memo[i] = abs(h[i-2]-h[i-1])
        return memo[i]
    elif memo[i] < 10**10:
        return memo[i]
    else:
        memo[i] = min(
            dp(i-1) + abs(h[i-2]-h[i-1]),
            dp(i-2) + abs(h[i-3]-h[i-1]),
            dp(i-2) + abs(h[i-3]-h[i-2]) + abs(h[i-2]-h[i-1])
        )
        return memo[i]
print(dp(N))