Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
xyz = [tuple(map(int, input().split())) for _ in range(n)]
def isum(i, x, y, z):
    s = 0
    s += x if (i>>0)&1 else -x
    s += y if (i>>1)&1 else -y
    s += z if (i>>2)&1 else -z
    return s
ans = 0
for i in range(2**3):
    dp = [0 for _ in range(m+1)]
    for k in range(n):
        x, y, z = xyz[k]
        ndp = [0 for _ in range(m+1)]
        for j in range(1, k+1):
            # iを含める場合は、一つ前のj-1から
            # iを含めない場合は、一つ前のjから
            sumj_1 = dp[j-1]
            sj_1 = sumj_1 + isum(i, x, y, z)
            noinc_sum = dp[j]
            ndp[j] = max(sj_1, noinc_sum)

        dp = ndp
    ans = max(ans, dp[-1])
print(ans)

Fixed submission:
n, m = map(int, input().split())
xyz = [tuple(map(int, input().split())) for _ in range(n)]
ans = 0
from itertools import product
for p in product([-1, 1], repeat=3):
    xyz_ = map(lambda l: l[0]*p[0] + l[1]*p[1] + l[2]*p[2], xyz)
    xyz_ = sorted(xyz_, reverse=True)
    ans = max(ans, sum(xyz_[:m]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
xyz = [tuple(map(int, input().split())) for _ in range(n)]
def isum(i, x, y, z):
    s = 0
    s += x if (i>>0)&1 else -x
    s += y if (i>>1)&1 else -y
    s += z if (i>>2)&1 else -z
    return s
ans = 0
for i in range(2**3):
    dp = [0 for _ in range(m+1)]
    for k in range(n):
        x, y, z = xyz[k]
        ndp = [0 for _ in range(m+1)]
        for j in range(1, m+1):
            # iを含める場合は、一つ前のj-1から
            # iを含めない場合は、一つ前のjから
            sumj_1 = dp[j-1]
            sj_1 = sumj_1 + isum(i, x, y, z)
            noinc_sum = dp[j]
            ndp[j] = max(sj_1, noinc_sum)

        dp = ndp
    ans = max(ans, dp[-1])
print(ans)


Fixed submission:
n, m = map(int, input().split())
xyz = [tuple(map(int, input().split())) for _ in range(n)]
ans = 0
from itertools import product
for p in product([-1, 1], repeat=3):
    xyz_ = map(lambda l: l[0]*p[0] + l[1]*p[1] + l[2]*p[2], xyz)
    xyz_ = sorted(xyz_, reverse=True)
    ans = max(ans, sum(xyz_[:m]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
xyz = [tuple(map(int, input().split())) for _ in range(n)]
def isum(i, x, y, z):
    s = 0
    s += x if (i>>0)&1 else -x
    s += y if (i>>1)&1 else -y
    s += z if (i>>2)&1 else -z
    return s

ans = 0
for i in range(2**3):
    dp = [[0, 0, 0] for _ in range(m+1)]
    for k in range(n):
        x, y, z = xyz[k]
        ndp = [[0, 0, 0] for _ in range(m+1)]
        for j in range(1, m+1):
            # iを含める場合は、一つ前のj-1から
            # iを含めない場合は、一つ前のjから
            xj, yj, zj = dp[j-1]
            inc = [xj+x, yj+y, zj+z]
            sj_1 = isum(i, *inc)

            noinc = dp[j]
            noinc_sum = isum(i, *noinc)
            if sj_1 > noinc_sum:
                ndp[j] = inc
            else:
                ndp[j] = noinc
        dp = ndp
    ans = max(ans, isum(i, *dp[-1]))
print(ans)

Fixed submission:
n, m = map(int, input().split())
xyz = [tuple(map(int, input().split())) for _ in range(n)]
ans = 0
from itertools import product
for p in product([-1, 1], repeat=3):
    xyz_ = map(lambda l: l[0]*p[0] + l[1]*p[1] + l[2]*p[2], xyz)
    xyz_ = sorted(xyz_, reverse=True)
    ans = max(ans, sum(xyz_[:m]))
print(ans)