Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
a=[]
for i in range(n):
    a.append(tuple(map(int, input().split())))

dp = []
for i in range(m+1):
    dp.append([0 for j in range(n+1)])

def f(fn):
    global dp, a
    for i in range(1,m+1):
        for j in range(1,n+1):
            dp[i][j] = max(dp[i][j-1], dp[i-1][j-1] + fn(a[j-1]))

ans = []
f(lambda x: x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]-x[2])
ans.append(dp[m][n])

print(max(ans))


Fixed submission:
n,m = map(int, input().split())
a=[]
for i in range(n):
    a.append(tuple(map(int, input().split())))

dp = []
for i in range(m+1):
    dp.append([0 for j in range(n+1)])

def f(fn):
    global dp, a
    for i in range(1,m+1):
        for j in range(i,n+1):
            if j == i:
                dp[i][j] = sum(map(fn, a[:j]))
            else:
                dp[i][j] = max(dp[i][j-1], dp[i-1][j-1] + fn(a[j-1]))

ans = []
f(lambda x: x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]-x[2])
ans.append(dp[m][n])

print(max(ans))

Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
a=[]
for i in range(n):
    a.append(tuple(map(int, input().split())))

memo = {}
def f(a, s, fn):
    global m
    l = len(a)
    if s >= m or l == 0:
        return 0
    if (l,s) in memo:
        return memo[(l,s)]
    elif len(a) == 1:
        ans = max(0, fn(a[0]))
    else:
        ans = max(f(a[1:], s, fn), fn(a[0]) + f(a[1:], s+1, fn))
    memo[(l,s)] = ans
    return ans

ans = []
ans.append(f(a,0, lambda x: x[0]+x[1]+x[2]))
memo.clear()
ans.append(f(a,0, lambda x: x[0]+x[1]-x[2]))
memo.clear()
ans.append(f(a,0, lambda x: x[0]-x[1]+x[2]))
memo.clear()
ans.append(f(a,0, lambda x: x[0]-x[1]-x[2]))
memo.clear()
ans.append(f(a,0, lambda x: -x[0]+x[1]+x[2]))
memo.clear()
ans.append(f(a,0, lambda x: -x[0]+x[1]-x[2]))
memo.clear()
ans.append(f(a,0, lambda x: -x[0]-x[1]+x[2]))
memo.clear()
ans.append(f(a,0, lambda x: -x[0]-x[1]-x[2]))
print(max(ans))


Fixed submission:
n,m = map(int, input().split())
a=[]
for i in range(n):
    a.append(tuple(map(int, input().split())))

dp = []
for i in range(m+1):
    dp.append([0 for j in range(n+1)])

def f(fn):
    global dp, a
    for i in range(1,m+1):
        for j in range(i,n+1):
            if j == i:
                dp[i][j] = sum(map(fn, a[:j]))
            else:
                dp[i][j] = max(dp[i][j-1], dp[i-1][j-1] + fn(a[j-1]))

ans = []
f(lambda x: x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]-x[2])
ans.append(dp[m][n])

print(max(ans))

Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
a=[]
for i in range(n):
    a.append(tuple(map(int, input().split())))

memo = {}
def f(a, s, fn):
    global m
    l = len(a)
    if s >= m or l == 0:
        return 0
    if (l,s) in memo:
        return memo[(l,s)]
    elif len(a) == 1:
        return max(0, fn(a[0]))
    else:
        return max(f(a[1:], s, fn), fn(a[0]) + f(a[1:], s+1, fn))

ans = []
ans.append(f(a,0, lambda x: x[0]+x[1]+x[2]))
memo.clear()
ans.append(f(a,0, lambda x: x[0]+x[1]-x[2]))
memo.clear()
ans.append(f(a,0, lambda x: x[0]-x[1]+x[2]))
memo.clear()
ans.append(f(a,0, lambda x: x[0]-x[1]-x[2]))
memo.clear()
ans.append(f(a,0, lambda x: -x[0]+x[1]+x[2]))
memo.clear()
ans.append(f(a,0, lambda x: -x[0]+x[1]-x[2]))
memo.clear()
ans.append(f(a,0, lambda x: -x[0]-x[1]+x[2]))
memo.clear()
ans.append(f(a,0, lambda x: -x[0]-x[1]-x[2]))
memo.clear()
print(max(ans))


Fixed submission:
n,m = map(int, input().split())
a=[]
for i in range(n):
    a.append(tuple(map(int, input().split())))

dp = []
for i in range(m+1):
    dp.append([0 for j in range(n+1)])

def f(fn):
    global dp, a
    for i in range(1,m+1):
        for j in range(i,n+1):
            if j == i:
                dp[i][j] = sum(map(fn, a[:j]))
            else:
                dp[i][j] = max(dp[i][j-1], dp[i-1][j-1] + fn(a[j-1]))

ans = []
f(lambda x: x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]-x[2])
ans.append(dp[m][n])

print(max(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n,m = map(int, input().split())
a=[]
for i in range(n):
    a.append(tuple(map(int, input().split())))

max = 0
for i in range(1,m+1):
    for j in itertools.combinations(a,i):
        x = sum(map(abs, map(sum, zip(*j))))
        if max < x:
            max = x

print(max)


Fixed submission:
n,m = map(int, input().split())
a=[]
for i in range(n):
    a.append(tuple(map(int, input().split())))

dp = []
for i in range(m+1):
    dp.append([0 for j in range(n+1)])

def f(fn):
    global dp, a
    for i in range(1,m+1):
        for j in range(i,n+1):
            if j == i:
                dp[i][j] = sum(map(fn, a[:j]))
            else:
                dp[i][j] = max(dp[i][j-1], dp[i-1][j-1] + fn(a[j-1]))

ans = []
f(lambda x: x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: x[0]-x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]+x[1]-x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]+x[2])
ans.append(dp[m][n])
f(lambda x: -x[0]-x[1]-x[2])
ans.append(dp[m][n])

print(max(ans))