Buggy python submission (verdict: Runtime Error):
def pathSmMax(matrix,i,j,cache):
    if i > len(matrix)-1: return 0
    happiness = 0
    if cache[i][j] != 0:
        return cache[i][j]
    for k in range(3):
        if k != j:
            cache[i][j] = max(cache[i][j],pathSmMax(matrix,i+1,k,cache) + matrix[i][j])
    return cache[i][j]
N = int(input())
matrix = []
for i in range(N):
    matrix.append([int(i) for i in input().split()])
maxs = -float("inf")
cache = [[0 for i in range(4)] for j in range(len(matrix)+1)]
for i in range(len(matrix)):
    maxs = max(maxs,pathSmMax(matrix,0,i,cache))
print(maxs)

Fixed submission:
N = int(input())
matrix = []
for i in range(N):
    matrix.append([int(i) for i in input().split()])
ans = -float("inf")
dp = [[0 for i in range(4)] for j in range(len(matrix)+1)]
for i in range(len(matrix)-1,-1,-1):
    for j in range(len(matrix[0])-1,-1,-1):
        dp[i][j] = -float("inf")
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j],dp[i+1][k]+matrix[i][j])
        ans = max(ans,dp[i][j])
print(ans)

Buggy python submission (verdict: Runtime Error):
def pathSmMax(matrix,i,j,cache):
    if i > len(matrix)-1: return 0
    happiness = 0
    if cache[i][j] != 0:
        return cache[i][j]
    for k in range(3):
        if k != j:
            happiness = max(happiness,pathSmMax(matrix,i+1,k,cache) + matrix[i][j])
    cache[i][j] = happiness
    return cache[i][j]
N = int(input())
matrix = []
for i in range(N):
	matrix.append([int(i) for i in input().split()])
maxs = -float("inf")
cache = [[0 for i in range(4)] for j in range(len(matrix)+1)]
for i in range(len(matrix)):
    maxs = max(maxs,pathSmMax(matrix,0,i,cache))
print(maxs)

Fixed submission:
N = int(input())
matrix = []
for i in range(N):
    matrix.append([int(i) for i in input().split()])
ans = -float("inf")
dp = [[0 for i in range(4)] for j in range(len(matrix)+1)]
for i in range(len(matrix)-1,-1,-1):
    for j in range(len(matrix[0])-1,-1,-1):
        dp[i][j] = -float("inf")
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j],dp[i+1][k]+matrix[i][j])
        ans = max(ans,dp[i][j])
print(ans)

Buggy python submission (verdict: Runtime Error):
def pathSmMax(matrix,i,j,cache):
    if i < 0 or j < 0 or i > len(matrix)-1 or j > len(matrix[0])-1: return 0
    happiness = -float("inf")
    if cache[i][j] != 0:
        return cache[i][j]
    for k in range(3):
        if k != j:
            happiness = max(happiness,pathSmMax(matrix,i+1,k,cache) + matrix[i][j])
    cache[i][j] = happiness
    return cache[i][j]
N = int(input())
matrix = []
for i in range(N):
    matrix.append([int(i) for i in input().split()])
maxs = -float("inf")
cache = [[0 for i in range(len(matrix[0])+1)] for j in range(len(matrix)+1)]
for i in range(len(matrix)):
    maxs = max(maxs,pathSmMax(matrix,0,i,cache))
print(maxs)

Fixed submission:
N = int(input())
matrix = []
for i in range(N):
    matrix.append([int(i) for i in input().split()])
ans = -float("inf")
dp = [[0 for i in range(4)] for j in range(len(matrix)+1)]
for i in range(len(matrix)-1,-1,-1):
    for j in range(len(matrix[0])-1,-1,-1):
        dp[i][j] = -float("inf")
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j],dp[i+1][k]+matrix[i][j])
        ans = max(ans,dp[i][j])
print(ans)