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:
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 = int("".join(str(i) for i in bitx[::-1]), 2)
    # 1が出てくるbitのみを抽出
    bity = [1 if p == 1 else 0 for p in pattern]
    paty = int("".join(str(i) for i in bity[::-1]), 2)

    # 各点において、最小の移動距離をコスト計上
    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 = int("".join(str(i) for i in bitx[::-1]), 2)
    # 1が出てくるbitのみを抽出
    bity = [1 if p == 1 else 0 for p in pattern]
    paty = int("".join(str(i) for i in bity[::-1]), 2)

    # 各点において、最小の移動距離をコスト計上
    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
    
    anss[train_counts] = min(anss[train_counts], cost)

for ans in anss:
    if ans == 10**9:
        print(0)
    else:
        print(ans)

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)


    ans = [INF] * (N+1)
    for i in range(3**N):
        s = copy.copy(r)
        cnt = 0
        for b in range(N):
            if i % 3 == 1:
                cnt += 1
                for j in range(N):
                    s[j] = min(s[j],abs(X[j]-X[b])*P[j])
            elif i % 3 == 2:
                cnt += 1
                for j in range(N):
                    s[j] = min(s[j],abs(Y[j]-Y[b])*P[j])
            i //= 3
        ans[cnt] = min(ans[cnt],sum(s))

    t = ans[0]
    for i in range(N+1):
        t = min(t,ans[i])
        print(t)


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)


    ans = [INF] * (N+1)
    for i in range(3**N):
        s = copy.copy(r)
        cnt = 0
        for b in range(N):
            if i % 3 == 1:
                cnt += 1
                for j in range(N):
                    s[j] = min(s[j],abs(X[j]-X[b])*P[j])
            elif i % 3 == 2:
                cnt += 1
                for j in range(N):
                    s[j] = min(s[j],abs(Y[j]-Y[b])*P[j])
            i //= 3
        ans[cnt] = min(ans[cnt],sum(s))


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



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
 
