Problem p03160 (python)

Fixed submission:
import sys
sys.setrecursionlimit(500000)

def get_cost(arr, n, dp):
  if dp[n] != float('inf'):
    return dp[n]
  if n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  else:
    dp[n] = min(
      abs(arr[n] - arr[n-1]) + get_cost(arr, n-1, dp),
      abs(arr[n] - arr[n-2]) + get_cost(arr, n-2, dp)
    )
    return dp[n]


N = int(input())
arr = [int(num) for num in input().split()]
dp_arr = [float('inf')]*N
print(get_cost(arr, N-1, dp_arr))

Buggy submission (verdict: Runtime Error):
def get_cost(arr, n, k, dp):
  if dp[n] is not None:
    return dp[n]
  elif k >= n-1:
    return abs(arr[-1] - arr[0])
  elif n == 0:
    dp[0] = 0
    return dp[0]
  elif n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]

N, K = input().split()
N = int(N)
K = int(K)

dp = [None]*N
print(get_cost(arr, N-1, K, dp)

Buggy submission (verdict: Runtime Error):
def get_cost(arr, n, dp):
  if dp[n] != 0:
    return dp[n]
  if n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  else:
    dp[n] = min(
      abs(arr[n] - arr[n-1]) + get_cost(arr, n-1, dp),
      abs(arr[n] - arr[n-2]) + get_cost(arr, n-2, dp)
    )
    if sum(dp) > 100:
      raise Exception("WRONG", dp)
    return dp[n]


N = int(input())
arr = [int(num) for num in input().split()]
dp_arr = [0]*N
print(get_cost(arr, N-1, dp_arr))

Buggy submission (verdict: Runtime Error):
def get_cost(arr, n, dp):
  if dp[n] != 0:
    return dp[n]
  if n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  if n == 0:
    dp[0] = 0
    return dp[0]
  dp[n] = min(
    abs(arr[n] - arr[n-1]) + get_cost(arr, n-1, dp),
    abs(arr[n] - arr[n-2]) + get_cost(arr, n-2, dp)
  )
  return dp[n]


N = int(input())
arr = [int(num) for num in input().split()]
dp_arr = [0]*N
print(get_cost(arr, N-1, dp_arr))

Buggy submission (verdict: Runtime Error):
def get_cost(arr, n, dp):
  if dp[n] != 0:
    return dp[n]
  if n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  else:
    dp[n] = min(
      abs(arr[n] - arr[n-1]) + get_cost(arr, n-1, dp),
      abs(arr[n] - arr[n-2]) + get_cost(arr, n-2, dp)
    )
    return dp[n]


N = int(input())
arr = [int(num) for num in input().split()]
dp_arr = [0]*N
print(get_cost(arr, N-1, dp_arr))

Buggy submission (verdict: Runtime Error):
def get_cost(arr, n, dp_arr):
  if n == 1:
    return abs(arr[1] - arr[0])
  elif n == 0:
    return 0
  else:
    if dp_arr[n] != 0:
      return dp_arr[n]
    else:
      dp_arr[n] = min(
        abs(arr[n] - arr[n-1]) + get_cost(arr, n-1, dp_arr),
        abs(arr[n] - arr[n-2]) + get_cost(arr, n-2, dp_arr)
      )
      return dp_arr[n]


N = int(input())
arr = [int(num) for num in input().split()]
dp_arr = [0]*N
print(get_cost(arr, N-1, dp_arr))

Buggy submission (verdict: Runtime Error):
def get_cost(arr, n):
  global dp_arr

  if n == 1:
    return abs(arr[1] - arr[0])
  elif n == 0:
    return 0
  else:
    if dp_arr[n] is not 0:
      return dp_arr[n]
    else:
      dp_arr[n] = min(
        abs(arr[n] - arr[n-1]) + get_cost(arr, n-1),
        abs(arr[n] - arr[n-2]) + get_cost(arr, n-2)
      )
      return dp_arr[n]


N = int(input())
arr = [int(num) for num in input().split()]
dp_arr = [0]*(N+1)
print(get_cost(arr, N-1))

Buggy submission (verdict: Runtime Error):
def get_cost(arr, n):
  if n == 1:
    return abs(arr[1] - arr[0])
  elif n == 0:
    return 0
  else:
    return min(
      abs(arr[n] - arr[n-1]) + get_cost(arr, n-1),
      abs(arr[n] - arr[n-2]) + get_cost(arr, n-2)
    )


N = int(input())
arr = [int(num) for num in input().split()]
print(get_cost(arr, N-1))

Buggy submission (verdict: Runtime Error):
def get_cost(arr, n):
  if n == 1:
    return abs(arr[1] - arr[0])
  elif n == 0:
    return 0
  else:
    return min(
      abs(arr[n] - arr[n-1]) + get_cost(arr, n-1),
      abs(arr[n] - arr[n-2]) + get_cost(arr, n-2)
    )


N = int(input())
arr = [int(num) for num in input().split()]
print(get_cost(arr, N-1))

Buggy submission (verdict: Runtime Error):
N = int(input())
arr = [int(num) for num in input().split()]
print(get_cost(arr, N-1))

def get_cost(arr, n):
  if n == 1:
    return abs(arr[1] - arr[0])
  elif n == 0:
    return 0
  else:
    return min(
      abs(arr[n] - arr[n-1]) + get_cost(arr, n-1),
      abs(arr[n] - arr[n-2] + get_cost(arr, n-2)
    )