Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

N,M = map(int,input().split()) # <=1000
xyz = [list(map(int,input().split())) for _ in range(N)]
x = [i[0] for i in xyz]
y = [i[1] for i in xyz]
z = [i[2] for i in xyz]
if  M == 0:
    print(0)
    exit()
result = 0
for s in combinations(range(N),M):
    tx = ty = tz = 0
    for i in s:
        tx += x[i]
        ty += y[i]
        tz += z[i]
    result = max(result,abs(tx)+abs(ty)+abs(tz))
print(result)


Fixed submission:
import itertools

N,M = map(int,input().split()) # <=1000
xyz = [list(map(int,input().split())) for _ in range(N)]
if  M == 0:
    print(0)
    exit()
result = 0
L = [[0] * N for i in range(8)]
for i,j in enumerate(itertools.product((-1,1),repeat=3)):
    for k,l in enumerate(xyz):
        L[i][k] = l[0] * j[0] + l[1] * j[1] + l[2] * j[2]
    L[i] = sorted(L[i],reverse=True)
print(max([sum(i[:M]) for i in L]))

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split()) # <=1000
xyz = [list(map(int,input().split())) for _ in range(N)]

dp = [[{'S':0,'L':[0,0,0]} for __ in range(N)]for _ in range(M+1)]
dp[1][0]['L'] = xyz[0]
dp[1][0]['S'] = abs(xyz[0][0]) + abs(xyz[0][1]) + abs(xyz[0][2])
for i in range(1,N):
    for j in range(1,min(i+2,M+1)):
        t = dp[j-1][i-1]['L']
        t = [t[0] + xyz[i][0],t[1] + xyz[i][1],t[2] + xyz[i][2]]
        ts = abs(t[0])+abs(t[1])+abs(t[2])
        if dp[j][i-1]['S'] < ts:
            dp[j][i]['S'] = ts
            dp[j][i]['L'] = t
        else:
            dp[j][i]['S'] = dp[j][i-1]['S']
            dp[j][i]['L'] = dp[j][i-1]['L']

print(dp[M][N-1]['S'])

Fixed submission:
import itertools

N,M = map(int,input().split()) # <=1000
xyz = [list(map(int,input().split())) for _ in range(N)]
if  M == 0:
    print(0)
    exit()
result = 0
L = [[0] * N for i in range(8)]
for i,j in enumerate(itertools.product((-1,1),repeat=3)):
    for k,l in enumerate(xyz):
        L[i][k] = l[0] * j[0] + l[1] * j[1] + l[2] * j[2]
    L[i] = sorted(L[i],reverse=True)
print(max([sum(i[:M]) for i in L]))

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split()) # <=1000
xyz = [list(map(int,input().split())) for _ in range(N)]

dp = [[{'S':0,'L':[0,0,0]} for __ in range(N)]for _ in range(M+1)]
dp[1][0]['L'] = xyz[0]
dp[1][0]['S'] = abs(xyz[0][0]) + abs(xyz[0][1]) + abs(xyz[0][2])
for i in range(1,N):
    for j in range(1,min(i+2,M+1)):
        t = dp[j-1][i-1]['L']
        t = [t[0] + xyz[i][0],t[1] + xyz[i][1],t[2] + xyz[i][2]]
        ts = abs(t[0])+abs(t[1])+abs(t[2])
        if dp[j][i-1]['S'] < ts:
            dp[j][i]['S'] = ts
            dp[j][i]['L'] = t
        else:
            dp[j][i]['S'] = dp[j][i-1]['S']
            dp[j][i]['L'] = dp[j][i-1]['L']

print(dp[M][N-1]['S'])

Fixed submission:
import itertools

N,M = map(int,input().split()) # <=1000
xyz = [list(map(int,input().split())) for _ in range(N)]
if  M == 0:
    print(0)
    exit()
result = 0
L = [[0] * N for i in range(8)]
for i,j in enumerate(itertools.product((-1,1),repeat=3)):
    for k,l in enumerate(xyz):
        L[i][k] = l[0] * j[0] + l[1] * j[1] + l[2] * j[2]
    L[i] = sorted(L[i],reverse=True)
print(max([sum(i[:M]) for i in L]))