Problem:


Score: 500 points

Problem Statement

New AtCoder City has an infinite grid of streets, as follows:

At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point.

A straight street, which we will call East-West Main Street, runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane.

There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane.

A straight street, which we will call North-South Main Street, runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane.

There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane.

There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas.

The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.

M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets.

Let the walking distance of each citizen be the distance from his/her residential area to the nearest railroad.

M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized.

For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?

Constraints

1 \leq N \leq 15

-10 \ 000 \leq X_i \leq 10 \ 000

-10 \ 000 \leq Y_i \leq 10 \ 000

1 \leq P_i \leq 1 \ 000 \ 000

The locations of the N residential areas, (X_i, Y_i), are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 P_1
X_2 Y_2 P_2
 :  :  :
X_N Y_N P_N

Output

Print the answer in N+1 lines.

The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1.

Sample Input 1

3
1 2 300
3 3 600
1 4 800

Sample Output 1

2900
900
0
0

When K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1, 3, and 1, respectively, to reach a railroad.

Thus, the sum of the walking distances of all citizens, S, is 1 \times 300 + 3 \times 600 + 1 \times 800 = 2900.

When K = 1, if we build a railroad along the street corresponding to the line y = 4 in the coordinate plane, the walking distances of the citizens of Area 1, 2, and 3 become 1, 1, and 0, respectively.

Then, S = 1 \times 300 + 1 \times 600 + 0 \times 800 = 900.

We have many other options for where we build the railroad, but none of them makes S less than 900.

When K = 2, if we build a railroad along the street corresponding to the lines x = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad with the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.

The figure below shows the optimal way to build railroads for the cases K = 0, 1, 2.

The street painted blue represents the roads along which we build railroads.

Sample Input 2

5
3 5 400
5 3 700
5 5 1000
5 7 700
7 5 400

Sample Output 2

13800
1600
0
0
0
0

The figure below shows the optimal way to build railroads for the cases K = 1, 2.

Sample Input 3

6
2 5 1000
5 2 1100
5 5 1700
-2 -5 900
-5 -2 600
-5 -5 2200

Sample Output 3

26700
13900
3200
1200
0
0
0

The figure below shows the optimal way to build railroads for the case K = 3.

Sample Input 4

8
2 2 286017
3 1 262355
2 -2 213815
1 -3 224435
-2 -2 136860
-3 -1 239338
-2 2 217647
-1 3 141903

Sample Output 4

2576709
1569381
868031
605676
366338
141903
0
0
0

The figure below shows the optimal way to build railroads for the case K = 4.



Sample input:
3
1 2 300
3 3 600
1 4 800


Sample output:
2900
900
0
0

Submission status: Time Limit Exceeded

Solution code:
# 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


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

        for line in x_lines_list:
            for idx, x in enumerate(X):
                dist_xx[idx] = min(abs(line - x) * P[idx], dist_xx[idx])
        dist_arr_x[j] = dist_xx

        for line in y_lines_list:
            for idx, y in enumerate(Y):
                dist_yy[idx] = min(abs(line - y) * P[idx], dist_yy[idx])
        dist_arr_y[j] = dist_yy
    return dist_arr_x, dist_arr_y


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

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

    ans = [INF] * (N + 1)

    dist_arr_x , dist_arr_y = generate_dist_arr(X,Y,P)

    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(X[k])

            dists_base = dist_arr_x[j]
            dist_ = []
            for k in x_town_list:
                dist_.append(dists_base[k])

            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])

            dists_base = dist_arr_y[j]
            dist_ = []
            for k in y_town_list:
                dist_.append(dists_base[k])

            tmp = len(y_lines_list) - 1
            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()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
X, Y, P = [], [], []
for i in range(n):
    x, y, p = map(int, input().split())
    X.append(x)
    Y.append(y)
    P.append(p)

DX = [[0] * n for s in range(2**n)]
DY = [[0] * n for s in range(2**n)]
for s in range(2**n):
    for i in range(n):
        DX[s][i] = abs(X[i]) * P[i]
        DY[s][i] = abs(Y[i]) * P[i]
        for j in range(n):
            if s & (1 << j):
                DX[s][i] = min(DX[s][i], abs(X[i] - X[j]) * P[i])
                DY[s][i] = min(DY[s][i], abs(Y[i] - Y[j]) * P[i])

