Problem p02604 (python)

Fixed submission:
import bisect
import copy
import heapq
import sys
import itertools
import math
from functools import lru_cache
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    P = read_lists(N)
    # N = 15
    # P = [(i ** 2 + 1, i ** 2 + 1, i ** 2 + 1) for i in range(N)]
    
    C = [1 << 62] * (N + 1)
    # CX = [None for _ in range(2 ** N)]
    # CY = [None for _ in range(2 ** N)]
    CX = [1 << 62] * (N * 2 ** N)
    CY = [1 << 62] * (N * 2 ** N)

    LX = [0] * (2 ** N)
    LY = [0] * (2 ** N)
    for K in range(2 ** N):
        X = {0}
        Y = {0}
        for i in range(N):
            if (K >> i) & 1:
                X.add(P[i][0])
                Y.add(P[i][1])
         
        # CXX = [0] * N
        # CYY = [0] * N
        for j, (xp, yp, cp) in enumerate(P):
            for x in X:
                # CXX[j] = min(abs(xp - x) * cp, CXX[j])
                CX[K * N + j] = min(abs(xp - x) * cp, CX[K * N + j])
            for y in Y:
                CY[K * N + j] = min(abs(yp - y) * cp, CY[K * N + j])
                # CYY[j] = min(abs(yp - y) * cp, CYY[j])
            
        LX[K] = len(X)
        LY[K] = len(Y)
        # CX[K] = tuple(CXX)
        # CY[K] = tuple(CYY)

    for T in range(2 ** N):
        p = format(T, "160b").count("1")
        if C[p] == 0:
            continue

        r = 1 << 62
        tx = T
        while True:
            ty = T - tx
            c = 0
            for n in range(N):
                c += min(CX[tx * N + n], CY[ty * N + n])
            r = min(r, c)
            if tx == 0:
                break
            tx = (tx - 1) & T
        C[p] = min(C[p], r)
    print("\n".join(map(str, C)))


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    N = int(input())
    P = read_lists(N)

    C = [V(min) for _ in range(N + 1)]
    for T in itertools.product(range(3), repeat=N):
        X = {0}
        Y = {0}
        S = set()
        for i, t in enumerate(T):
            if t == 1:
                X.add(P[i][0])
                S.add(i)
            elif t == 2:
                Y.add(P[i][1])
                S.add(i)

        c = 0
        for j, (xp, yp, p) in enumerate(P):
            if j in S:
                continue
            cx = min(abs(xp - x) for x in X)
            cy = min(abs(yp - y) for y in Y)
            c += min(cx, cy) * p
        C[len(X) + len(Y) - 2].ud(c)

    print("\n".join(map(str, C)))


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import sys
import itertools
import math
from functools import lru_cache
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class Comb:
    def __init__(self, N):
        F = [1] * (N + 1)
        for i in range(N):
            F[i + 1] = (i + 1) * F[i] % mod

        self.d_inv = {}
        self.d_comb = {}
        self.F = F

    def inv(self, a):
        if a not in self.d_inv:
            self.d_inv[a] = pow(a, mod - 2, mod)
        return self.d_inv[a]

    def comb(self, a, b):
        if (a, b) not in self.d_comb:
            self.d_comb[(a, b)] = (self.F[a] * self.inv(self.F[a - b]) * self.inv(self.F[b])) % mod
            self.d_comb[(a, a - b)] = self.d_comb[(a, b)]
        return self.d_comb[(a, b)] 


def main():
    N = int(input())
    P = read_lists(N)

    CX = [[0 for _ in range(N)] for _ in range(N ** 2)]
    LX = {i: set() for i in range(1, N + 1)}
    C = [0] * (N + 1)

    for T in itertools.product(range(3), repeat=N):
        X = set()
        Y = set()
        for i, t in enumerate(T):
            if t == 1:
                X.add(P[i][0])
            elif t == 2:
                Y.add(P[i][1])

        
        for j, (xp, yp, p) in enumerate(P):
            cx = min(abs(xp - x) for x in X)
            cy = min(abs(yp - y) for y in Y)
            C[len(X) + len(Y)] = min(cx, cy) * p

    
    print(map(str, C))


if __name__ == "__main__":
    main()