Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
xyz = [list(map(int,input().split())) for i in range(n)]
ans = 0
for i in range(8):
    s = [0 for i in range(n)]
    for j in range(n):
        a = 0
        for k in range(3):
            if i>>k&1:
                a += xyz[j][k]
            else:
                a -= xyz[j][k]
        s[i] = a
    s.sort()
    ans = max(ans,sum(s[n-m:]))
print(ans)

Fixed submission:
n,m = map(int,input().split())
xyz = [list(map(int,input().split())) for i in range(n)]
ans = 0
for i in range(8):
    s = []
    for j in range(n):
        a = 0
        for k in range(3):
            if i>>k&1:
                a += xyz[j][k]
            else:
                a -= xyz[j][k]
        s.append(a)
    s.sort()
    ans = max(ans,sum(s[n-m:]))
print(ans)

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

dp = [[[[0,0,0] for k in range(8)] for j in range(n+1)] for i in range(n+1)]

for i in range(n):
    for j in range(1,i+2):
        for k in range(8):
            l,r = 0,0
            for d in range(3):
                if k>>d&1:
                    l += dp[i][j-1][k][d] + xyz[i][d]
                    r += dp[i][j][k][d]
                else:
                    l -= dp[i][j-1][k][d] + xyz[i][d]
                    r -= dp[i][j][k][d]
            if l > r:
                for d in range(3):
                    dp[i+1][j][k][d] = dp[i][j-1][k][d] + xyz[i][d]
            else:
                dp[i+1][j][k] = dp[i][j][k]

ans = 0
for k in range(8):
    a = 0
    for d in range(3):
        if k>>d&1:
            a += dp[n][m][k][d]
        else:
            a -= dp[n][m][k][d]
    ans = max(ans,a)
print(ans)



Fixed submission:
n,m = map(int,input().split())
xyz = [list(map(int,input().split())) for i in range(n)]
ans = 0
for i in range(8):
    s = []
    for j in range(n):
        a = 0
        for k in range(3):
            if i>>k&1:
                a += xyz[j][k]
            else:
                a -= xyz[j][k]
        s.append(a)
    s.sort()
    ans = max(ans,sum(s[n-m:]))
print(ans)

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

dp = [[[[0,0,0] for k in range(8)] for j in range(n+1)] for i in range(n+1)]

for i in range(n):
    for j in range(1,i+2):
        for k in range(8):
            l,r = 0,0
            for d in range(3):
                if k>>d&1:
                    l += dp[i][j-1][k][d] + xyz[i][d]
                    r += dp[i][j][k][d]
                else:
                    l -= dp[i][j-1][k][d] + xyz[i][d]
                    r -= dp[i][j][k][d]
            if l > r:
                for d in range(3):
                    dp[i+1][j][k][d] = dp[i][j-1][k][d] + xyz[i][d]
            else:
                dp[i+1][j][k] = dp[i][j][k]

ans = 0
for k in range(8):
    a = 0
    for d in range(3):
        if k>>d&1:
            a += dp[n][m][k][d]
        else:
            a -= dp[n][m][k][d]
    ans = max(ans,a)
print(ans)


Fixed submission:
n,m = map(int,input().split())
xyz = [list(map(int,input().split())) for i in range(n)]
ans = 0
for i in range(8):
    s = []
    for j in range(n):
        a = 0
        for k in range(3):
            if i>>k&1:
                a += xyz[j][k]
            else:
                a -= xyz[j][k]
        s.append(a)
    s.sort()
    ans = max(ans,sum(s[n-m:]))
print(ans)