Buggy python submission (verdict: Time Limit Exceeded):
#おわり、むり

N,W = map(int,input().split())

X,Y,Z = [],[],[]
for i in range(N):
    x,y,z = map(int,input().split())
    X.append(x)
    Y.append(y)
    Z.append(z)  
    
dp = [[[0,0,0] for i in range(W+1)] for j in range(N+1)]
for i in range(W+1):
    dp[0][i][0]= 0 #X
    dp[0][i][1]= 0 #Y
    dp[0][i][2]= 0 #Z
    
ans = 0
for fgo in range(0,8):
    FGO = [fgo%2,(fgo//2)%2,(fgo//4)%2]
    for i in range(N):
        for w in range(W+1):
            if w>=1:
                x = dp[i][w-1][0]+X[i]
                y = dp[i][w-1][1]+Y[i]
                z = dp[i][w-1][2]+Z[i]
                NOW = (x*pow(-1,FGO[0]))+(y*pow(-1,FGO[1]))+(z*pow(-1,FGO[2]))
                BEFORE = sum([dp[i][w][f]*pow(-1,FGO[f]) for f in range(3)])
                if NOW > BEFORE:
                    dp[i+1][w][0] = x
                    dp[i+1][w][1] = y
                    dp[i+1][w][2] = z
                else:
                    dp[i+1][w][0] = dp[i][w][0]
                    dp[i+1][w][1] = dp[i][w][1]
                    dp[i+1][w][2] = dp[i][w][2]
            else:
                dp[i+1][w][0] = dp[i][w][0]
                dp[i+1][w][1] = dp[i][w][1]
                dp[i+1][w][2] = dp[i][w][2]
    MAX_ABS = abs(dp[N][W][0])+abs(dp[N][W][1])+abs(dp[N][W][2])
    if MAX_ABS > ans: ans = MAX_ABS
    #print(MAX_ABS,FGO,dp)

print(ans)

Fixed submission:
N,W = map(int,input().split())

L = []
for i in range(N):
    L.append(list(map(int,input().split())))
    
MAX_ANS = 0
for fgo in range(0,8):
    FGO = [pow(-1,fgo%2),pow(-1,(fgo//2)%2),pow(-1,(fgo//4)%2)]
    L = sorted(L,key=lambda x:x[0]*FGO[0]+x[1]*FGO[1]+x[2]*FGO[2],reverse=True)
    ans = 0
    for m in L[:W]:
        ans += m[0]*FGO[0] + m[1]*FGO[1] + m[2]*FGO[2]
    if ans > MAX_ANS: MAX_ANS = ans

print(MAX_ANS)

Buggy python submission (verdict: Time Limit Exceeded):
#おわり、むり

N,W = map(int,input().split())

X,Y,Z = [],[],[]
for i in range(N):
    x,y,z = map(int,input().split())
    X.append(x)
    Y.append(y)
    Z.append(z)  
    
dp = [[[0,0,0] for i in range(W+1)] for j in range(N+1)]
    
ans = 0
for fgo in range(0,8):
    FGO = [fgo%2,(fgo//2)%2,(fgo//4)%2]
    for i in range(W+1):
        dp[0][i][0]= 0 #X
        dp[0][i][1]= 0 #Y
        dp[0][i][2]= 0 #Z
    for i in range(N):
        for w in range(W+1):
            if w>=1:
                x = dp[i][w-1][0]+X[i]
                y = dp[i][w-1][1]+Y[i]
                z = dp[i][w-1][2]+Z[i]
                NOW = (x*pow(-1,FGO[0]))+(y*pow(-1,FGO[1]))+(z*pow(-1,FGO[2]))
                BEFORE = sum([dp[i][w][f]*pow(-1,FGO[f]) for f in range(3)])
                if NOW > BEFORE:
                    dp[i+1][w][0] = x
                    dp[i+1][w][1] = y
                    dp[i+1][w][2] = z
                else:
                    dp[i+1][w][0] = dp[i][w][0]
                    dp[i+1][w][1] = dp[i][w][1]
                    dp[i+1][w][2] = dp[i][w][2]
            else:
                dp[i+1][w][0] = dp[i][w][0]
                dp[i+1][w][1] = dp[i][w][1]
                dp[i+1][w][2] = dp[i][w][2]
    MAX_ABS = abs(dp[N][W][0])+abs(dp[N][W][1])+abs(dp[N][W][2])
    if MAX_ABS > ans: ans = MAX_ABS
    #print(MAX_ABS,FGO,dp)

print(ans)

Fixed submission:
N,W = map(int,input().split())

L = []
for i in range(N):
    L.append(list(map(int,input().split())))
    
MAX_ANS = 0
for fgo in range(0,8):
    FGO = [pow(-1,fgo%2),pow(-1,(fgo//2)%2),pow(-1,(fgo//4)%2)]
    L = sorted(L,key=lambda x:x[0]*FGO[0]+x[1]*FGO[1]+x[2]*FGO[2],reverse=True)
    ans = 0
    for m in L[:W]:
        ans += m[0]*FGO[0] + m[1]*FGO[1] + m[2]*FGO[2]
    if ans > MAX_ANS: MAX_ANS = ans

print(MAX_ANS)

Buggy python submission (verdict: Time Limit Exceeded):
#おわり、むり

N,W = map(int,input().split())

X,Y,Z = [],[],[]
for i in range(N):
    x,y,z = map(int,input().split())
    X.append(x)
    Y.append(y)
    Z.append(z)  
    
    
ans = 0
for fgo in range(0,8):
    FGO = [fgo%2,(fgo//2)%2,(fgo//4)%2]
    dp = [[[0,0,0] for i in range(W+1)] for j in range(N+1)]
    for i in range(W+1):
        dp[0][i][0]= 0 #X
        dp[0][i][1]= 0 #Y
        dp[0][i][2]= 0 #Z
    for i in range(N):
        for w in range(W+1):
            if w>=1:
                x = dp[i][w-1][0]+X[i]
                y = dp[i][w-1][1]+Y[i]
                z = dp[i][w-1][2]+Z[i]
                NOW = (x*pow(-1,FGO[0]))+(y*pow(-1,FGO[1]))+(z*pow(-1,FGO[2]))
                BEFORE = sum([dp[i][w][f]*pow(-1,FGO[f]) for f in range(3)])
                if NOW > BEFORE:
                    dp[i+1][w][0] = x
                    dp[i+1][w][1] = y
                    dp[i+1][w][2] = z
                else:
                    dp[i+1][w][0] = dp[i][w][0]
                    dp[i+1][w][1] = dp[i][w][1]
                    dp[i+1][w][2] = dp[i][w][2]
            else:
                dp[i+1][w][0] = dp[i][w][0]
                dp[i+1][w][1] = dp[i][w][1]
                dp[i+1][w][2] = dp[i][w][2]
    MAX_ABS = abs(dp[N][W][0])+abs(dp[N][W][1])+abs(dp[N][W][2])
    if MAX_ABS > ans: ans = MAX_ABS
    #print(MAX_ABS,FGO,dp)

print(ans)

Fixed submission:
N,W = map(int,input().split())

L = []
for i in range(N):
    L.append(list(map(int,input().split())))
    
MAX_ANS = 0
for fgo in range(0,8):
    FGO = [pow(-1,fgo%2),pow(-1,(fgo//2)%2),pow(-1,(fgo//4)%2)]
    L = sorted(L,key=lambda x:x[0]*FGO[0]+x[1]*FGO[1]+x[2]*FGO[2],reverse=True)
    ans = 0
    for m in L[:W]:
        ans += m[0]*FGO[0] + m[1]*FGO[1] + m[2]*FGO[2]
    if ans > MAX_ANS: MAX_ANS = ans

print(MAX_ANS)