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:
def main():
    n = int(input())
    xyp = [tuple(map(int, input().split())) for _ in range(n)]

    # 初期値は0との距離
    x_dists = [[abs(xyp[j][0]) for j in range(n)] for _ in range(2**n)]
    y_dists = [[abs(xyp[j][1]) for j in range(n)] for _ in range(2**n)]

    for k in range(2**n):
        # パターンk
        trainX = [0]
        trainY = [0]
        rest = []
        for i, (x, y, p) in enumerate(xyp):
            if (k>>i)&1:
                # 自分の集落に路線がある場合、距離は0
                x_dists[k][i] = 0
                y_dists[k][i] = 0
                trainX.append(x)
                trainY.append(y)
            else:
                # 自分の集落に路線がない場合 路線がある集落との比較を後で行う
                rest.append(i)

        for j in rest:
            minx = 10**12
            for xx in trainX:
                minx = min(minx, abs(xyp[j][0]-xx))
            x_dists[k][j] = minx

            miny = 10**9
            for yy in trainY:
                miny = min(miny, abs(xyp[j][1]-yy))
            y_dists[k][j] = miny
            

    anss = [10**12] * (n+1)
    anss[n] = 0
    for pint in range(1, 3**n):
        train_counts = 0
        patx = 0
        paty = 0
        not_on_train = set()
        for j in range(n):
            pint, jp = divmod(pint, 3)
            if jp == 0:
                patx += 2**j
                train_counts += 1
            elif jp == 1:
                paty += 2**j
                train_counts += 1
            else:
                not_on_train.add(j)

        if anss[train_counts] == 0:
            continue

        # 路線上にない点において、最小の移動距離をコスト計上
        cost = 0
        for i in not_on_train:
            cost += min(x_dists[patx][i], y_dists[paty][i])*xyp[i][2]
            if cost > anss[train_counts]:
                break

        anss[train_counts] = min(anss[train_counts], cost)

    for ans in anss:
        print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = [None] * N
Y = [None] * N
P = [None] * N
INF = 10**12

def solve():
    xsel = [[None]*N for _ in range(1<<N)]
    ysel = [[None]*N for _ in range(1<<N)]

    for i in range(2**N):
        for j in range(N):
            xsel[i][j] = abs(X[j])
            ysel[i][j] = abs(Y[j])
            for k in range(N):
                if i>>k & 1:
                    xsel[i][j] = min(xsel[i][j], abs(X[j]-X[k]))
                    ysel[i][j] = min(ysel[i][j], abs(Y[j]-Y[k]))
    
    ans = [INF] * (N+1)
    for i in range(1<<N):
        cnt = 0
        for j in range(N):
            if i>>j & 1:
                cnt += 1
        
        for j in range(i,-1,-1):
            j &= i
            cost = 0
            for k in range(N):
                if not (i>>k & 1):
                    cost += min(xsel[j][k], ysel[i-j][k]) * P[k]
            
            ans[cnt] = min(ans[cnt],cost)

    return ans


def main():
    for i in range(N):
        X[i], Y[i], P[i] = map(int, input().split())
    [print(s) for s in solve()]

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#解法１

#入力
N = int(input())
town = [list(map(int,input().split())) for i in range(N)]

#縦と横のリストを作る
vert = [town[i][0] for i in range(N)]
hori = [town[i][1] for i in range(N)]

#bit全探索
dX = [[] for m in range(N+1)]
for i in range(2**N):
    m = 0
    vertsel = []
    dXconst = [abs(town[l][0])*town[l][2] for l in range(N)]
    for j in range(N):
        if ((i >> j) & 1):
            vertsel.append(town[j][0])
            m += 1
    for k in vertsel:
        dXconst = [min(dXconst[l],abs(town[l][0]-k)*town[l][2]) for l in range(N)]
    dX[m].append(dXconst)

dY = [[] for n in range(N+1)]
for i in range(2**N):
    n = 0
    horisel = []
    dYconst = [abs(town[l][1])*town[l][2] for l in range(N)]
    for j in range(N):
        if ((i >> j) & 1):
            horisel.append(town[j][1])
            n += 1
    for k in horisel:
        dYconst = [min(dYconst[l],abs(town[l][1]-k)*town[l][2]) for l in range(N)]
    dY[n].append(dYconst)

