Problem p03162 (python)

Fixed submission:
n = int(input())
a,b,c = [],[],[]
for i in range(n):
    line = input()
    line = line.strip("\n").split(" ")
    a.append(int(line[0]))
    b.append(int(line[1]))
    c.append(int(line[2]))

def bottomup(a, b, c):
    dp = [[0 for _ in range(3)] for _ in range(n)]
    
    dp[(n-1)] = [a[n-1], b[n-1], c[n-1]]

    for i in range(n-2,-1,-1):
        dp[i][0] = a[i] + max(dp[i+1][1:])
        dp[i][1] = b[i] + max(dp[i+1][0], dp[i+1][2])
        dp[i][2] = c[i] + max(dp[i+1][:2])
    
    return max(dp[0])
    
print(bottomup(a,b,c))

Buggy submission (verdict: Runtime Error):
n = int(input())
a,b,c = [],[],[]
for i in range(n):
    line = input()
    line = line.strip("\n").split(" ")
    a.append(int(line[0]))
    b.append(int(line[1]))
    c.append(int(line[2]))

def helper(i, prev=None, memo={}):
    if (i,prev) in memo: return memo[(i,prev)]

    if i >= n: return 0

    happ1 = 0 if prev == 'a' else a[i] + helper(i+1, 'a', memo)
    happ2 = 0 if prev == 'b' else b[i] + helper(i+1, 'b', memo)
    happ3 = 0 if prev == 'c' else c[i] + helper(i+1, 'c', memo)

    memo[(i,prev)] = max(happ1, happ2, happ3)
    return memo[(i, prev)]

print(helper(0))

Buggy submission (verdict: Runtime Error):
n = int(input())
a,b,c = [],[],[]
for i in range(n):
    line = input()
    line = line.strip("\n").split(" ")
    a.append(int(line[0]))
    b.append(int(line[1]))
    c.append(int(line[2]))

def helper(i, prev=None, memo={}):

    if (i,prev) in memo: return memo[(i,prev)]

    if i >= n:
        return 0

    happ1, happ2, happ3 = 0,0,0
    if prev != 'a':
        happ1 = a[i] + helper(i+1, 'a', memo)
    if prev != 'b':
        happ2 = b[i] + helper(i+1, 'b', memo)
    if prev != 'c':
        happ3 = c[i] + helper(i+1, 'c', memo)

    ret = max(happ1, happ2, happ3)
    memo[(i,prev)] = ret
    return ret

print(helper(0))

Buggy submission (verdict: Runtime Error):
n = int(input())
a,b,c = [],[],[]
for i in range(n):
    line = input()
    line = line.strip("\n").split(" ")
    a.append(int(line[0]))
    b.append(int(line[1]))
    c.append(int(line[2]))

def helper(i, prev=None, memo={}):

    if (i,prev) in memo: return memo[(i,prev)]

    if i == n:
        return 0

    happ1, happ2, happ3 = 0,0,0
    if prev != 'a':
        happ1 = a[i] + helper(i+1, 'a', memo)
    if prev != 'b':
        happ2 = b[i] + helper(i+1, 'b', memo)
    if prev != 'c':
        happ3 = c[i] + helper(i+1, 'c', memo)

    ret = max(happ1, happ2, happ3)
    memo[(i,prev)] = ret
    return ret

print(helper(0))

Buggy submission (verdict: Runtime Error):
n = int(input())
a,b,c = [],[],[]
for i in range(n):
    line = input()
    line = line.strip("\n").split(" ")
    a.append(int(line[0]))
    b.append(int(line[1]))
    c.append(int(line[2]))

memo = {}
def helper(i, prev=None, memo={}):

    if (i,prev) in memo: return memo[(i,prev)]

    if i == n:
        return 0

    happ1, happ2, happ3 = 0,0,0
    if prev != 'a':
        happ1 = a[i] + helper(i+1, 'a', memo)
    if prev != 'b':
        happ2 = b[i] + helper(i+1, 'b', memo)
    if prev != 'c':
        happ3 = c[i] + helper(i+1, 'c', memo)

    ret = max(happ1, happ2, happ3)
    memo[(i,prev)] = ret
    return ret

print(str(helper(0)))