Buggy python submission (verdict: Runtime Error):
N = int(raw_input())
act = [map(int, raw_input().split()) for _ in range(N)]
memo = [[-1]*3 for _ in range(N)]

def happy(n, i):
  if memo[n][i] >= 0:
    return memo[n][i]
  if n == 0:
    h = act[n][i]
  else:
    h = -1
    for j in range(3):
      if j == i:
        continue
      c = happy(n-1,j)+act[n][i]
      h = h if h > c else c
  memo[n][i] = h
  return h
print max(happy(N-1,0),happy(N-1,1),happy(N-1,2))



Fixed submission:
import sys

sys.setrecursionlimit(100000)

N = int(raw_input())
H = map(int, raw_input().split())
cost = [-1]*N

def sumOfCost(i):
  if cost[i] >= 0:
    return cost[i]
  if i == 0:
    soc=0
  elif i == 1:
    soc= sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    soc=min(c1,c2)
  cost[i] = soc
  return soc

print sumOfCost(N-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(100000)

N = int(raw_input())
H = map(int, raw_input().split())

def sumOfCost(i):
  if i == 0:
    return 0
  elif i == 1:
    return sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    return min(c1,c2)
print sumOfCost(N-1)




Fixed submission:
import sys

sys.setrecursionlimit(100000)

N = int(raw_input())
H = map(int, raw_input().split())
cost = [-1]*N

def sumOfCost(i):
  if cost[i] >= 0:
    return cost[i]
  if i == 0:
    soc=0
  elif i == 1:
    soc= sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    soc=min(c1,c2)
  cost[i] = soc
  return soc

print sumOfCost(N-1)

Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(100000)

N = int(raw_input())
H = map(int, raw_input().split())

def sumOfCost(i):
  if i == 0:
    return 0
  elif i == 1:
    return sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    return min(c1,c2)
print sumOfCost(N-1)




Fixed submission:
import sys

sys.setrecursionlimit(100000)

N = int(raw_input())
H = map(int, raw_input().split())
cost = [-1]*N

def sumOfCost(i):
  if cost[i] >= 0:
    return cost[i]
  if i == 0:
    soc=0
  elif i == 1:
    soc= sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    soc=min(c1,c2)
  cost[i] = soc
  return soc

print sumOfCost(N-1)

Buggy python submission (verdict: Runtime Error):
N = int(raw_input())
H = map(int, raw_input().split())

def sumOfCost(i):
  if i == 0:
    return 0
  elif i == 1:
    return sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    return min(c1,c2)
print sumOfCost(N-1)




Fixed submission:
import sys

sys.setrecursionlimit(100000)

N = int(raw_input())
H = map(int, raw_input().split())
cost = [-1]*N

def sumOfCost(i):
  if cost[i] >= 0:
    return cost[i]
  if i == 0:
    soc=0
  elif i == 1:
    soc= sumOfCost(0)+abs(H[i]-H[0])
  else:
    c1 = sumOfCost(i-1)+abs(H[i]-H[i-1])
    c2 = sumOfCost(i-2)+abs(H[i]-H[i-2])
    soc=min(c1,c2)
  cost[i] = soc
  return soc

print sumOfCost(N-1)