Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
N, M = map(int, input().split())
x, y, z = zip(*(map(int, input().split()) for _ in range(N)))


def f(a, b, c, p, q, r):
    dp = [(0, 0, 0) for _ in range(M + 1)]
    for t in zip(a, b, c):
        for i in range(M, 0, -1):
            u = (dp[i - 1][0] + p * t[0], dp[i - 1][1] + q * t[1], dp[i - 1][2] + r * t[2])
            if sum(u) > sum(dp[i]):
                dp[i] = u
    return sum(map(abs, dp[M]))


ans = max(
    f(x, y, z, p, q, r)
    for p, q, r in product((-1, 1), repeat=3)
)
print(ans)


Fixed submission:
from itertools import product
N, M = map(int, input().split())
x, y, z = zip(*(map(int, input().split()) for _ in range(N)))


def f(a, b, c):
    return sum(sorted(map(sum, zip(a, b, c)), reverse=True)[:M])


def g(k, a):
    return [k * e for e in a]


ans = max(
    f(g(p, x), g(q, y), g(r, z))
    for p, q, r in product((-1, 1), repeat=3)
)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
from itertools import product
N, M = map(int, input().split())
x, y, z = zip(*(map(int, input().split()) for _ in range(N)))


def f(a, b, c):
    dp = [(0, 0, 0) for _ in range(M + 1)]
    for t in zip(a, b, c):
        for i in range(M, 0, -1):
            if sum(dp[i - 1]) + sum(t) > sum(dp[i]):
                dp[i] = (dp[i - 1][0] + t[0], dp[i - 1][1] + t[1], dp[i - 1][2] + t[2])
    return sum(map(abs, dp[M]))


def g(k, a):
    return [k * e for e in a]


ans = max(
    f(g(p, x), g(q, y), g(r, z))
    for p, q, r in product((-1, 1), repeat=3)
)
print(ans)


Fixed submission:
from itertools import product
N, M = map(int, input().split())
x, y, z = zip(*(map(int, input().split()) for _ in range(N)))


def f(a, b, c):
    return sum(sorted(map(sum, zip(a, b, c)), reverse=True)[:M])


def g(k, a):
    return [k * e for e in a]


ans = max(
    f(g(p, x), g(q, y), g(r, z))
    for p, q, r in product((-1, 1), repeat=3)
)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
from itertools import product
N, M = map(int, input().split())
x, y, z = zip(*(map(int, input().split()) for _ in range(N)))


def f(a, b, c):
    return (
        sum(
            map(
                abs,
                reduce(
                    lambda acc, t: [
                        (0, 0, 0) if i == 0 else
                        (acc[i - 1][0] + t[0], acc[i - 1][1] + t[1], acc[i - 1][2] + t[2]) if sum(acc[i - 1]) + sum(t) > sum(acc[i]) else
                        acc[i]
                        for i in range(M + 1)
                    ],
                    zip(a, b, c),
                    [(0, 0, 0) for _ in range(M + 1)]
                )[M]
            )
        )
    )


def g(k, a):
    return [k * e for e in a]


ans = max(
    f(g(p, x), g(q, y), g(r, z))
    for p, q, r in product((-1, 1), repeat=3)
)
print(ans)


Fixed submission:
from itertools import product
N, M = map(int, input().split())
x, y, z = zip(*(map(int, input().split()) for _ in range(N)))


def f(a, b, c):
    return sum(sorted(map(sum, zip(a, b, c)), reverse=True)[:M])


def g(k, a):
    return [k * e for e in a]


ans = max(
    f(g(p, x), g(q, y), g(r, z))
    for p, q, r in product((-1, 1), repeat=3)
)
print(ans)