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: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

INF = float('inf')

N = int(input().strip())
data = [list(map(int, input().split())) for _ in range(N)]
cost_b = []
cost_D = []
for b in range(1<<N):
  towns = []
  cost = INF
  for i in range(N):
    if (b>>i)&1:
      towns.append(data[i])
  for i in range(len(towns)):
    X = towns[i][0]
    cost = min(cost,sum(abs(towns[j][0]-X)*towns[j][2] for j in range(len(towns))))
  for i in range(len(towns)):
    Y = towns[i][1]
    cost = min(cost,sum(abs(towns[j][1]-Y)*towns[j][2] for j in range(len(towns))))
  if len(towns) == 0:
    cost = 0
  cost_b.append(cost)
  cost_D.append(sum(min(abs(towns[j][0]),abs(towns[j][1]))*towns[j][2] for j in range(len(towns))))

dp = [[INF]*(N+1) for i in range(1<<N)]

for i in range(1<<N):
  dp[i][0] = cost_D[i]
count = 0
for k in range(1,N+1):
  for i in range(1<<N)[::-1]:
    x = i
    while x>=0:
      x &= i
      new = dp[i-x][k-1]+cost_b[x]
      #if dp[i][k] > new:
      #  dp[i][k] = new
      x -= 1
for i in range(N+1):
  print(dp[-1][i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

INF = float('inf')

N = int(input().strip())
data = [list(map(int, input().split())) for _ in range(N)]
cost_b = []
cost_D = []
for b in range(1<<N):
  towns = []
  cost = INF
  for i in range(N):
    if (b>>i)&1:
      towns.append(data[i])
  for i in range(len(towns)):
    X = towns[i][0]
    cost = min(cost,sum(abs(towns[j][0]-X)*towns[j][2] for j in range(len(towns))))
  for i in range(len(towns)):
    Y = towns[i][0]
    cost = min(cost,sum(abs(towns[j][1]-Y)*towns[j][2] for j in range(len(towns))))
  if len(towns) == 0:
    cost = 0
  cost_b.append(cost)
  cost_D.append(sum(min(abs(towns[j][0]),abs(towns[j][1]))*towns[j][2] for j in range(len(towns))))

dp = [[INF]*(N+1) for i in range(1<<N)]

for i in range(1<<N):
  dp[i][0] = cost_D[i]
count = 0
for k in range(1,N+1):
  for i in range(1<<N)[::-1]:
    x = i
    while x>=0:
      x &= i
      new = dp[i-x][k-1]+cost_b[x]
      if dp[i][k] > new:
        dp[i][k] = new
      x -= 1
for i in range(N+1):
  print(dp[-1][i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

INF = float('inf')

N = int(input().strip())
data = [list(map(int, input().split())) for _ in range(N)]
cost_b = []
cost_D = []
for b in range(1<<N):
  towns = []
  cost = INF
  for i in range(N):
    if (b>>i)&1:
      towns.append(data[i])
  for i in range(len(towns)):
    X = towns[i][0]
    cost = min(cost,sum(abs(towns[j][0]-X)*towns[j][2] for j in range(len(towns))))
  for i in range(len(towns)):
    Y = towns[i][0]
    cost = min(cost,sum(abs(towns[j][1]-Y)*towns[j][2] for j in range(len(towns))))
  if len(towns) == 0:
    cost = 0
  cost_b.append(cost)
  cost_D.append(sum(min(abs(towns[j][0]),abs(towns[j][1]))*towns[j][2] for j in range(len(towns))))

dp = [[INF]*(N+2) for i in range(1<<N)]

for i in range(1<<N):
  dp[i][0] = cost_D[i]
count = 0
for k in range(1,N+1):
  for i in range(1<<N)[::-1]:
    x = i
    while x>=0:
      x &= i
      new = dp[i-x][k-1]+cost_b[x]
      if dp[i][k] > new:
        dp[i][k] = new
      x -= 1
for i in range(N+1):
  print(dp[-1][i])

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def S(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

alphabets = "abcdefghijklmnopqrstuvwxyz"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    N = I()
    XYP = LIR(N)

    anss = [MOD] * (N+1)

    for pattern in itertools.product([0, 1, 2], repeat=N):
        X = [0]
        Y = [0]
        root = 0
        for i, (x, y, p) in enumerate(XYP):
            if pattern[i] == 1:
                X.append(x)
            elif pattern[i] == 2:
                Y.append(y)
            if pattern[i] != 0:
                root += 1

        tmp_ans = 0
        for x, y, p in XYP:
            min_dist = MOD
            for rx in X:
                min_dist = min(min_dist, abs(x - rx))
            for ry in Y:
                min_dist = min(min_dist, abs(y - ry))
            tmp_ans += min_dist * p

        anss[root] = min(anss[root], tmp_ans)

    for ans in anss:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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: Wrong Answer

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)

Submission status: Wrong Answer

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[0])
#print(dY[0])
#歩行距離を求める
ans = [float('inf') for K in range(N+1)]
for i in range(3**N):
    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)
    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: Wrong Answer

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

def main():
    INF = 10**8
    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 = []
    for a,b,c in zip(X,Y,P):
        r.append(min(abs(a),abs(b))*c)


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

    for i in range(2**N):
        hor[i] = copy.copy(r)
        ver[i] = copy.copy(r)
        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 = [INF] * (N+1)
    for i in range(3**N):
        cnt = 0
        bx = 0
        by = 0
        bit = 1
        for b in range(N):
            if i % 3 == 1:
                bx += bit
                cnt += 1
            elif i % 3 == 2:
                by += bit
                cnt += 1
            bit *= 2
            i //= 3
        s = 0
        for b in range(N):
            s += min(hor[bx][b],ver[by][b])
        ans[cnt] = min(ans[cnt],s)

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    INF = 10**8
    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 = []
    for a,b,c in zip(X,Y,P):
        r.append(min(abs(a),abs(b))*c)


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

    for i in range(2**N):
        hor[i] = copy.copy(r)
        ver[i] = copy.copy(r)
        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 = [INF] * (N+1)
    for i in range(3**N):
        cnt = 0
        bx = 0
        by = 0
        for b in range(N):
            bx *= 2
            by *= 2
            if i % 3 == 1:
                bx += 1
                cnt += 1
            elif i % 3 == 2:
                by += 1
                cnt += 1

            i //= 3
        s = 0
        for b in range(N):
            s += min(hor[bx][b],ver[by][b])
        ans[cnt] = min(ans[cnt],s)

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())

    towns = []
    xs = []
    ys = []
    for i in range(n):
        t = tuple(map(int, input().split()))
        towns.append(t)
        xs.append(t[0])
        ys.append(t[1])

    memoX = [dict() for i in range(n)]
    memoY = [dict() for i in range(n)]

    def calculate_cost(railX, railY):
        sumCost = 0
        index = 0
        xKey = tuple(sorted(set(railX)))
        yKey = tuple(sorted(set(railY)))
        for x, y, p in towns:

            memo_x = memoX[index]
            if xKey in memo_x:
                minX = memo_x[xKey]
            else:
                minX = min([abs(x - X) for X in railX])
                memo_x[xKey] = minX

            memo_y = memoY[index]
            if yKey in memo_y:
                minY = memo_y[yKey]
            else:
                minY = min([abs(y - Y) for Y in railY])
                memo_y[yKey] = minY
            cost = min(minX, minY)
            sumCost += (cost * p)
            index += 1
        return sumCost

    import itertools

    prev = 1
    for k in range(n + 1):
        if prev == 0:
            print(0)
            continue

        combi = list(itertools.combinations(towns, k))

        ans = 10 ** 13

        for c in combi:
            candidate = []
            for value in range(2 ** k):
                railX = [0]
                railY = [0]

                for i in range(k):
                    if value >> i == 0:
                        railX.append(c[i][0])
                    else:
                        railY.append(c[i][1])

                result = calculate_cost(railX, railY)

                ans = min(ans, result)

        print(ans)
        prev = ans


main()

Submission status: Wrong Answer

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 = [0]*(1<<n)
tatemi = [0]*(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] += pp*p[j]
        tatemi[i] += 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

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

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

Submission status: Wrong Answer

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 = [0]*(1<<n)
tatemi = [0]*(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] += pp*p[j]
        tatemi[i] += 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

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

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

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
import sys

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(N + 1):
    print(ans[i])

sys.exit()

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: Wrong Answer

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

        if i % 100000 == 0:
            print(i)

        pt = i % (3**(n + 1)) // 3**n

        honsuu = 0
        xi = 0
        yi = 0
        # 路線の引き方を確認する
        for n in range(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: Wrong Answer

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 = 10 ** 20
  if zero:
    print(0)
    continue
  for c in combi([x for x in range(N)], t):
    tt = 0
    for i in c: tt |= 1 << i
    i = tt + 0
    while True:
      i = (i - 1) & tt
      x = i + 0
      y = tt ^ i
      rres = 0
      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)
      if i == 0:
        break
  print(res)
  if res == 0: zero = 1

Submission status: Wrong Answer

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: Wrong Answer

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

xyp = []
xlist = []
ylist = []
plist = []
comb_list = set()

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

for i in range(n+1):
    c = combinations(comb_list,i)
    
    min_sum = math.inf
    for j in c:
        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])
        sum = 0
        for m in range(n):
            min = math.inf
            for q in kv_list:
                t = abs(xlist[m] - q)
                if t < min:
                    min = t
            for q in kh_list:
                t = abs(ylist[m] - q)
                if t < min:
                    min = t
            sum += plist[m] * min

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

