Problem p03326 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, XYZ):
    ans = 0
    for i, j, k in product([-1, 1], [-1, 1], [-1, 1]):
        ans = max(ans,
                  sum(
                      sorted(
                          [i * x + j * y + k * z for x, y, z in XYZ],
                          reverse=True)[:M]))
    return ans


def main():
    N, M = read_int_n()
    XYZ = [read_int_n() for _ in range(N)]

    print(slv(N, M, XYZ))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, XYZ):

    # cakes = []

    # for x, y, z in XYZ:
    #     cakes.append((x, y, z, abs(x) + abs(y) + abs(z)))
    # cakes.sort(reverse=True, key=lambda x: x[3])
    # XYZ = cakes

    def v(xyz):
        return abs(xyz[0]) + abs(xyz[1]) + abs(xyz[2])

    take = list(range(M))
    V = 0
    f = True
    while f:
        f = False
        X = Y = Z = 0
        for i in take:
            x, y, z = XYZ[i]
            X += x
            Y += y
            Z += z

        V = v((X, Y, Z))

        v_min_j = 0
        v_min = sys.maxsize
        x_min_j = 0
        x_min = sys.maxsize
        y_min_j = 0
        y_min = sys.maxsize
        z_min_j = 0
        z_min = sys.maxsize
        for j in take:
            x, y, z = XYZ[j]
            C = (X - XYZ[j][0], Y - XYZ[j][1], Z - XYZ[j][2])
            vt = v(C)
            if vt < v_min:
                v_min = vt
                v_min_j = j
            if abs(x) < x_min:
                x_min = abs(x)
                x_min_j = j
            if abs(y) < y_min:
                y_min = abs(y)
                y_min_j = j
            if abs(z) < z_min:
                z_min = abs(z)
                z_min_j = j

        IJ = None
        for i in range(N):
            if i in take:
                continue
            for j in (v_min_j, x_min_j, y_min_j, z_min_j):
                C = (X - XYZ[j][0] + XYZ[i][0], Y - XYZ[j][1] + XYZ[i][1],
                     Z - XYZ[j][2] + XYZ[i][2])
                vt = v(C)
                if V < vt:
                    V = vt
                    IJ = (i, j)
            #         break
            # if IJ:
            #     break
        if IJ:
            take.remove(IJ[1])
            take.append(IJ[0])
            f = True
            # error_print(V)

    return V


def main():
    N, M = read_int_n()
    XYZ = [read_int_n() for _ in range(N)]

    # random.seed(0)
    # N = 1000
    # M = N // 2
    # XYZ = [[
    #     random.randint(-10000000000, 10000000000),
    #     random.randint(-10000000000, 10000000000),
    #     random.randint(-10000000000, 10000000000)
    # ] for _ in range(N)]
    print(slv(N, M, XYZ))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, XYZ):

    # cakes = []

    # for x, y, z in XYZ:
    #     cakes.append((x, y, z, abs(x) + abs(y) + abs(z)))
    # cakes.sort(reverse=True, key=lambda x: x[3])
    # XYZ = cakes

    def v(xyz):
        return abs(xyz[0]) + abs(xyz[1]) + abs(xyz[2])

    take = list(range(M))
    V = 0
    f = True
    while f:
        f = False
        X = Y = Z = 0
        for i in take:
            x, y, z = XYZ[i]
            X += x
            Y += y
            Z += z

        V = v((X, Y, Z))

        v_min_j = -1
        v_min = sys.maxsize
        x_min_j = -1
        x_min = sys.maxsize
        y_min_j = -1
        y_min = sys.maxsize
        z_min_j = -1
        z_min = sys.maxsize
        for j in take:
            x, y, z = XYZ[j]
            C = (X - XYZ[j][0], Y - XYZ[j][1], Z - XYZ[j][2])
            vt = v(C)
            if vt < v_min:
                v_min = vt
                v_min_j = j
            if abs(x) < x_min:
                x_min = abs(x)
                x_min_j = j
            if abs(y) < y_min:
                y_min = abs(y)
                y_min_j = j
            if abs(z) < z_min:
                z_min = abs(z)
                z_min_j = j

        IJ = None
        for i in range(N):
            if i in take:
                continue
            for j in (v_min_j, x_min_j, y_min_j, z_min_j):
                C = (X - XYZ[j][0] + XYZ[i][0], Y - XYZ[j][1] + XYZ[i][1],
                     Z - XYZ[j][2] + XYZ[i][2])
                vt = v(C)
                if V < vt:
                    V = vt
                    IJ = (i, j)
            #         break
            # if IJ:
            #     break
        if IJ:
            take.remove(IJ[1])
            take.append(IJ[0])
            f = True
            # error_print(V)

    return V


def main():
    N, M = read_int_n()
    XYZ = [read_int_n() for _ in range(N)]

    # random.seed(0)
    # N = 1000
    # M = N // 2
    # XYZ = [[
    #     random.randint(-10000000000, 10000000000),
    #     random.randint(-10000000000, 10000000000),
    #     random.randint(-10000000000, 10000000000)
    # ] for _ in range(N)]
    print(slv(N, M, XYZ))


if __name__ == '__main__':
    main()