Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
xs,ys,zs =[],[],[]
comps1 = []
comps2 = []
comps3 = []
comps4 = []
comps5 = []
comps6 = []
comps7 = []
comps8 = []
for i in range(N):
    x,y,z = map(int,input().split())
    raw = [x+y+z,x+y-z,x-y+z,x-y-z,-x+y+z,-x+y-z,-x-y+z,-x-y-z,]
    comps1.append(raw[0])
    comps2.append(raw[1])
    comps3.append(raw[2])
    comps4.append(raw[3])
    comps5.append(raw[4])
    comps6.append(raw[5])
    comps7.append(raw[6])
    comps8.append(raw[7])

ans = 0
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps1[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps2[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps3[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps4[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps5[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps6[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps7[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps8[i-1])
cand = dp[N][M]
ans = max(ans,cand)

print(ans)

Fixed submission:
N,M = map(int,input().split())
comp = [[] for _ in range(8)]
for i in range(N):
    x,y,z = map(int,input().split())
    raw = [x+y+z,x+y-z,x-y+z,x-y-z,-x+y+z,-x+y-z,-x-y+z,-x-y-z]
    for j in range(8):
        comp[j].append(raw[j])
ans = 0
for k in range(8):
    comp[k].sort(reverse=True)
    sum = 0
    for l in range(M):
        sum += comp[k][l]
    ans = max(ans,sum)


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
xs,ys,zs =[],[],[]
comps = []
for i in range(N):
    x,y,z = map(int,input().split())
    raw = [x+y+z,x+y-z,x-y+z,x-y-z,-x+y+z,-x+y-z,-x-y+z,-x-y-z,]
    comps.append(raw)

dp = [[[0]*8 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        for k in range(8):
            dp[i][j][k] = max(dp[i-1][j][k],dp[i-1][j-1][k]+comps[i-1][k] )
print(max(dp[N][M]))

Fixed submission:
N,M = map(int,input().split())
comp = [[] for _ in range(8)]
for i in range(N):
    x,y,z = map(int,input().split())
    raw = [x+y+z,x+y-z,x-y+z,x-y-z,-x+y+z,-x+y-z,-x-y+z,-x-y-z]
    for j in range(8):
        comp[j].append(raw[j])
ans = 0
for k in range(8):
    comp[k].sort(reverse=True)
    sum = 0
    for l in range(M):
        sum += comp[k][l]
    ans = max(ans,sum)


print(ans)