Buggy python submission (verdict: Runtime Error):
N,M = map(int, input().split())
X = [ [int(j) for j in input().split()] for _ in range(N)]
ans = 0
for i in range(1<<3):
    if M=0:
        break
    dp = [ [0] * (M+1) for _ in range(N+1)]
 
    for n in range(0,N):
        x,y,z = X[n]
        s = (x if (i & (1<<2)) else -x) \
            + (y if (i & (1<<1)) else -y) \
            + (z if (i & (1<<0)) else -z) \
 
        for m in range(min(n,M-1), -1, -1 ):
            dp[n+1][m+1] =  max(dp[n+1][m+1], s + dp[n][m] )
            dp[n+1][m]   =  max(dp[n+1][m], dp[n][m])
 
        dp[n+1][M] = max(dp[n+1][M], dp[n][M])
    ans = max(ans, dp[N][M])
print(ans)

Fixed submission:
N,M = map(int, input().split())
X = [ [int(j) for j in input().split()] for _ in range(N)]
ans = 0
for i in range(1<<3):
    if M==0:
        break
    ss = [None]*N

    for n in range(0,N):
        x,y,z = X[n]
        s = (x if (i & (1<<2)) else -x) \
            + (y if (i & (1<<1)) else -y) \
            + (z if (i & (1<<0)) else -z)
        ss[n] = s
    ss.sort()
    ans = max(ans, sum(ss[N-M:]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())
X = [ [int(j) for j in input().split()] for _ in range(N)]
ans = 0
for i in range(1<<3):
    dp = [ [0] * (M+1) for _ in range(N+1)]
 
    for n in range(0,N):
        x,y,z = X[n]
        s = (x if (i & (1<<2)) else -x) \
            + (y if (i & (1<<1)) else -y) \
            + (z if (i & (1<<0)) else -z) \
 
        # for m in range(0, min(n+1,M) ):
        for m in range(min(n,M-1), -1, -1 ):
            dp[n+1][m+1] =  max(dp[n+1][m+1], s + dp[n][m] )
            dp[n+1][m]   =  max(dp[n+1][m], dp[n][m])
 
        dp[n+1][M] = max(dp[n+1][M], dp[n][M])
    ans = max(ans, dp[N][M])
print(ans)

Fixed submission:
N,M = map(int, input().split())
X = [ [int(j) for j in input().split()] for _ in range(N)]
ans = 0
for i in range(1<<3):
    if M==0:
        break
    ss = [None]*N

    for n in range(0,N):
        x,y,z = X[n]
        s = (x if (i & (1<<2)) else -x) \
            + (y if (i & (1<<1)) else -y) \
            + (z if (i & (1<<0)) else -z)
        ss[n] = s
    ss.sort()
    ans = max(ans, sum(ss[N-M:]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())
X = [ [int(j) for j in input().split()] for _ in range(N)]
ans = 0
sgn = [[-1,-1,-1],
        [-1,-1, 1],
        [-1, 1,-1],
        [-1, 1, 1],
        [ 1,-1,-1],
        [ 1,-1, 1],
        [ 1, 1,-1],
        [ 1, 1, 1]]
for s in sgn:
    dp = [ [0] * (M+1) for _ in range(N+1)]
 
    for n in range(0,N):
        x,y,z = X[n]
 
        # for m in range(0, min(n+1,M) ):
        for m in range(min(n,M-1), -1, -1 ):
            dp[n+1][m+1] =  max(dp[n+1][m+1], \
                (x if (s[0]>0) else -x) \
                + (y if (s[1]>0) else -y) \
                + (z if (s[2]>0) else -z) \
                + dp[n][m] )
            dp[n+1][m]   =  max(dp[n+1][m], dp[n][m])
 
        dp[n+1][M] = max(dp[n+1][M], dp[n][M])
    ans = max(ans, dp[N][M])
print(ans)

Fixed submission:
N,M = map(int, input().split())
X = [ [int(j) for j in input().split()] for _ in range(N)]
ans = 0
for i in range(1<<3):
    if M==0:
        break
    ss = [None]*N

    for n in range(0,N):
        x,y,z = X[n]
        s = (x if (i & (1<<2)) else -x) \
            + (y if (i & (1<<1)) else -y) \
            + (z if (i & (1<<0)) else -z)
        ss[n] = s
    ss.sort()
    ans = max(ans, sum(ss[N-M:]))
print(ans)