Problem p03160 (python)

Fixed submission:
import math
import sys
sys.setrecursionlimit(10**6)

n = int(input())
mincost = [None]*n

def cost(n,h):
  if (mincost[n-1] is not None):
    return mincost[n-1]
  elif (n == 1):
    mincost[n-1] = 0
  elif (n == 2):
    mincost[n-1] =  abs(h[1] - h[0])
  else:
    mincost[n-1] = min(cost(n-1,h) + abs(h[n-1]-h[n-2]), cost(n-2,h) + abs(h[n-1]-h[n-3]))
  return mincost[n-1]

h = [None]*n

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

print(cost(n,h))

Buggy submission (verdict: Runtime Error):
import math
import sys
sys.setrecursionlimit(10**6)

n, k = input().split()
n = int(n)
k = int(k)
mincost = [None]*n
h = list(map(int, input().split()))

def cost(n):
  if (mincost[n-1] is not None):
    return mincost[n-1]
  elif (n == 1):
    mincost[n-1] = 0
  elif (n == 2):
    mincost[n-1] =  abs(h[1] - h[0])
  else:
    x = [10**5]*k
    for i in range(k):
        if (n-i-2 < 0):
            break
        x[i] = cost(n-i-1) + abs(h[n-1]-h[n-i-2])
    mincost[n-1] = min(x)
  return mincost[n-1]

print(cost(n))

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
mincost = [None]*n

def cost(n,h):
  if (mincost[n-1] is not None):
    return mincost[n-1]
  elif (n == 1):
    mincost[n-1] = 0
  elif (n == 2):
    mincost[n-1] =  abs(h[1] - h[0])
  else:
    mincost[n-1] = min(cost(n-1,h) + abs(h[n-1]-h[n-2]), cost(n-2,h) + abs(h[n-1]-h[n-3]))
  return mincost[n-1]

h = [None]*n

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

print(cost(n,h))

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
mincost = [None]*n

def cost(n,h):
  if (mincost[n-1] is not None):
    return mincost[n-1]
  elif (n == 1):
    mincost[n-1] = 0
  elif (n == 2):
    mincost[n-1] =  abs(h[1] - h[0])
  else:
    mincost[n-1] = min(cost(n-1,h) + abs(h[n-1]-h[n-2]), cost(n-2,h) + abs(h[n-1]-h[n-3]))
  return mincost[n-1]

h = [None]*n

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

print(cost(n,h))

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
mincost = [None]*n

def cost(n,h):
  if (mincost[n-1] is not None):
    return mincost[n-1]
  elif (n == 1):
    mincost[n-1] = 0
  elif (n == 2):
    mincost[n-1] =  abs(h[1] - h[0])
  else:
    mincost[n-1] = min(cost(n-1,h) + abs(h[n-1]-h[n-2]), cost(n-2,h) + abs(h[n-1]-h[n-3]))
  return mincost[n-1]

h = [None]*n

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

print(cost(n,h))

Buggy submission (verdict: Runtime Error):
import math

def cost(n,h,mincost):
  if (n == 1):
    return 0
  elif (n == 2):
    return abs(h[1] - h[0])
  elif (mincost[n-1] is not None):
    return mincost[n]
  else:
  	mincost[n-1] = min(cost(n-1,h,mincost) + abs(h[n-1]-h[n-2]), cost(n-2,h,mincost) + abs(h[n-1]-h[n-3]))
  return mincost[n-1]

n = int(input())
h = [None]*n
mincost = [None]*n
i = 0

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

print(cost(n,h,mincost))

Buggy submission (verdict: Runtime Error):
import math

def cost(n,h,mincost):
  if (n == 1):
    return 0
  elif (n == 2):
    return abs(h(1) - h(0))
  elif (mincost(n) is not None):
    return mincost[n]
  else:
  	mincost[n] = min(cost(n-1,h,mincost) + abs(h[n]-h[n-1]), cost(n-2,h,mincost) + abs(h[n]-h[n-2]))
  return mincost[n]

n = input()
h = [None]*n
mincost = [None]*n
i = 0

while(i<n):
  h[i] = input()
  i = i + 1 

return cost(n,h,mincost)