#print('len(dX) ==',len(dX))
#print('len(dY) ==',len(dY))
#print(dX)
#print(dY)

#歩行距離を各Kに対してdX,dYをクロスさせて求める
ans = [float('inf') for K in range(N+1)]
for K in range(N+1):
    #print(K)
    for m in range(K+1):
        for dx in dX[m]:
            for dy in dY[K-m]:
                #print(dx)
                #print(dy)
                ans[K] = min(sum([min(dx[l],dy[l]) for l in range(N)]),ans[K])

#出力
[print(ans[K]) for K in range(N+1)]

Submission status: Time Limit Exceeded

Solution code:
#解法１

#入力
N = int(input())
town = [list(map(int,input().split())) for i in range(N)]

#縦と横のリストを作る
vert = [town[i][0] for i in range(N)]
hori = [town[i][1] for i in range(N)]

#bit全探索
dX = []
for i in range(2**N):
    vertsel = []
    dXconst = [abs(town[l][0])*town[l][2] for l in range(N)]
    for j in range(N):
        if ((i >> j) & 1):
            vertsel.append(town[j][0])
    for k in vertsel:
        dXconst = [min(dXconst[l],abs(town[l][0]-k)*town[l][2]) for l in range(N)]
    dX.append(dXconst)

dY = []
for i in range(2**N):
    horisel = []
    dYconst = [abs(town[l][1])*town[l][2] for l in range(N)]
    for j in range(N):
        if ((i >> j) & 1):
            horisel.append(town[j][1])
    for k in horisel:
        dYconst = [min(dYconst[l],abs(town[l][1]-k)*town[l][2]) for l in range(N)]
    dY.append(dYconst)

#print('len(dX) ==',len(dX))
#print('len(dY) ==',len(dY))
#print(dX)
#print(dY)

#歩行距離を3進数を使って求める
ans = [float('inf') for K in range(N+1)]
for i in range(3**N):
    #print(i)
    Xvalue = 0
    Yvalue = 0
    K = 0
    #print('i ==',i)
    for j in range(N):
        p = i%3
        i = i//3
        if p == 1:
            Xvalue += 2**j
            K += 1
        if p == 2:
            Yvalue += 2**j
            K += 1
    #if K > N:
        #print('Error K ==',K)
    #elif Xvalue > len(dX):
        #print('Error Xvalue ==',Xvalue)
    #elif Yvalue > len(dY):
        #print('Error Yvalue ==',Yvalue)
    #else:
        #print(dX[Xvalue],dY[Yvalue])
    #print(K)
    #print(K,Xvalue,Yvalue)
    ans[K] = min(sum([min(dX[Xvalue][l],dY[Yvalue][l]) for l in range(N)]),ans[K])

#出力
[print(ans[K]) for K in range(N+1)]

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
n = int(input())
 
x = []
y = []
p = []
for i in range(1,n+1):
    xx,yy,pp = map(int,input().split())
    x.append(xx+10000)
    y.append(yy+10000)
    p.append(pp)
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]

def iter_p_adic(p, n):
    tmp = [range(p)] * n
    return product(*tmp)

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


iterator = iter_p_adic(3,n)
for idxs in iterator:
    c = 0
    xn = 0
    yn = 0
    p2 = 1

    for e in idxs:
        if e == 1:
            yn += p2
            c += 1
        elif e == 2:
            xn += p2
            c += 1
        p2 *= 2

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

Submission status: Time Limit Exceeded

Solution code:
INF = 10 ** 12

N = int(input())
X = [0] * N
Y = [0] * N
P = [0] * N
for i in range(N):
    X[i], Y[i], P[i] = map(int,input().split())

score_X = []

for i in range(2 ** N):
    x = []
    I = i
    for j in range(N):
        i = I
        score = abs(X[j]) * P[j]
        for k in range(N):
            if i & 1:
                score = min(score, abs(X[j] - X[k]) * P[j])
            i >>= 1
        x.append(score)
    score_X.append(x)