A = [10**20] * (n + 1)
for s in range(3**n):
    tmp = s
    c, sx, sy = 0, 0, 0
    for i in range(n):
        if tmp % 3 != 0:
            c += 1
        if tmp % 3 == 1:
            sx += 2**i
        if tmp % 3 == 2:
            sy += 2**i
        tmp //= 3
    d = 0
    for i in range(n):
        d += min(DX[sx][i], DY[sy][i])
    A[c] = min(A[c], d)
print(*A, sep="\n")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
XYP = [tuple(map(int, input().split())) for i in range(n)]
X = [x for x, y, p in XYP]
Y = [y for x, y, p in XYP]
P = [p for x, y, p in XYP]

DX = [[0] * n for s in range(2**n)]
DY = [[0] * n for s in range(2**n)]
for s in range(2**n):
    for i in range(n):
        DX[s][i] = abs(X[i]) * P[i]
        DY[s][i] = abs(Y[i]) * P[i]
        for j in range(n):
            if s & (1 << j):
                DX[s][i] = min(DX[s][i], abs(X[i] - X[j]) * P[i])
                DY[s][i] = min(DY[s][i], abs(Y[i] - Y[j]) * P[i])

pow2 = [2**i for i in range(n)]
A = [10**20] * (n + 1)
for s in range(3**n):
    tmp = s
    c, sx, sy = 0, 0, 0
    for i in range(n):
        if tmp % 3 == 1:
            sx += pow2[i]
            c += 1
        if tmp % 3 == 2:
            sy += pow2[i]
            c += 1
        tmp //= 3
    d = 0
    for i in range(n):
        d += min(DX[sx][i], DY[sy][i])
    A[c] = min(A[c], d)
print(*A, sep="\n")

Submission status: Time Limit Exceeded

