Problem p03326 (python)

Fixed submission:
a = []
b = []
ans = 0
def flip(f):
    for i in range(len(a)):
        for j in range(3):
            if f & (1 << j) != 0:
                a[i][j] = - a[i][j]
n,m = map(int,input().split())
for i in range(n):
    a.append(list(map(int,input().split())))
for f in range(8):
    del(b[:])
    flip(f)
    for x in a:
        b.append(sum(x))
    b.sort(reverse = True)
    ans = max([ans,sum(b[:m])])
    flip(f)
print(ans)

Buggy submission (verdict: Runtime Error):
a = []
b = []
mem = {}
ans = 0
def flip(f):
    for i in range(len(a)):
        for j in range(3):
            if f & (1 << j) == 1:
                a[i][j] = - a[i][j]
def solve(n,m):
    if(mem.get((n,m))!=None):
        return mem[(n,m)]
    if m == 0:
        return 0
    if n == m:
        mem[(n,m)] = sum(b[:n])
        return mem[(n,m)]
    ans = max([solve(n-1,m-1) + b[n-1] , solve(n-1,m)])
    mem[(n,m)] = ans
    return ans
n,m = map(int,input().split())
for i in range(n):
    a.append(list(map(int,input().split())))
for f in range(8):
    b.clear()
    flip(f)
    for x in a:
        b.append(sum(x))
    mem.clear()
    ans = max([ans,solve(n,m)])
    flip(f)
print(ans)

Buggy submission (verdict: Runtime Error):
a = []
b = []
mem = {}
ans = 0
def flip(a,f):
    for i in range(len(a)):
        for j in range(3):
            if f & (1 << j) == 1:
                a[i][j] = - a[i][j]
def solve(n,m):
    if(mem.get((n,m))!=None):
        return mem[(n,m)]
    if m == 0:
        return 0
    if n == m:
        mem[(n,m)] = sum(b[:n])
        return mem[(n,m)]
    ans = max([solve(n-1,m-1) + b[n-1] , solve(n-1,m)])
    mem[(n,m)] = ans
    return ans
n,m = map(int,input().split())
for i in range(n):
    a.append(list(map(int,input().split())))
for f in range(8):
    b.clear()
    flip(a,f)
    for x in a:
        b.append(sum(x))
    mem.clear()
    ans = max([ans,solve(n,m)])
    flip(a,f)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
a = []
b = []
mem = {}
ans = 0
def flip(a,f):
    for i in range(len(a)):
        for j in range(3):
            if f & (1 << j):
                a[i][j] = - a[i][j]
def solve(n,m):
    if(mem.get((n,m))!=None):
        return mem[(n,m)]
    if m == 0:
        return 0
    if n == m:
        mem[(n,m)] = sum(b[:n])
        return mem[(n,m)]
    ans = max([solve(n-1,m-1) + b[n-1] , solve(n-1,m)])
    mem[(n,m)] = ans
    return ans
for i in range(n):
    a.append(list(map(int,input().split())))
for f in range(8):
    b.clear()
    flip(a,f)
    for x in a:
        b.append(sum(x))
    mem.clear()
    ans = max([ans,solve(n,m)])
    flip(a,f)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
a = []
b = []
mem = {}
ans = 0
def flip(a,f):
    for i in range(len(a)):
        for j in range(3):
            if f & (1 << j):
                a[i][j] = - a[i][j]
def solve(n,m):
    if(mem.get((n,m))!=None):
        return mem[(n,m)]
    if m == 0:
        return 0
    if n == m:
        mem[(n,m)] = sum(b[:n])
        return mem[(n,m)]
    ans = max([solve(n-1,m-1) + b[n-1] , solve(n-1,m)])
    mem[(n,m)] = ans
    return ans
for i in range(n):
    a.append(list(map(int,input().split())))
for f in range(8):
    b.clear()
    flip(a,f)
    for x in a:
        b.append(sum(x))
    mem.clear()
    ans = max([ans,solve(n,m)])
    flip(a,f)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
a = []
b = []
mem = {}
ans = 0
def flip(a,f):
    for i in range(len(a)):
        for j in range(3):
            if f & (1 << j):
                a[i][j] = - a[i][j]
def solve(n,m):
    if(mem.get((n,m))!=None):
        return mem[(n,m)]
    if m == 0:
        return 0
    if n == m:
        mem[(n,m)] = sum(b[:n])
        return mem[(n,m)]
    ans = max([solve(n-1,m-1) + b[n-1] , solve(n-1,m)])
    mem[(n,m)] = ans
    return ans
for i in range(n):
    a.append(list(map(int,input().split())))
for f in range(8):
    b.clear()
    flip(a,f)
    for x in a:
        b.append(sum(x))
    mem.clear()
    ans = max([ans,solve(n,m)])
    flip(a,f)
print(ans)