score_Y = []

for i in range(2 ** N):
    y = []
    I = i
    for j in range(N):
        i = I
        score = abs(Y[j]) * P[j]
        for k in range(N):
            if i & 1:
                score = min(score, abs(Y[j] - Y[k]) * P[j])
            i >>= 1
        y.append(score)
    score_Y.append(y)

ans = [INF] * (N + 1)

for i in range(2 ** N):
    cnt = 0
    for j in range(N):
        if (i >> j) & 1:
            cnt += 1
    for j in range(i, -1, -1):
        j &= i
        score = 0
        for k in range(N):
            if not (i >> k) & 1:
                score += min(score_X[j][k], score_Y[i - j][k])
        ans[cnt] = min(ans[cnt], score)
    

for i in range(N + 1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
n = int(input())
 
x = []
y = []
p = []
for i in range(1,n+1):
    xx,yy,pp = map(int,input().split())
    x.append(xx+10000)
    y.append(yy+10000)
    p.append(pp)

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]

def iter_p_adic(p, n):
    tmp = [range(p)] * n
    return product(*tmp)

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


iterator = iter_p_adic(3,n)
for idxs in iterator:
    c = 0
    xn = 0
    yn = 0
    p2 = 1

    for i in range(n):
        if idxs[i] == 1:
            yn += p2
            c += 1
        elif idxs[i] == 2:
            xn += p2
            c += 1
        p2 *= 2

    tmp = 0
    for i in range(n):

        tmp += min(tatemi[xn*n+i],yokomi[yn*n+i])
    if c < n+1 and 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]

def iter_p_adic(p, n):
    tmp = [range(p)] * n
    return product(*tmp)

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


iterator = iter_p_adic(3,n)
for idxs in iterator:
    c = 0
    xn = 0
    yn = 0
    p2 = 1

    for i in range(n):
        if idxs[i] == 1:
            yn += p2
            c += 1
        elif idxs[i] == 2:
            xn += p2
            c += 1
        p2 *= 2

    tmp = 0
    for i in range(n):

        tmp += min(tatemi[xn*n+i],yokomi[yn*n+i])
    if c < n+1 and res[c] > tmp:
        res[c] = tmp
for e in res:
    print(e)

Submission status: Time Limit Exceeded

Solution code:
INF = 10 ** 12

N = int(input())
X = [0] * N
Y = [0] * N
P = [0] * N
for i in range(N):
    X[i], Y[i], P[i] = map(int,input().split())

score_X = []

for i in range(2 ** N):
    x = []
    I = i
    for j in range(N):
        i = I
        score = abs(X[j]) * P[j]
        for k in range(N):
            if i & 1:
                score = min(score, abs(X[j] - X[k]) * P[j])
            i >>= 1
        x.append(score)
    score_X.append(x)

score_Y = []

for i in range(2 ** N):
    y = []
    I = i
    for j in range(N):
        i = I
        score = abs(Y[j]) * P[j]
        for k in range(N):
            if i & 1:
                score = min(score, abs(Y[j] - Y[k]) * P[j])
            i >>= 1
        y.append(score)
    score_Y.append(y)

ans = [INF] * (N + 1)

for i in range(3 ** N):
    cnt = N
    index_X = 0
    index_Y = 0
    for j in range(N):
        index_X <<= 1
        index_Y <<= 1
        if i % 3 == 1:
            index_X += 1
        elif i % 3 == 2:
            index_Y += 1
        else:
            cnt -= 1
        i //= 3
    
    score = 0
    for j in range(N):
        score += min(score_X[index_X][j], score_Y[index_Y][j])
    
    ans[cnt] = min(ans[cnt], score)