Submission status: Wrong Answer

Solution code:
n = int(input())
xyp = []
for _ in range(n):
    x, y, p = map(int, input().split())
    xyp.append((x, y, p))

# 初期値は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
    for i, (x, y, p) in enumerate(xyp):
        # 自分の集落に路線がない場合 路線がある集落との比較
        for j, (xx, yy, pp) in enumerate(xyp):
            if (k>>j)&1:
                # 相手の集落に路線あり
                x_dists[k][i] =  min(x_dists[k][i], abs(xx-x))
                y_dists[k][i] =  min(y_dists[k][i], abs(yy-y))

anss = [10**9] * (n+1)
from itertools import product
# 各集落に対するパターンでのコストを計算
for pattern in product([0, 1, 2], repeat=n):
    train_counts = len([p for p in pattern if p<=1])
    # 0が出てくるbitのみを抽出
    bitx = [1 if p == 0 else 0 for p in pattern]
    patx = 0
    for i, b in enumerate(bitx):
        patx += b*(2**i)

    # 1が出てくるbitのみを抽出
    bity = [1 if p == 1 else 0 for p in pattern]
    paty = 0
    for i, b in enumerate(bity):
        paty += b*(2**i)

    # 各点において、最小の移動距離をコスト計上
    cost = 0
    #for i, (x, y, p) in enumerate(xyp):
    #    if pattern[i] <= 1:
    #        # patternうち0か1なら路線上に集落があるのでコストは0
    #        continue
    #    cost += min(x_dists[patx][i], y_dists[paty][i])*p
    #    if cost > anss[train_counts]:
    #        break
    
    anss[train_counts] = min(anss[train_counts], cost)