def main():
    n = int(input())
    li = []
    for _ in range(n):
        xi, yi, pi = map(int, input().split())
        li.append([xi, yi, pi])
    li.sort(key=lambda x: x[2],reverse=True)
    xx=[[abs(li[j][0]) for j in range(n)] for i in range(2**n)]
    yy=[[abs(li[j][1]) for j in range(n)] for i in range(2**n)]
 
    for i in range(2**n):
        X=[0]
        rest=[]
        for j in range(n):
            if i>>j&1==1:
                X.append(li[j][0])
                xx[i][j]=0
            else:
                rest.append(j)
        for j in rest:
            res=10**15
            for k in X:
                res=min(res,abs(li[j][0]-k))
            xx[i][j]=res
    
    for i in range(2**n):
        Y=[0]
        rest=[]
        for j in range(n):
            if i>>j&1==1:
                Y.append(li[j][1])
                yy[i][j]=0
            else:
                rest.append(j)
        for j in rest:
            res=10**15
            for k in Y:
                res=min(res,abs(li[j][1]-k))
            yy[i][j]=res
 
    index_list=[2**q for q in range(n)]
    klist = [10**15]*(n+1)
    klist[n]=0

    if n<15:hani=3**n
    else:hani=2*3**(n-1)
    
    for xyr2 in range(hani):
        xyr = xyr2
        k,xopen,yopen=0,0,0
        for index in range(n):            
            num = xyr%3
            k+= (num+1)//2
            xopen += (num//2)*index_list[index]
            yopen += (num%2)*index_list[index]
            xyr//=3 
 
        cost = 0
        for w in range(n):
            cost += min(xx[xopen][w], yy[yopen][w])*li[w][2]
 
        klist[k] = min(klist[k], cost)
 
    for g in range(n+1):
        print(klist[g])
 
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
 
def main():
    n = int(input())
    li = []
    for _ in range(n):
        xi, yi, pi = map(int, input().split())
        li.append([xi, yi, pi])
    li.sort(key=lambda x: x[2],reverse=True)
    xx=[[abs(li[j][0]) for j in range(n)] for i in range(2**n)]
    yy=[[abs(li[j][1]) for j in range(n)] for i in range(2**n)]
 
    for i in range(2**n):
        X=[0]
        rest=[]
        for j in range(n):
            if i>>j&1==1:
                X.append(li[j][0])
                xx[i][j]=0
            else:
                rest.append(j)
        for j in rest:
            res=10**15
            for k in X:
                res=min(res,abs(li[j][0]-k))
            xx[i][j]=res
    
    for i in range(2**n):
        Y=[0]
        rest=[]
        for j in range(n):
            if i>>j&1==1:
                Y.append(li[j][1])
                yy[i][j]=0
            else:
                rest.append(j)
        for j in rest:
            res=10**15
            for k in Y:
                res=min(res,abs(li[j][1]-k))
            yy[i][j]=res
 
    index_list=[2**q for q in range(n)]
    klist = [10**15]*(n+1)
    klist[n]=0

    if n<15:hani=3**n
    else:hani=2*3**(n-1)
    
    for xyr2 in range(hani):
        xyr = xyr2
        k,xopen,yopen=0,0,0
        for index in range(n):            
            num = xyr%3
            k+= (num+1)//2
            xopen += (num%2)*index_list[index]
            yopen += (num//2)*index_list[index]
            xyr//=3 
 
        cost = 0
        for w in range(n):
            cost += min(xx[xopen][w], yy[yopen][w])*li[w][2]
 
        klist[k] = min(klist[k], cost)
 
    for g in range(n+1):
        print(klist[g])
 
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

ctr_x = [[0] * N for _ in range(2 ** N)]
ctr_y = [[0] * N for _ in range(2 ** N)]
for bit in range(2 ** N):
    for i in range(N):
        tmp_x = abs(X[i][0]) * X[i][2]
        tmp_y = abs(X[i][1]) * X[i][2]
        for j in range(N):
            if bit >> j & 1:
                tmp_x = min(tmp_x, abs(X[i][0] - X[j][0]) * X[i][2])
                tmp_y = min(tmp_y, abs(X[i][1] - X[j][1]) * X[i][2])

        ctr_x[bit][i] = tmp_x
        ctr_y[bit][i] = tmp_y

ans = [10 ** 9] * (N + 1)
for bit in range(2 ** N):
    cnt = bin(bit).count("1")
    for i in reversed(range(bit + 1)):
        i &= bit
        cost = 0
        for j in range(N):
            if not bit >> j & 1:
                cost += min(ctr_x[i][j], ctr_y[bit - i][j])
        ans[cnt] = min(ans[cnt], cost)

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

Submission status: Wrong Answer

Solution code:

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

ctr_x = [[0] * N for _ in range(2 ** N)]
ctr_y = [[0] * N for _ in range(2 ** N)]
for bit in range(2 ** N):
    for i in range(N):
        tmp_x = abs(X[i][0]) * X[i][2]
        tmp_y = abs(X[i][1]) * X[i][2]
        for j in range(N):
            if bit >> j & 1:
                tmp_x = min(tmp_x, abs(X[i][0] - X[j][0]) * X[i][2])
                tmp_y = min(tmp_y, abs(X[i][1] - X[j][1]) * X[i][2])

        ctr_x[bit][i] = tmp_x
        ctr_y[bit][i] = tmp_y

ans = [10 ** 9] * (N + 1)
for bit_x in range(2 ** N):
    for bit_y in range(2 ** N):
        n = bin(bit_x).count("1") + bin(bit_y).count("1")
        if n > N:
            continue

        cnt = 0
        for i in range(N):
            cnt += min(ctr_x[bit_x][i], ctr_y[bit_y][i])
        ans[n] = min(ans[n], cnt)

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

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

dists = [[[0, 0] for _ in range(n)] for _ in range(n)]
for i, (x, y, p) in enumerate(xyp):
    for j, (xx, yy, pp) in enumerate(xyp):
        dists[i][j][0] = abs(xx - x) * p
        dists[i][j][1] = abs(yy - y) * p

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])
    cost = 0
    # 各点において、最小の移動距離をコスト計上
    for i, (x, y, p) in enumerate(xyp):
        if pattern[i] <= 1:
            # patternうち0か1なら路線上に集落があるのでコストは0
            continue
        d = [abs(x)*p, abs(y)*p]
        for j, pp in enumerate(pattern):
            if i == j or pp == 2:
                # 相手は路線上に存在する必要がある
                continue
            d[pp] = min(d[pp], dists[i][j][pp])
        cost += min(d)
    
    anss[train_counts] = min(anss[train_counts], cost)

