Problem p02604 (python)

Fixed submission:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def generate_dist_arr(X, Y, P):
    N = len(X)
    dist_arr_x = [[] for _ in range(1<<N)]
    dist_arr_y = [[] for _ in range(1<<N)]
    for j in range(1 << N):  # x-y-lines
        x_lines_list = [0]
        y_lines_list = [0]
        for k in range(N):
            if j >> k & 1:
                x_lines_list.append(X[k])
                y_lines_list.append(Y[k])

        dist_xx, dist_yy = [INF] * N, [INF] * N
        x_lines_list = sorted(x_lines_list)
        y_lines_list = sorted(y_lines_list)
        for idx, x in enumerate(X):
            v = bisect.bisect_left(x_lines_list, x)
            pre_x = x_lines_list[min(v-1,  0)]
            post_x = x_lines_list[min(v, len(x_lines_list)-1)]
            dist_xx[idx] = min(abs(pre_x - x), abs(post_x - x)) * P[idx]
        dist_arr_x[j] = dist_xx

        for idx, y in enumerate(Y):
            v = bisect.bisect_left(y_lines_list, y)
            pre_y = y_lines_list[max(v-1, 0)]
            post_y = y_lines_list[min(v, len(y_lines_list) - 1)]
            dist_yy[idx] = min(abs(pre_y - y), abs(post_y - y)) * P[idx]
        dist_arr_y[j] = dist_yy
    return dist_arr_x, dist_arr_y

def calc(val_list):
    val_list = sorted(val_list)
    n = len(val_list)
    dp = [[INF] * (n+1) for _ in range(n+1)]
    dp[0][0] = 0

    for l in range(n+1):
        for r in range(l+1, n+1):
            next_dist = INF
            for k in range(l+1, r+1):
                sum = 0
                for v in range(l+1, r+1):
                    sum += abs(val_list[v-1][0] - val_list[k-1][0]) * val_list[v-1][1]
                next_dist = min(next_dist, sum)
            for j in range(n):
                dp[r][j + 1] = min(dp[r][j + 1], dp[l][j] + next_dist)
            # 0-line
            sum = 0
            for v in range(l + 1, r + 1):
                sum += abs(val_list[v-1][0]) * val_list[v-1][1]
            next_dist = sum

            for j in range(n+1):
                dp[r][j] = min(dp[r][j], dp[l][j]+next_dist)
    return dp


def run():
    N = int(sysread())
    X, Y = [], []

    for i in range(1, N+1):
        x,y,p = map(int, sysread().split())
        X.append((x, p))
        Y.append((y, p))

    ans = [INF] * (N + 1)


    for i in range(1 << N):# x-y-directions
        x_town_list = []
        y_town_list = []
        for k in range(N):
            if i >> k & 1:
                x_town_list.append(X[k])
            else:
                y_town_list.append(Y[k])
        #print('x', x_town_list)
        #print('y', y_town_list)
        dp_x = calc(x_town_list)[-1]
        dp_y = calc(y_town_list)[-1]
        #print(calc(x_town_list))
        #print(calc(y_town_list), '\n')
        #print(bin(i))
        #print(dist_towns_x)
        #print(dist_towns_y)

        for i in range(len(dp_x)):
            for j in range(len(dp_y)):
                val = dp_x[i] + dp_y[j]
                ans[i + j] = min(ans[i + j], val)


    for a in ans:
        print(a)


if __name__ == "__main__":
    run()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
import numpy as np
#from copy import deepcopy
#from collections import deque