for ans in anss:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
xyp = []
for _ in range(n):
    x, y, p = map(int, input().split())
    xyp.append((x, y, p))

# 初期値は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
    for i, (x, y, p) in enumerate(xyp):
        # 自分の集落に路線がない場合 路線がある集落との比較
        for j, (xx, yy, pp) in enumerate(xyp):
            if (k>>j)&1:
                # 相手の集落に路線あり
                x_dists[k][i] =  min(x_dists[k][i], abs(xx-x))
                y_dists[k][i] =  min(y_dists[k][i], abs(yy-y))

anss = [10**9] * (n+1)
from itertools import product
# 各集落に対するパターンでのコストを計算
for pattern in product([0, 1, 2], repeat=n):
    train_counts = len([p for p in pattern if p<=1])
    # 0が出てくるbitのみを抽出
    bitx = [1 if p == 0 else 0 for p in pattern]
    patx = 0
    for i, b in enumerate(bitx):
        patx += b*(2**i)

    # 1が出てくるbitのみを抽出
    bity = [1 if p == 1 else 0 for p in pattern]
    paty = 0
    for i, b in enumerate(bity):
        paty += b*(2**i)

    # 各点において、最小の移動距離をコスト計上
    cost = 0
    for i, (x, y, p) in enumerate(xyp):
        if pattern[i] <= 1:
            # patternうち0か1なら路線上に集落があるのでコストは0
            continue
        cost += min(x_dists[patx][i], y_dists[paty][i])*p
        if cost > anss[train_counts]:
            break
    
    anss[train_counts] = min(anss[train_counts], cost)

for ans in anss:
        print(ans)