Problem p03576 (python)

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

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, K, XY):
    X = sorted(map(lambda x: x[0], XY))
    Y = sorted(map(lambda x: x[1], XY))

    ans = sys.maxsize
    for lx in X:
        for rx in X[::-1]:
            if lx > rx:
                break
            for by in Y:
                for ty in Y[::-1]:
                    if by > ty:
                        break

                    # print(lx, rx, by, ty)
                    k = 0
                    for x, y in XY:
                        if lx <= x <= rx and by <= y <= ty:
                            k += 1
                    # print(k)
                    if k >= K:
                        ans = min(ans, (rx-lx)*(ty-by))
                    else:
                        break

    return ans


def main():
    N, K = read_int_n()
    XY = [read_int_n() for _ in range(N)]

    # N = random.randint(2, 50)
    # N = 50
    # K = random.randint(2, N)
    # cm = int(1e+9)
    # XY = [(random.randint(-cm, cm), random.randint(-cm, cm)) for _ in range(N)]
    # print(N, K)
    print(slv(N, K, XY))


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, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul

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, A):
    EC = []
    for i, r in enumerate(A):
        for j, c in enumerate(r):
            if c == 0:
                break
            EC.append((c, i, j))

    EC.sort()
    ans = 0
    for c, i, j in EC:
        q = [(0, i)]
        min_path = c
        f = True
        while q:
            c2, k = q.pop()
            if k != i and c2 + A[k][j] <= min_path:
                min_path = c2 + A[k][j]
                f = False
            else:
                for l, c3 in enumerate(A[k]):
                    if l != i and c3 != 0 and c2 + c3 < c:
                        q.append((c2 + c3, l))

        if min_path != c:
            return -1
        elif f:
            ans += c
    return ans


def main():
    N = read_int()
    A = [read_int_n() for _ in range(N)]

    print(slv(N, A))


if __name__ == '__main__':
    main()