for i in range(N + 1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import math
n = int(input())

xlist = []
ylist = []
plist = []
xcomb_list = set()
ycomb_list = set()
x_dict = dict()
y_dict = dict()

for i in range(n):
    x,y,p = map(int,input().split())
    xlist.append(x)
    ylist.append(y)
    xcomb_list.add(("x",x,i))
    ycomb_list.add(("y",y,i))
    plist.append(p)

for i in range(n+1):
    xcomb_set = list(combinations(xcomb_list,i))
    for m in range(n):
        x_dict[(m,tuple([0]))] = abs(xlist[m])
        if i > 0:
            for q in range(len(xcomb_set)):
                min = abs(xlist[m])
                xs_list = [0]
                for xs in xcomb_set[q]:
                    t = abs(xlist[m] - xs[1])
                    xs_list.append(xs[1])
                    if t < min:
                        min = t
                xs_list.sort()
                x_dict[(m,tuple(xs_list))] = min

for i in range(n+1):
    ycomb_set = list(combinations(ycomb_list,i))
    for m in range(n):
        y_dict[(m,tuple([0]))] = abs(ylist[m])
        if i > 0:
            for q in range(len(ycomb_set)):
                min = abs(ylist[m])
                ys_list = [0]
                for ys in ycomb_set[q]:
                    t = abs(ylist[m] - ys[1])
                    ys_list.append(ys[1])
                    if t < min:
                        min = t
                ys_list.sort()
                y_dict[(m,tuple(ys_list))] = min

for i in range(n+1):
    comb_list = xcomb_list.union(ycomb_list)
    c = combinations(comb_list,i)
    
    min_sum = math.inf
    for j in c:
        mult = set()
        for l in j:
            mult.add(l[2])
        if len(mult) < len(j):
            continue
        kv_list = [0]
        kh_list = [0]
        for l in j:
            if l[0] == "x":
                kv_list.append(l[1])
            else:
                kh_list.append(l[1])
        kv_list = list(set(kv_list))
        kh_list = list(set(kh_list))
        kv_list.sort()
        kh_list.sort()
        sum = 0
        for m in range(n):
            
            x_min = x_dict[(m,tuple(kv_list))]
            y_min = y_dict[(m,tuple(kh_list))]
            if x_min < y_min:
                min = x_min
            else:
                min = y_min

            sum += plist[m] * min

        if sum < min_sum:
            min_sum = sum
    print(min_sum)

Submission status: Time Limit Exceeded

Solution code:
INF = 10 ** 12

N = int(input())
X = [0] * N
Y = [0] * N
P = [0] * N
for i in range(N):
    X[i], Y[i], P[i] = map(int,input().split())

score_X = []

for i in range(2 ** N):
    x = []
    I = i
    for j in range(N):
        i = I
        score = abs(X[j]) * P[j]
        for k in range(N):
            if i & 1:
                score = min(score, abs(X[j] - X[k]) * P[j])
            i >>= 1
        x.append(score)
    score_X.append(x)

score_Y = []

for i in range(2 ** N):
    y = []
    I = i
    for j in range(N):
        i = I
        score = abs(Y[j]) * P[j]
        for k in range(N):
            if i & 1:
                score = min(score, abs(Y[j] - Y[k]) * P[j])
            i >>= 1
        y.append(score)
    score_Y.append(y)

ans = [INF] * (N + 1)

for i in range(3 ** N):
    cnt = N
    index_X = 0
    index_Y = 0
    for j in range(N):
        index_X *= 2
        index_Y *= 2
        if i % 3 == 1:
            index_X += 1
        elif i % 3 == 2:
            index_Y += 1
        else:
            cnt -= 1
        i //= 3
    
    score = 0
    for j in range(N):
        score += min(score_X[index_X][j], score_Y[index_Y][j])
    
    ans[cnt] = min(ans[cnt], score)

for i in range(N + 1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
 
 
def main():
 
    N = int(sys.stdin.readline().rstrip())
    X, Y, P = [], [], []
    for i in range(N):
        x, y, p = map(int, sys.stdin.readline().rstrip().split())
        X.append(x)
        Y.append(y)
        P.append(p)
 
    # 前処理 2**15パターンの歩行距離
    hokou_X = [[0] * (2**N) for _ in range(N)]
    hokou_Y = [[0] * (2**N) for _ in range(N)]
 
    for i in range(2**N):
        for n in range(N):
            min_hokou_x_n = abs(X[n])
            min_hokou_y_n = abs(Y[n])
            for shift in range(N):
                if (i >> shift) & 1 == 1:
                    min_hokou_x_n = min(abs(X[n] - X[shift]), min_hokou_x_n)  # もっと近ければ更新
                    min_hokou_y_n = min(abs(Y[n] - Y[shift]), min_hokou_y_n)  # もっと近ければ更新
            hokou_X[n][i] += min_hokou_x_n * P[n]
            hokou_Y[n][i] += min_hokou_y_n * P[n]
 
    # print(hokou_X)
    # print(hokou_Y)
 
    hokou = [20000 * 15 * max(P)] * (N + 1)
 
    # 縦、横、なし　の3パターンを計算
    for i in range(3**N):
 
        honsuu = 0
        xi = 0
        yi = 0
        # 路線の引き方を確認する
        for n in range(N):
            pt = i % (3**(n + 1)) // 3**n
            if pt == 1:  # 縦を使う
                xi += 2**n
                honsuu += 1
            if pt == 2:  # 横を使う
                yi += 2**n
                honsuu += 1
 
        # この路線の引き方の場合の最小値を考える
        hokou_tmp = 0
        for n in range(N):
            hokou_tmp += min(hokou_X[n][xi], hokou_Y[n][yi])
 
        hokou[honsuu] = min(hokou_tmp, hokou[honsuu])
 
    for i in hokou:
        print(i)
 
 
main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import combinations as combi
input = sys.stdin.readline
N = int(input())
a = [tuple(map(int, input().split())) for _ in range(N)]
#print(a)
tableud = [0] * (N ** 2)
tablelr = [0] * (N ** 2)
for i in range(N):
  x, y, _ = a[i]
  for j in range(N):
    tableud[i * N + j] = min(abs(a[j][0] - x), min(abs(a[j][0]), abs(a[j][1]))) * a[j][2]
    tablelr[i * N + j] = min(abs(a[j][1] - y), min(abs(a[j][0]), abs(a[j][1]))) * a[j][2]
tableudi = [10 ** 20 for _ in range((1 << N) * N)]
tablelri = [10 ** 20 for _ in range((1 << N) * N)]
for i in range(1 << N):
  for j in range(N):
    if i & (1 << j):
      for k in range(N):
        tableudi[i * N + k] = min(tableudi[i * N + k], tableud[j * N + k])
        tablelri[i * N + k] = min(tablelri[i * N + k], tablelr[j * N + k])
    else:
      for k in range(N):
        tableudi[i * N + k] = min(tableudi[i * N + k], min(abs(a[k][0]), abs(a[k][1])) * a[k][2])
        tablelri[i * N + k] = min(tablelri[i * N + k], min(abs(a[k][0]), abs(a[k][1])) * a[k][2])
res = [10 ** 20] * (N + 1)
for t in range(1 << N):
  i = t + 0
  c = bin(t).count("1")
  while True:
    x = i + 0
    y = t ^ x
    rres = 0
    for j in range(N):
      rres += min(tableudi[x * N + j], tablelri[y * N + j])
    res[c] = min(res[c], rres)
    #print(t, res, rres)
    if i == 0: break
    i = (i - 1) & t
for r in res: print(r)

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():

    N = int(sys.stdin.readline().rstrip())
    X, Y, P = [], [], []
    for i in range(N):
        x, y, p = map(int, sys.stdin.readline().rstrip().split())
        X.append(x)
        Y.append(y)
        P.append(p)

    # 前処理 2**15パターンの歩行距離
    hokou_X = [[0] * (2**N) for _ in range(N)]
    hokou_Y = [[0] * (2**N) for _ in range(N)]

    for i in range(2**N):
        for n in range(N):
            min_hokou_x_n = abs(X[n])
            min_hokou_y_n = abs(Y[n])
            for shift in range(N):
                if (i >> shift) & 1 == 1:
                    min_hokou_x_n = min(abs(X[n] - X[shift]), min_hokou_x_n)  # もっと近ければ更新
                    min_hokou_y_n = min(abs(Y[n] - Y[shift]), min_hokou_y_n)  # もっと近ければ更新
            hokou_X[n][i] += min_hokou_x_n * P[n]
            hokou_Y[n][i] += min_hokou_y_n * P[n]

    # print(hokou_X)
    # print(hokou_Y)

    hokou = [20000 * 15 * max(P)] * (N + 1)

    # 縦、横、なし　の3パターンを計算
    for i in range(3**N):

        honsuu = 0
        xi = 0
        yi = 0
        # 路線の引き方を確認する
        for n in range(N):
            pt = i % (3**(n + 1)) // 3**n
            if pt == 1:  # 縦を使う
                xi += 2**n
                honsuu += 1
            if pt == 2:  # 横を使う
                yi += 2**n
                honsuu += 1

        # この路線の引き方の場合の最小値を考える
        hokou_tmp = 0
        for n in range(N):
            hokou_tmp += min(hokou_X[n][xi], hokou_Y[n][yi])

        hokou[honsuu] = min(hokou_tmp, hokou[honsuu])

    for i in hokou:
        print(i)


main()

Submission status: Time Limit Exceeded

Solution code:
# 写経
# https://atcoder.jp/contests/m-solutions2020/submissions/15454214

import sys
import numpy as np
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
XYP = np.array(read().split(), np.int64)

def resolve():
	X, Y, P = XYP[::3], XYP[1::3], XYP[2::3]
	N = len(X)
	X_cost = np.empty((1 << N,N), np.int64)
	X_cost[0] = np.abs(X) * P
	for n in range(N):
		B = 1 << n
		X_cost[B:2 * B] = np.minimum(X_cost[:B], np.abs((X-X[n]) * P))
	#print(X_cost)
	Y_cost = np.empty((1 << N,N), np.int64)
	Y_cost[0] = np.abs(Y) * P
	for n in range(N):
		B = 1 << n
		Y_cost[B:2 * B] = np.minimum(Y_cost[:B], np.abs((Y-Y[n]) * P))
	#print(Y_cost)
	popcount = np.zeros(1 << N, np.int64)
	for n in range(N):
		popcount[1 << n:1 << n + 1] = popcount[:1 << n] + 1
	#print(popcount)

	INF = 10**18
	ans = np.full(N + 1, INF, np.int64)
	for s in range(1 << N):
		size = popcount[s]
		t = s
		#print("s" + str(s))
		#print("size" + str(size))
		while True:
			u = s ^ t
			#print("u" + str(u))
			#print("t" + str(t))
			cost = (np.minimum(X_cost[t], Y_cost[u])).sum()
			ans[size] = min(ans[size], cost)
			if t == 0:
				break
			t = (t - 1) & s
	for Ans in ans:
		print(Ans)
    
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import combinations as combi
input = sys.stdin.readline
N = int(input())
a = [tuple(map(int, input().split())) for _ in range(N)]
#print(a)
tableud = [[0] * N for _ in range(N)]
tablelr = [[0] * N for _ in range(N)]
for i in range(N):
  x, y, _ = a[i]
  for j in range(N):
    tableud[i][j] = abs(a[j][0] - x) * a[j][2]
    tablelr[i][j] = abs(a[j][1] - y) * a[j][2]
tableudi = [[10 ** 20] * N for _ in range(1 << N)]
tablelri = [[10 ** 20] * N for _ in range(1 << N)]
for i in range(1 << N):
  for j in range(N):
    if i & (1 << j):
      for k in range(N):
        tableudi[i][k] = min(tableudi[i][k], tableud[j][k])
        tablelri[i][k] = min(tablelri[i][k], tablelr[j][k])
zero = 0
for t in range(N + 1):
  res = float("inf")
  if zero:
    print(0)
    continue
  for c in combi([x for x in range(N)], t):
    for i in range(1 << t):
      x = 0
      y = 0
      rres = 0
      for j in range(t):
        if i & (1 << j): x |= 1 << c[j]
        else: y |= 1 << c[j]
      for j in range(N):
        rres += min(min(abs(a[j][0]), abs(a[j][1])) * a[j][2], tableudi[x][j], tablelri[y][j])
      res = min(res, rres)
  print(res)
  if res == 0: zero = 1

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import combinations as combi
input = sys.stdin.readline
N = int(input())
a = [tuple(map(int, input().split())) for _ in range(N)]
#print(a)
tableud = [[0] * N for _ in range(N)]
tablelr = [[0] * N for _ in range(N)]
for i in range(N):
  x, y, _ = a[i]
  for j in range(N):
    tableud[i][j] = abs(a[j][0] - x) * a[j][2]
    tablelr[i][j] = abs(a[j][1] - y) * a[j][2]
tableudi = [[10 ** 20] * N for _ in range(1 << N)]
tablelri = [[10 ** 20] * N for _ in range(1 << N)]
for i in range(1 << N):
  for j in range(N):
    if i & (1 << j):
      for k in range(N):
        tableudi[i][k] = min(tableudi[i][k], tableud[j][k])
        tablelri[i][k] = min(tablelri[i][k], tablelr[j][k])
zero = 0
for t in range(N + 1):
  res = float("inf")
  if zero:
    print(0)
    continue
  for c in combi([x for x in range(N)], t):
    for i in range(1 << t):
      table = [10 ** 20] * N
      for j in range(N):
        table[j] = min(abs(a[j][0]), abs(a[j][1])) * a[j][2]
      x = 0
      y = 0
      for j in range(t):
        if i & (1 << j): x |= 1 << c[j]
        else: y |= 1 << c[j]
      for j in range(N):
        table[j] = min(table[j], tableudi[x][j], tablelri[y][j])
      res = min(res, sum(table))
  print(res)
  if res == 0: zero = 1

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict as dd
from itertools import combinations as cmb
n = int(input())
X = [0] * n
Y = [0] * n
P = [0] * n
for i in range(n):
    X[i], Y[i], P[i] = map(int, input().split())

def S():
    total = 0
    for idx in range(n):
        x = X[idx]
        y = Y[idx]
        people_num = P[idx]
        min_dis = 10**6
        for train_x in  roads_x:
            min_dis = min(min_dis, abs(train_x - x))
        for train_y in roads_y:
            min_dis = min(min_dis, abs(train_y - y))
        total += min_dis * people_num
    return total


x_cand = sorted(list(set(X)))
y_cand = sorted(list(set(Y)))
cand = [["x", x] for x in x_cand] + [["y", y] for y in y_cand]

for k in range(0, min(len(cand), n+1)):
    min_s = float("inf")
    for pat in cmb(cand, k):
        roads_x = [0]
        roads_y = [0]
        for x_or_y, val in pat:
            if x_or_y == "x":
                roads_x.append(val)
            else:
                roads_y.append(val)
        #print(k, pat, S())
        min_s = min(min_s, S())
    print(min_s)

for i in range(n - k):
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000000)

n = int(input())
x = [0]*n
y = [0]*n
p = [0]*n
for i in range(n): x[i],y[i],p[i] = map(int,input().split())

distx = []
disty = []
for i in range(1<<n):
    dx = [abs(j) for j in x]
    dy = [abs(j) for j in y]
    for j in range(n):
        for k in range(n):
            if i >> k & 1:
                dx[j] = min(dx[j],abs(x[j]-x[k]))
                dy[j] = min(dy[j],abs(y[j]-y[k]))
    distx.append(dx)
    disty.append(dy) 

INF = float("INF")
ans = [INF]*(n+1)
def f(s):
    if len(s) == n:
        tar = n-s.count("0")
        ix = s.replace("x","1")
        ix = int(ix.replace("y","0"),2)
        iy = s.replace("y","1")
        iy = int(iy.replace("x","0"),2)
        res = 0
        for i in range(n): res += min(distx[ix][i],disty[iy][i])*p[i]
        ans[tar] = min(ans[tar],res)
        return
    for c in ("x","y","0"): f(s+c)
    
f("")
for i in range(n+1): print(ans[i])