Solution code:
# 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

    for i in range(1, N+1):
        x,y,p = map(int, sysread().split())
        X.append(x)
        Y.append(y)
        P.append(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()

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect

input = sys.stdin.readline

INF = pow(10, 18)

n = int(input())
xyp = [tuple(map(int, input().split())) for _ in range(n)]

ans = [INF] * (n + 1)
ans[0] = 0
for x, y, p in xyp:
    ans[0] += min(abs(x), abs(y)) * p

xmin, ymin = [[INF] * pow(2, n) for _ in range(n)], [[INF] * pow(2, n) for _ in range(n)]
for i in range(pow(2, n)):
    axisx = [0]
    for j in range(n):
        if (i // pow(2, j)) % 2:
            axisx.append(xyp[j][0])
    axisx.sort()
    for j in range(n):
        if (i // pow(2, j)) % 2:
            xmin[j][i] = 0
        else:
            idx = bisect.bisect_left(axisx, xyp[j][0])
            diff1, diff2 = INF, INF
            if idx != len(axisx):
                diff1 = axisx[idx] - xyp[j][0]
            if idx != 0:
                diff2 = xyp[j][0] - axisx[idx - 1]
            xmin[j][i] = min(diff1, diff2)
    
    axisy = [0]
    for j in range(n):
        if (i // pow(2, j)) % 2:
            axisy.append(xyp[j][1])
    axisy.sort()
    for j in range(n):
        if (i // pow(2, j)) % 2:
            ymin[j][i] = 0
        else:
            idx = bisect.bisect_left(axisy, xyp[j][1])
            diff1, diff2 = INF, INF
            if idx != len(axisy):
                diff1 = axisy[idx] - xyp[j][1]
            if idx != 0:
                diff2 = xyp[j][1] - axisy[idx - 1]
            ymin[j][i] = min(diff1, diff2)


for allset in range(pow(2, n)):
    subset = allset
    while subset >= 0:
        tmp = 0
        for i in range(n):
            tmp += min(xmin[i][subset], ymin[i][allset ^ subset]) * xyp[i][2]
        ans[bin(allset).count('1')] = min(ans[bin(allset).count('1')], tmp)
        if subset == 0:
            break
        subset = (subset - 1) & allset

print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect

input = sys.stdin.readline

INF = pow(10, 18)

n = int(input())
xyp = [tuple(map(int, input().split())) for _ in range(n)]

ans = [INF] * (n + 1)
ans[0] = 0
for x, y, p in xyp:
    ans[0] += min(abs(x), abs(y)) * p

xmin, ymin = [[INF] * n for _ in range(pow(2, n))], [[INF] * n for _ in range(pow(2, n))]
for i in range(pow(2, n)):
    axisx = [0]
    for j in range(n):
        if (i // pow(2, j)) % 2:
            axisx.append(xyp[j][0])
    axisx.sort()
    for j in range(n):
        if (i // pow(2, j)) % 2:
            xmin[i][j] = 0
        else:
            idx = bisect.bisect_left(axisx, xyp[j][0])
            diff1, diff2 = INF, INF
            if idx != len(axisx):
                diff1 = axisx[idx] - xyp[j][0]
            if idx != 0:
                diff2 = xyp[j][0] - axisx[idx - 1]
            xmin[i][j] = min(diff1, diff2)
    
    axisy = [0]
    for j in range(n):
        if (i // pow(2, j)) % 2:
            axisy.append(xyp[j][1])
    axisy.sort()
    for j in range(n):
        if (i // pow(2, j)) % 2:
            ymin[i][j] = 0
        else:
            idx = bisect.bisect_left(axisy, xyp[j][1])
            diff1, diff2 = INF, INF
            if idx != len(axisy):
                diff1 = axisy[idx] - xyp[j][1]
            if idx != 0:
                diff2 = xyp[j][1] - axisy[idx - 1]
            ymin[i][j] = min(diff1, diff2)


for allset in range(pow(2, n)):
    subset = allset
    while subset >= 0:
        tmp = 0
        for i in range(n):
            tmp += min(xmin[subset][i], ymin[allset ^ subset][i]) * xyp[i][2]
        ans[bin(allset).count('1')] = min(ans[bin(allset).count('1')], tmp)
        if subset == 0:
            break
        subset = (subset - 1) & allset

print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
XYP = [tuple(map(int, input().split())) for i in range(n)]
X = [x for x, y, p in XYP]
Y = [y for x, y, p in XYP]
P = [p for x, y, p in XYP]

DX = [[0] * n for s in range(2**n)]
DY = [[0] * n for s in range(2**n)]
for s in range(2**n):
    for i in range(n):
        DX[s][i] = abs(X[i]) * P[i]
        DY[s][i] = abs(Y[i]) * P[i]
        for j in range(n):
            if s & (1 << j):
                DX[s][i] = min(DX[s][i], abs(X[i] - X[j]) * P[i])
                DY[s][i] = min(DY[s][i], abs(Y[i] - Y[j]) * P[i])

A = [10**20] * (n + 1)
for s in range(3**n):
    tmp = s
    c, sx, sy = 0, 0, 0
    i = 0
    while tmp:
        if tmp % 3 == 1:
            sx += 2**i
            c += 1
        if tmp % 3 == 2:
            sy += 2**i
            c += 1
        tmp //= 3
        i += 1
    A[c] = min(A[c], sum(min(DX[sx][i], DY[sy][i]) for i in range(n)))
print(*A, sep="\n")

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = [list(map(int,input().split())) for _ in range(N)]

#準備：１次元に投影してソート
A1 = [(A[i][0],A[i][2],i) for i in range(N)]
A2 = [(A[i][1],A[i][2],i) for i in range(N)]
A1.sort()
A2.sort()

#要素の対応関係
I1 = [0 for _ in range(N)]
I2 = [0 for _ in range(N)]
for i in range(N):
  I1[A1[i][2]] = i #i番目がソート後に何番目になったか
  I2[A2[i][2]] = i

#準備：S_の外延表記sを求める
def settify(S):
  s = []
  i = 0
  while S > 0:
    if (S & 1):  
      s.append(i)
    S = S >> 1
    i += 1
  return s

#準備：2べき
b = [2**n for n in range(N)]

#準備：集合sのindexを2番目の命名方法に変換
def convert(s,I):
  return sum([b[I[x]] for x in s])

Sets = [settify(S_) for S_ in range(2**N)]

def DP_solve(S, A, L): #A:ソート済み、L:最大本数
  if S == 0:
    return [0 for _ in range(L+1)]
  s = Sets[S]
  n = len(s)
  B = [A[i] for i in s]
  Q = [[0 for _ in range(n)] for _ in range(n)]#準備用
  for j in range(n):
    for k in range(j):#k < j
      Q[k][j] = sum([min(abs(B[l][0]),abs(B[l][0]-B[k][0]),abs(B[l][0]-B[j][0]))*B[l][1] for l in range(k+1,j)])
  DP = [[0 for _ in range(n)] for _ in range(L+1)]
  
  for j in range(n):
    DP[1][j] = sum([min(abs(B[l][0]),abs(B[l][0]-B[j][0]))*B[l][1] for l in range(j)]) #i本目を置いて、最後がj番目の集落
  
  for i in range(2,L+1):
    for j in range(1,n):
      DP[i][j] = min([DP[i-1][k]+Q[k][j] for k in range(j)])
      #i本目を置いて、最後がj番目の集落
      
  return [min([DP[i][j]+sum([min(abs(B[l][0]),abs(B[l][0]-B[j][0]))*B[l][1] for l in range(j+1,n)]) for j in range(n)]) if i >= 1 else sum([abs(B[l][0])*B[l][1] for l in range(n)]) for i in range(L+1)]

#1次元へ帰着
X = [DP_solve(S_,A1, N) for S_ in range(2**N)]
Y = [DP_solve(S_,A2, N) for S_ in range(2**N)]
 
#print(X)
#print(Y)
#print(I1,A1)
#print(I2,A2)

for k in range(N+1):#合計k本(大通り以外)
  ans = 10**20
  for S_ in range(2**N):#X側に集合S_が割り当てられる
    ans = min(ans,min([X[convert(Sets[S_],I1)][i] + Y[convert(Sets[2**N-1-S_],I2)][k-i] for i in range(k+1)]))
    #print(S_,convert(Sets[S_],I1),convert(Sets[2**N-1-S_],I2),ans)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

x = []
y = []
p = []
for i in range(1,n+1):
    xx,yy,pp = map(int,input().split())
    x.append(xx)
    y.append(yy)
    p.append(pp)

yokomi = [10**9]*(n*(1<<n))
tatemi = [10**9]*(n*(1<<n))


for i in range(1<<n):
    yoko = [0]
    tate = [0]
    for j in range(n):
        if (i>>j)&1 == 1:
            yoko.append(y[j])
            tate.append(x[j])
    for j in range(n):
        pp = 10**9
        qq = 10**9
        for e in yoko:
            if pp > abs(e-y[j]):
                pp = abs(e-y[j])
        for e in tate:
            if qq > abs(e-x[j]):
                qq = min(qq,abs(e-x[j]))
        yokomi[i*n+j] = pp*p[j]
        tatemi[i*n+j] = qq*p[j]



res = [10**20]*(n+1)

for idxs in range(3**n):
    c = 0
    xn = 0
    yn = 0
    p2 = 1

    num = idxs
    while num:
        num,e = divmod(num,3)
        if e == 1:
            yn += p2
            c += 1
        elif e == 2:
            xn += p2
            c += 1
        p2 *= 2

    tmp = 0
    xn *= n
    yn *= n
    for i in range(n):
        if tatemi[xn+i] < yokomi[yn+i]:
            tmp += tatemi[xn+i]
        else:
            tmp += yokomi[yn+i]
    if res[c] > tmp:
        res[c] = tmp
for e in res:
    print(e)

Submission status: Time Limit Exceeded

Solution code:

from itertools import product
n = 15

x = []
y = []
p = []
for i in range(1,n+1):
    x.append(i+10000)
    y.append(i+10000)
    p.append(i)

yokomi = [10**9]*(n*(1<<n))
tatemi = [10**9]*(n*(1<<n))


for i in range(1<<n):
    yoko = [10000]
    tate = [10000]
    for j in range(n):
        if (i>>j)&1 == 1:
            yoko.append(y[j])
            tate.append(x[j])
    for j in range(n):
        pp = 10**9
        qq = 10**9
        for e in yoko:
            if pp > abs(e-y[j]):
                pp = abs(e-y[j])
        for e in tate:
            if qq > abs(e-x[j]):
                qq = min(qq,abs(e-x[j]))
        yokomi[i*n+j] = pp*p[j]
        tatemi[i*n+j] = qq*p[j]



res = [10**20]*(n+1)

for idxs in range(3**n):
    c = 0
    xn = 0
    yn = 0
    p2 = 1

    num = idxs
    while num:
        num,e = divmod(num,3)
        if e == 1:
            yn += p2
            c += 1
        elif e == 2:
            xn += p2
            c += 1
        p2 *= 2

    tmp = 0
    xn *= n
    yn *= n
    for i in range(n):
        if tatemi[xn+i] < yokomi[yn+i]:
            tmp += tatemi[xn+i]
        else:
            tmp += yokomi[yn+i]
    if res[c] > tmp:
        res[c] = tmp
for e in res:
    print(e)

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
    INF = 10 ** 12
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    X.append(0)
    Y.append(0)
    hor = [[0] * N for _ in range(2 ** N)]
    ver = [[0] * N for _ in range(2 ** N)]

    mx = [[0] * (N+1) for _ in range(N)]
    my = [[0] * (N+1) for _ in range(N)]
    for i in range(N):
        for j in range(N+1):
            mx[i][j] = abs(X[i] - X[j])
            my[i][j] = abs(Y[i] - Y[j])

    for i in range(2 ** N):
        for j in range(N):
            hor[i][j] = mx[j][N]
            ver[i][j] = my[j][N]
            k = i
            for b in range(N):
                if k & 1:
                    hor[i][j] = min(hor[i][j], mx[j][b])
                    ver[i][j] = min(ver[i][j], my[j][b])
                k >>= 1

    ans = [INF] * (N + 1)
    for i in range(2 ** N):
        cnt = 0
        j = i
        for _ in range(N):
            cnt += j & 1
            j >>= 1

        j = i
        while True:
            s = 0
            for k in range(N):
                s += min(hor[j][k], ver[i-j][k]) * P[k]
            ans[cnt] = min(ans[cnt], s)
            if j == 0: break
            j = (j-1) & i

    print(*ans, sep='\n')

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys, queue, math, copy, itertools, bisect, collections, heapq


def main():
    INF = 10 ** 15
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    X.append(0)
    Y.append(0)
    hor = [[0] * N for _ in range(2 ** N)]
    ver = [[0] * N for _ in range(2 ** N)]

    mx = [[0] * (N+1) for _ in range(N)]
    my = [[0] * (N+1) for _ in range(N)]
    for i in range(N):
        for j in range(N+1):
            mx[i][j] = abs(X[i] - X[j])
            my[i][j] = abs(Y[i] - Y[j])


    for i in range(2 ** N):
        for j in range(N):
            hor[i][j] = mx[j][N]
            ver[i][j] = my[j][N]
            k = i
            for b in range(N):
                if k & 1:
                    hor[i][j] = min(hor[i][j], mx[j][b])
                    ver[i][j] = min(ver[i][j], my[j][b])
                k >>= 1

    ans = [INF] * (N + 1)
    for i in range(2 ** N):
        cnt = 0
        j = i
        for _ in range(N):
            cnt += j & 1
            j >>= 1

        j = i
        while True:
            s = 0
            for b in range(N):
                s += min(hor[j][b], ver[i-j][b]) * P[b]
            ans[cnt] = min(ans[cnt], s)
            if j == 0: break
            j -= 1
            j &= i

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys, queue, math, copy, itertools, bisect, collections, heapq


def main():
    INF = 10 ** 15
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    hor = [[0] * N for _ in range(2 ** N)]
    ver = [[0] * N for _ in range(2 ** N)]

    for i in range(2 ** N):
        for j in range(N):
            hor[i][j] = abs(X[j])
            ver[i][j] = abs(Y[j])
            k = i
            for b in range(N):
                if k & 1:
                    hor[i][j] = min(hor[i][j], abs(X[j] - X[b]))
                    ver[i][j] = min(ver[i][j], abs(Y[j] - Y[b]))
                k >>= 1

    ans = [INF] * (N + 1)
    for i in range(2 ** N):
        cnt = 0
        j = i
        for _ in range(N):
            cnt += j & 1
            j >>= 1

        j = i
        while True:
            s = 0
            for b in range(N):
                s += min(hor[j][b], ver[i-j][b]) * P[b]
            ans[cnt] = min(ans[cnt], s)
            if j == 0: break
            j -= 1
            j &= i

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys, queue, math, copy, itertools, bisect, collections, heapq


def main():
    INF = 10 ** 15
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    r = []
    st = 0
    for a, b, c in zip(X, Y, P):
        r.append(min(abs(a), abs(b)))
        st += r[-1] * c
    hor = [copy.copy(r) for _ in range(2 ** N)]
    ver = [copy.copy(r) for _ in range(2 ** N)]

    for i in range(2 ** N):
        k = i
        for b in range(N):
            if k % 2 == 1:
                for j in range(N):
                    hor[i][j] = min(hor[i][j], abs(X[j] - X[b]))
                    ver[i][j] = min(ver[i][j], abs(Y[j] - Y[b]))
            k //= 2

    ans = [st] * (N + 1)
    for i in range(2 ** N):
        cnt = 0
        j = i
        for _ in range(N):
            cnt += j & 1
            j >>= 1

        j = i
        while True:
            s = 0
            for b in range(N):
                s += min(hor[j][b], ver[i-j][b]) * P[b]
            ans[cnt] = min(ans[cnt], s)
            if j == 0: break
            j -= 1
            j &= i

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys, queue, math, copy, itertools, bisect, collections, heapq


def main():
    INF = 10 ** 15
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    r = []
    st = 0
    for a, b, c in zip(X, Y, P):
        r.append(min(abs(a), abs(b)))
        st += r[-1]
    hor = [copy.copy(r) for _ in range(2 ** N)]
    ver = [copy.copy(r) for _ in range(2 ** N)]

    for i in range(2 ** N):
        k = i
        for b in range(N):
            if k % 2 == 1:
                for j in range(N):
                    hor[i][j] = min(hor[i][j], abs(X[j] - X[b]))
                    ver[i][j] = min(ver[i][j], abs(Y[j] - Y[b]))
            k //= 2

    ans = [INF] * (N + 1)
    for i in range(2 ** N):
        cnt = 0
        j = i
        for _ in range(N):
            cnt += j & 1
            j >>= 1

        j = i
        while True:
            s = 0
            for b in range(N):
                s += min(hor[j][b], ver[i-j][b]) * P[b]
            ans[cnt] = min(ans[cnt], s)
            if j == 0: break
            j -= 1
            j &= i

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys, queue, math, copy, itertools, bisect, collections, heapq


def main():
    INF = 10 ** 15
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    r = []
    st = 0
    for a, b, c in zip(X, Y, P):
        r.append(min(abs(a), abs(b)) * c)
        st += r[-1]
    hor = [copy.copy(r) for _ in range(2 ** N)]
    ver = [copy.copy(r) for _ in range(2 ** N)]

    for i in range(2 ** N):
        k = i
        for b in range(N):
            if k % 2 == 1:
                for j in range(N):
                    hor[i][j] = min(hor[i][j], abs(X[j] - X[b]) * P[j])
                    ver[i][j] = min(ver[i][j], abs(Y[j] - Y[b]) * P[j])
            k //= 2

    ans = [st] * (N + 1)
    for i in range(2 ** N):
        cnt = 0
        j = i
        for _ in range(N):
            cnt += j & 1
            j >>= 1

        j = i
        while True:
            s = 0
            for b in range(N):
                s += min(hor[j][b], ver[i-j][b])
            ans[cnt] = min(ans[cnt], s)
            if j == 0: break
            j -= 1
            j &= i

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement, product
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**12

    N = II()
    positions = deque()
    for i in range(N):
        positions.append(LMIIS())
    
    #縦、横の最短距離
    v = []
    h = []
    for i in range(2**N):
        v.append([INF]*N)
        h.append([INF]*N)
    for i in range(2**N):
        v_lines = deque([0])
        h_lines = deque([0])
        for j in range(N):
            if 2 ** j & i:
                v_lines.append(positions[j][0])
                h_lines.append(positions[j][1])
        for j,(x,y,p) in enumerate(positions):
            min_x = INF
            min_y = INF
            for x_c in v_lines:
                min_x = min(min_x, abs(x_c - x)*p)
            for y_c in h_lines:
                min_y = min(min_y, abs(y_c - y)*p)
            v[i][j] = min_x
            h[i][j] = min_y

    ans = [INF]*(N+1)
    #for pattern in product(range(3),repeat=N):
    n_bit_mask = (1<<N)-1
    for pattern in range(2**N):
        
        cities = [i for i in range(N) if 2**i&pattern==0]
        key = N - len(cities)
        

        pattern_v = pattern
        pattern_h = 0
        
        while True:
            sum_diff = 0
            for i in cities:
                sum_diff += min(v[pattern_v][i],h[pattern_h][i])
            ans[key] = min(ans[key], sum_diff)
            if pattern_v == 0:
                break
            pattern_v = (pattern_v-1)&pattern
            pattern_h = pattern - pattern_v
    print('\n'.join(map(str,ans)))





            



    

    



    
main()