for ans in anss:
    if ans == 10**9:
        print(0)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import random
def input(): return sys.stdin.readline().rstrip()


def main():
    n = int(input())
    li = []
    for _ in range(n):
        xi, yi, pi = map(int, input().split())
        li.append([xi, yi, pi])
    li.sort(key=lambda x: x[2], reverse=True)
    xx = [[abs(li[j][0]) for j in range(n)] for i in range(2**n)]
    yy = [[abs(li[j][1]) for j in range(n)] for i in range(2**n)]

    for i in range(2**n):
        X = [0]
        rest = []
        for j in range(n):
            if i >> j & 1 == 1:
                X.append(li[j][0])
                xx[i][j] = 0
            else:
                rest.append(j)
        for j in rest:
            res = 10**15
            for k in X:
                res = min(res, abs(li[j][0]-k))
            xx[i][j] = res

    for i in range(2**n):
        Y = [0]
        rest = []
        for j in range(n):
            if i >> j & 1 == 1:
                Y.append(li[j][1])
                yy[i][j] = 0
            else:
                rest.append(j)
        for j in rest:
            res = 10**15
            for k in Y:
                res = min(res, abs(li[j][1]-k))
            yy[i][j] = res

    index_list = [2**q for q in range(n)]
    klist = [10**15]*(n+1)
    klist[n] = 0

    if n < 15:
        for xyr2 in range(3**n):
            xyr = xyr2
            k, xopen, yopen = 0, 0, 0
            for index in range(n):
                num = xyr % 3
                k += (num+1)//2
                xopen += (num % 2)*index_list[index]
                yopen += (num//2)*index_list[index]
                xyr //= 3

            cost = 0
            for w in range(n):
                cost += min(xx[xopen][w], yy[yopen][w])*li[w][2]

            klist[k] = min(klist[k], cost)
    else:
        cntlist = [1, 90, 500, 1000, 10000, 50000, 50000, 50000,
                   50000, 50000, 50000, 50000, 50000, 50000, 50000]
        for nload in range(n):
            for _ in range(cntlist[nload]):
                if klist[nload] == 0:
                    break
                xopen, yopen = 0, 0
                a = random.sample(range(2*n), k=nload)
                for loa in a:
                    if loa < n:
                        xopen += index_list[loa]
                    else:
                        yopen += index_list[loa-n]
                cost = 0
                for w in range(n):
                    cost += min(xx[xopen][w], yy[yopen][w])*li[w][2]

                klist[nload] = min(klist[nload], cost)

    for g in range(n+1):
        print(klist[g])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import product
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    li = []
    for _ in range(n):
        xi, yi, pi = map(int, input().split())
        li.append([xi, yi, pi])
    li.sort(key=lambda x: x[2],reverse=True)
    xx=[[abs(li[j][0]) for j in range(n)] for i in range(2**n)]
    yy=[[abs(li[j][1]) for j in range(n)] for i in range(2**n)]
 
    for i in range(2**n):
        X=[0]
        rest=[]
        for j in range(n):
            if i>>j&1==1:
                X.append(li[j][0])
                xx[i][j]=0
            else:
                rest.append(j)
        for j in rest:
            res=10**15
            for k in X:
                res=min(res,abs(li[j][0]-k))
            xx[i][j]=res
    
    for i in range(2**n):
        Y=[0]
        rest=[]
        for j in range(n):
            if i>>j&1==1:
                Y.append(li[j][1])
                yy[i][j]=0
            else:
                rest.append(j)
        for j in rest:
            res=10**15
            for k in Y:
                res=min(res,abs(li[j][1]-k))
            yy[i][j]=res

    index_list=[2**q for q in range(n)]
    klist = [10**15]*(n+1)
    klist[n]=0 
    
    for xyr in product([0, 1, 2], repeat=n-1):
        k = 0
        for num in xyr:
            k += (num+1)//2
        if klist[k] == 0:
            continue
        xopen = 0
        yopen = 0
        for index, num in enumerate(xyr):
            xopen += (num%2)*index_list[index]
            yopen += (num//2)*index_list[index]        
        cost = 0
        for w in range(n):
            cost += min(xx[xopen][w], yy[yopen][w])*li[w][2]

        klist[k] = min(klist[k], cost)

    for g in range(n+1):
        print(klist[g])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

def solve(n, XYP):
    def convert(x, n=2, k=0):
        A = deque()
        while x:
            x, r = divmod(x, n)
            A.appendleft(r)
        return ''.join(map(str, A)).zfill(k)
    
    def preprocess(n, XYP):
        d_x = {}
        d_y = {}
        for i in range(2**n):
            X, Y = [0], [0]
            for j, (x, y, p) in enumerate(XYP):
                if (i>>j) & 1:
                    X.append(x)
                    Y.append(y)
            Z, W = [], []
            for x, y, p in XYP:
                Z.append(min(abs(x-x_) for x_ in X) * p)
                W.append(min(abs(y-y_) for y_ in Y) * p) 
            d_x[str(X)] = Z
            d_y[str(Y)] = W
        return d_x, d_y
    d_x, d_y = preprocess(n, XYP)
    
    A = defaultdict(lambda:1<<64)
    for i in range(3**n):
        S = list(bin(i))
        c = 0
        X, Y = [0], [0]
#         for s, (x, y, p) in zip(S, XYP):
#             if s == 1:
#                 X.append(x)
#                 c += 1
#             elif s == 2:
#                 Y.append(y)
#                 c += 1
#         s = sum(min(d_x[str(X)][j], d_y[str(Y)][j]) for j in range(n))
#         if A[c] > s:
#             A[c] = s
#     for i in range(n+1):
#         print(A[i])

n, *XYP = map(int, open(0).read().split())
XYP = list(zip(XYP[::3], XYP[1::3], XYP[2::3]))
solve(n, XYP)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

def solve(n, XYP):
    def convert(x, n=2, k=0):
        A = deque()
        while x:
            x, r = divmod(x, n)
            A.appendleft(r)
        return ''.join(map(str, A)).zfill(k)
    
    def preprocess(n, XYP):
        d_x = {}
        d_y = {}
        for i in range(2**n):
            X, Y = [0], [0]
            for j, (x, y, p) in enumerate(XYP):
                if (i>>j) & 1:
                    X.append(x)
                    Y.append(y)
            Z, W = [], []
            for x, y, p in XYP:
                Z.append(min(abs(x-x_) for x_ in X) * p)
                W.append(min(abs(y-y_) for y_ in Y) * p) 
            d_x[str(X)] = Z
            d_y[str(Y)] = W
        return d_x, d_y
    d_x, d_y = preprocess(n, XYP)
    
    A = defaultdict(lambda:1<<64)
    for i in range(3**n):
        S = list(map(int, convert(i, 3, n)))
        c = 0
        X, Y = [0], [0]
#         for s, (x, y, p) in zip(S, XYP):
#             if s == 1:
#                 X.append(x)
#                 c += 1
#             elif s == 2:
#                 Y.append(y)
#                 c += 1
#         s = sum(min(d_x[str(X)][j], d_y[str(Y)][j]) for j in range(n))
#         if A[c] > s:
#             A[c] = s
#     for i in range(n+1):
#         print(A[i])

n, *XYP = map(int, open(0).read().split())
XYP = list(zip(XYP[::3], XYP[1::3], XYP[2::3]))
solve(n, XYP)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

def solve(n, XYP):
    def convert(x, n=2, k=0):
        A = deque()
        while x:
            x, r = divmod(x, n)
            A.appendleft(r)
        return ''.join(map(str, A)).zfill(k)
    
    def preprocess(n, XYP):
        d_x = {}
        d_y = {}
        for i in range(2**n):
            X, Y = [0], [0]
            for j, (x, y, p) in enumerate(XYP):
                if (i>>j) & 1:
                    X.append(x)
                    Y.append(y)
            Z, W = [], []
            for x, y, p in XYP:
                Z.append(min(abs(x-x_) for x_ in X) * p)
                W.append(min(abs(y-y_) for y_ in Y) * p) 
            d_x[str(X)] = Z
            d_y[str(Y)] = W
        return d_x, d_y
    d_x, d_y = preprocess(n, XYP)
    
#     A = defaultdict(lambda:1<<64)
#     for i in range(3**n):
#         S = list(map(int, convert(i, 3, n)))
#         c = 0
#         X, Y = [0], [0]
#         for s, (x, y, p) in zip(S, XYP):
#             if s == 1:
#                 X.append(x)
#                 c += 1
#             elif s == 2:
#                 Y.append(y)
#                 c += 1
#         s = sum(min(d_x[str(X)][j], d_y[str(Y)][j]) for j in range(n))
#         if A[c] > s:
#             A[c] = s
#     for i in range(n+1):
#         print(A[i])

n, *XYP = map(int, open(0).read().split())
XYP = list(zip(XYP[::3], XYP[1::3], XYP[2::3]))
solve(n, XYP)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *XYP = map(int, read().split())
    X = XYP[::3]
    Y = XYP[1::3]
    P = XYP[2::3]

    dist_x = [[abs(x) for x in X] for _ in range(1 << N)]
    dist_y = [[abs(y) for y in Y] for _ in range(1 << N)]
    for i in range(1 << N):
        for j in range(N):
            if not i & (1 << j):
                continue
            for k, (x, y) in enumerate(zip(X, Y)):
                if dist_x[i][k] > abs(x - X[j]):
                    dist_x[i][k] = abs(x - X[j])
                if dist_y[i][k] > abs(y - Y[j]):
                    dist_y[i][k] = abs(y - Y[j])

    cost = [INF] * (N + 1)

    # def dfs(n, k, rail_x, rail_y):
    #     if n == N:
    #         ans = 0
    #         for i, p in enumerate(P):
    #             ans += min(dist_x[rail_x][i], dist_y[rail_y][i]) * p
    #         if cost[k] > ans:
    #             cost[k] = ans
    #         return

    #     dfs(n + 1, k, rail_x, rail_y)
    #     dfs(n + 1, k + 1, rail_x + (1 << n), rail_y)
    #     dfs(n + 1, k + 1, rail_x, rail_y + (1 << n))
    #     return

    # dfs(0, 0, 0, 0)

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

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
inf = 10**15


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    X = []
    Y = []
    P = []
    for _ in range(N):
        x, y, p = map(int, input().split())
        X.append(x)
        Y.append(y)
        P.append(p)

    dx = [[inf] * N for _ in range(1<<N)]
    dy = [[inf] * N for _ in range(1<<N)]
    for i in range(1 << N):
        Xline = [0]
        Yline = [0]
        for j in range(N):
            if i>>j&1:
                Xline.append(X[j])
                Yline.append(Y[j])
        for j in range(N):
            for x, y in zip(Xline, Yline):
                dx[i][j] = min(dx[i][j], abs(x - X[j]))
                dy[i][j] = min(dy[i][j], abs(y - Y[j]))

    ans = [inf] * (N+1)
    beki = [1<<j for j in range(N)]
    for i in range(3**(N-1)):
        M = i
        k = 0
        ix = iy = 0
        J = []
        for j in range(N-1):
            M, m = divmod(M, 3)
            if m == 0:
                J.append(j)
            elif m == 1:
                k += 1
                ix += beki[j]
            else:
                k += 1
                iy += beki[j]
        for x in range(3):
            kk = k
            if x == 0:
                J.append(N-1)
            elif x == 1:
                kk += 1
                ix += beki[N-1]
            else:
                kk += 1
                iy += beki[N-1]
            cost = 0
            for j in J:
                p = P[j]
                cost += min(dx[ix][j], dy[iy][j]) * p
            ans[kk] = min(ans[kk], cost)
    for c in ans:
        print(c)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import product
def input(): return sys.stdin.readline().rstrip()


def kyori(dlist, ds):
    return min(map(lambda m: abs(m-ds),dlist))

def main():
    n = int(input())
    li = []
    for _ in range(n):
        xi, yi, pi = map(int, input().split())
        li.append([xi, yi, pi])
    xdist = [[0]*(n) for i in range(2**n)]
    for i in range(2 ** n):
        xlist = [0]
        for j in range(n):
            if ((i >> j) & 1):
                xlist.append(li[j][0])
        for h in range(n):
            xdist[i][h] = kyori(xlist, li[h][0])
    ydist = [[0]*(n) for i in range(2**n)]
    for i in range(2 ** n):
        ylist = [0]
        for j in range(n):
            if ((i >> j) & 1):
                ylist.append(li[j][0])
        for h in range(n):
            ydist[i][h] = kyori(ylist, li[h][1])

    klist = [10**10]*(n+1)
    for xyr in list(product([0, 1, 2], repeat=n)):
        xopen = 0
        yopen = 0
        k = 0
        for index, num in enumerate(xyr):
            xopen += num*(2-num)*2**index
            yopen += (num*(num-1)*2**index)//2
            k += ((num-1)*(2-num)+2)//2
        if klist[k] == 0:
            continue
        cost = 0
        for w in range(n):
            cost += min(xdist[xopen][w], ydist[yopen][w])*li[w][2]

        klist[k] = min(klist[k], cost)

    for g in range(n+1):
        print(klist[g])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import product
def input(): return sys.stdin.readline().rstrip()


def kyori(dlist, ds):
    return min(map(lambda m: abs(m-ds),dlist))

def main():
    n = int(input())
    li = []
    for _ in range(n):
        xi, yi, pi = map(int, input().split())
        li.append([xi, yi, pi])
    xdist = [[0]*(2**n) for i in range(n)]
    for i in range(2 ** n):
        xlist = [0]
        for j in range(n):
            if ((i >> j) & 1):
                xlist.append(li[j][0])
        for h in range(n):
            xdist[h][i] = kyori(xlist, li[h][0])
    ydist = [[0]*(2**n) for i in range(n)]
    for i in range(2 ** n):
        ylist = [0]
        for j in range(n):
            if ((i >> j) & 1):
                ylist.append(li[j][0])
        for h in range(n):
            ydist[h][i] = kyori(ylist, li[h][1])

    klist = [10**10]*(n+1)
    for xyr in list(product([0, 1, 2], repeat=n)):
        xopen = 0
        yopen = 0
        k = 0
        for index, num in enumerate(xyr):
            xopen += num*(2-num)*2**index
            yopen += (num*(num-1)*2**index)//2
            k += ((num-1)*(2-num)+2)//2
        if klist[k] == 0:
            continue
        cost = 0
        for w in range(n):
            cost += min(xdist[w][xopen], ydist[w][yopen])*li[w][2]

        klist[k] = min(klist[k], cost)

    for g in range(n+1):
        print(klist[g])


if __name__ == '__main__':
    main()