def run():
    N = int(sysread())
    INF = 1 << 50
    X, Y, P = [], [], []
    ans = [INF] * N

    INF = 1 << 50

    dist_arr = np.zeros([2 * (N+1), N+1], dtype= np.int64)
    for i in range(1, N+1):
        x,y,p = map(int, sysread().split())
        X.append(x)
        Y.append(y)
        P.append(p)
        dist_arr[0, i] = abs(x) * p
        dist_arr[N+1, i] = abs(y) * p

    ans = [INF] * (N + 1)

    for i in range(1 << N):# x-y-directions
        dist_towns_x = [INF] * (N + 1)
        dist_towns_y = [INF] * (N + 1)
        x_town_list = []
        y_town_list = []
        for k in range(N):
            if i >> k & 1:
                x_town_list.append(k)
            else:
                y_town_list.append(k)


        for j in range(1 << N):  # x-lines
            x_lines_list = [0]

            for k in range(N):
                if j >> k & 1:
                    x_lines_list.append(Y[k])

            dist_ = [INF] * len(x_town_list)
            for line in x_lines_list:
                for idx, k in enumerate(x_town_list):
                    dist_[idx] = min(abs(line - X[k])*P[k], dist_[idx])
            tmp = len(x_lines_list)-1
            dist_towns_x[tmp] = min(sum(dist_), dist_towns_x[tmp])


        for j in range(1 << N):  # y-lines
            y_lines_list = [0]

            for k in range(N):
                if j >> k & 1:
                    y_lines_list.append(Y[k])

            dist_ = [INF] * len(y_town_list)
            for line in y_lines_list:
                for idx, k in enumerate(y_town_list):
                    dist_[idx] = min(abs(line - Y[k])*P[k], dist_[idx])
            tmp = len(y_lines_list)-1
                #print(y_lines_list)
            dist_towns_y[tmp] = min(sum(dist_), dist_towns_y[tmp])

        #print(bin(i))
        #print(dist_towns_x)
        #print(dist_towns_y)

        for i in range(N + 1):
            for j in range(N + 1 - i):
                val = dist_towns_x[i] + dist_towns_y[j]
                ans[i + j] = min(ans[i + j], val)


    for a in ans:
        print(a)


if __name__ == "__main__":
    run()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
import numpy as np
#from copy import deepcopy
#from collections import deque


def run():
    N = int(sysread())
    INF = 1 << 50
    X, Y, P = [-1], [-1], [-1]
    ans = [INF] * (N+1)

    INF = 1 << 50
    hline = [0]
    vline = [0]
    dist_arr = np.zeros([2 * (N+1), N+1], dtype= np.int64)
    for i in range(1, N+1):
        x,y,p = map(int, sysread().split())
        X.append(x)
        Y.append(y)
        P.append(p)
        dist_arr[0, i] = abs(x) * p
        dist_arr[N+1, i] = abs(y) * p



    for i in range(1, N+1):
        v = X[i]
        h = Y[i]
        for k in range(1, N+1):
            # vertical
            dist_arr[i, k] = abs(v-X[k]) * P[k]
            dist_arr[N+1+i, k] = abs(h-Y[k]) * P[k]
    dist_arr = dist_arr[:, 1:]
    #print(dist_arr)

    for vals in product((0,1,2), repeat = N):
        xlines = set()
        ylines = set()
        idx = [0, N+1]
        for i, val in enumerate(vals, 1):
            if val == 0:
                continue
            elif val == 1:
                # vertical
                xlines.add(X[i])
                idx.append(i)
            else:
                ylines.add(Y[i])
                idx.append(N+1+i)
        k = len(xlines) + len(ylines)
        idx = np.array(idx)
        print(idx)
        #D = dist_arr[idx, :].min(axis = 0).sum()

        ans[k] = min(ans[k], D)

    for a in ans:
        print(a)


if __name__ == "__main__":
    run()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
import numpy as np
#from copy import deepcopy
#from collections import deque


def run():
    N = int(sysread())
    INF = 1 << 50
    X, Y, P = [-1], [-1], [-1]
    ans = [INF] * (N+1)

    INF = 1 << 50
    hline = [0]
    vline = [0]
    dist_arr = np.zeros([2 * (N+1), N+1], dtype= np.int64)
    for i in range(1, N+1):
        x,y,p = map(int, sysread().split())
        X.append(x)
        Y.append(y)
        P.append(p)
        dist_arr[0, i] = abs(x) * p
        dist_arr[N+1, i] = abs(y) * p



    for i in range(1, N+1):
        v = X[i]
        h = Y[i]
        for k in range(1, N+1):
            # vertical
            dist_arr[i, k] = abs(v-X[k]) * P[k]
            dist_arr[N+1+i, k] = abs(h-Y[k]) * P[k]
    dist_arr = dist_arr[:, 1:]
    #print(dist_arr)

    for vals in product((0,1,2), repeat = N):
        xlines = set()
        ylines = set()
        idx = [0, N+1]
        for i, val in enumerate(vals, 1):
            if val == 0:
                continue
            elif val == 1:
                # vertical
                xlines.add(X[i])
                idx.append(i)
            else:
                ylines.add(Y[i])
                idx.append(N+1+i)
        k = len(xlines) + len(ylines)
        idx = np.array(idx)

        D = dist_arr[idx, :].min(axis = 0).sum()

        ans[k] = min(ans[k], D)

    for a in ans:
        print(a)





if __name__ == "__main__":
    run()