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:
from itertools import product

N = int(input())
XYP = [tuple(map(int, input().split())) for _ in range(N)]
X = [x for x, _, _ in XYP]
Y = [y for _, y, _ in XYP]
INF = 10**18

def calcCost(AP, A):
    ret = [[INF] * N for _ in range(1 << N)]
    for state in range(1 << N):
        useA = [a for i, a in enumerate(A) if (state & (1 << i)) != 0]
        for i, (a, p) in enumerate(AP):
            cost = abs(a)
            for z in useA:
                cost = min(cost, abs(a - z))
            ret[state][i] = cost * p
    return ret

costX = calcCost([(x, p) for x, _, p in XYP], X)
costY = calcCost([(y, p) for _, y, p in XYP], Y)

def makeState(state, sign):
    ret = 0
    for s in state[::-1]:
        ret <<= 1
        if s == sign:
            ret |= 1
    return ret

ans = [INF] * (N + 1)
for state in product((0, 1, 2), repeat=N):
    # stateX = makeState(state, 1)
    # stateY = makeState(state, 2)

    cost = 0
    # for i in range(N):
    #     cost += min(costX[stateX][i], costY[stateY][i])

    M = N - state.count(0)
    if ans[M] > cost:
        ans[M] = cost

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

Submission status: Wrong Answer

Solution code:
from itertools import product

N = int(input())
XYP = [tuple(map(int, input().split())) for _ in range(N)]
X = [x for x, _, _ in XYP]
Y = [y for _, y, _ in XYP]
INF = 10**18

def calcCost(AP, A):
    ret = [[INF] * N for _ in range(1 << N)]
    for state in range(1 << N):
        useA = [a for i, a in enumerate(A) if (state & (1 << i)) != 0]
        for i, (a, p) in enumerate(AP):
            cost = abs(a)
            for z in useA:
                cost = min(cost, abs(a - z))
            ret[state][i] = cost * p
    return ret

costX = calcCost([(x, p) for x, _, p in XYP], X)
costY = calcCost([(y, p) for _, y, p in XYP], Y)

def makeState(state, sign):
    ret = 0
    for s in state[::-1]:
        ret <<= 1
        if s == sign:
            ret |= 1
    return ret

ans = [INF] * (N + 1)
for state in product((0, 1, 2), repeat=N):
    stateX = makeState(state, 1)
    stateY = makeState(state, 2)

    cost = 0
    # for i in range(N):
    #     cost += min(costX[stateX][i], costY[stateY][i])

    M = N - state.count(0)
    if ans[M] > cost:
        ans[M] = cost

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

Submission status: Wrong Answer

Solution code:
from itertools import product

N = int(input())
XYP = [tuple(map(int, input().split())) for _ in range(N)]
X = [x for x, _, _ in XYP]
Y = [y for _, y, _ in XYP]
INF = 10**18

def calcCost(AP, A):
    ret = [[INF] * N for _ in range(1 << N)]
    for state in range(1 << N):
        useA = [a for i, a in enumerate(A) if (state & (1 << i)) != 0]
        for i, (a, p) in enumerate(AP):
            cost = abs(a)
            for z in useA:
                cost = min(cost, abs(a - z))
            ret[state][i] = cost * p
    return ret

costX = calcCost([(x, p) for x, _, p in XYP], X)
costY = calcCost([(y, p) for _, y, p in XYP], Y)

Submission status: Wrong Answer

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7

def main():
    n = int(ipt())
    xs = []
    ys = []
    ps = []
    for i in range(n):
        x,y,p = map(int,ipt().split())
        xs.append(x)
        ys.append(y)
        ps.append(p)
    mix = [abs(i) for i in xs]
    miy = [abs(i) for i in ys]
    mi2x = []
    mi2y = []
    for i in range(2**n):
        mi = mix*1
        for j,xj in enumerate(xs):
            if (i>>j)&1:
                for k,xk in enumerate(xs):
                    ax = abs(xj-xk)
                    if mi[k] > ax:
                        mi[k] = ax
        mi = [mi[i]*ps[i] for i in range(n)]
        mi2x.append(mi)
    for i in range(2**n):
        mi = miy*1
        for j,yj in enumerate(ys):
            if (i>>j)&1:
                for k,yk in enumerate(ys):
                    ay = abs(yj-yk)
                    if mi[k] > ay:
                        mi[k] = ay
        mi = [mi[i]*ps[i] for i in range(n)]
        mi2y.append(mi)
'''
    mi = [10**18]*(n+1)
    for i in range(3**n):
        xi = 0
        yi = 0
        ii = i
        t = n
        for j in range(n):
            if ii%3 == 1:
                xi += 2**j
            elif ii%3 == 2:
                yi += 2**j
            else:
                t -= 1
            ii //= 3
        nm = 0
        for xii,yii in zip(mi2x[xi],mi2y[yi]):
            if xii > yii:
                nm += yii
            else:
                nm += xii
        if mi[t] > nm:
            mi[t] = nm

    for i in mi:
        print(i)
'''

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import product

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

xx = [[abs(xyp[i][0]) for i in range(N)] for _ in range(2 ** N)]
yy = [[abs(xyp[i][1]) for i in range(N)] for _ in range(2 ** N)]

for bits in range(2 ** N):
    for j in range(N):
        if bits >> j & 1 == 1:
            for i in range(N):
                xx[bits][i] = min(xx[bits][i], abs(xyp[i][0] - xyp[j][0]))
                yy[bits][i] = min(yy[bits][i], abs(xyp[i][1] - xyp[j][1]))

min_costs = [10 ** 12] * (N + 1)

for bits in product(range(3), repeat=N):
    for i in range(N):
        i = i * i

# for xyz in product(('x', 'y', 'z'), repeat=N):
#     k = sum([z != 'z' for z in xyz])
# 
#     x_bits = tuple(x == 'x' for x in xyz)
#     y_bits = tuple(y == 'y' for y in xyz)
# 
#     cost = 0
#     for i in range(N):
#         cost += min(xx[(x_bits, i)], yy[(y_bits, i)]) * xyp[i][2]
# 
#     min_costs[k] = min(min_costs[k], cost)
# 
# for k in range(N + 1):
#     print(min_costs[k])

Submission status: Wrong Answer

Solution code:
N = int(input())
xyp = [tuple(map(int, input().split(' '))) for _ in range(N)]

xx = [[abs(xyp[i][0]) for i in range(N)] for _ in range(2 ** N)]
yy = [[abs(xyp[i][1]) for i in range(N)] for _ in range(2 ** N)]

for bits in range(2 ** N):
    for j in range(N):
        if bits >> j & 1 == 1:
            for i in range(N):
                xx[bits][i] = min(xx[bits][i], abs(xyp[i][0] - xyp[j][0]))
                yy[bits][i] = min(yy[bits][i], abs(xyp[i][1] - xyp[j][1]))

min_costs = [10 ** 12] * (N + 1)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.buffer.readline

n = int(input())
infi = 10 ** 10
kouho = []
for _ in range(n):
    x, y, p = map(int, input().split())
    kouho.append((x, y, p))

num = 1 << n
x_kouho = [[infi] * (n) for _ in range(num)]
y_kouho = [[infi] * (n) for _ in range(num)]

for i in range(num):
    xres = 0
    yres = 0
    xroad = [0]
    yroad = [0]
    for j in range(n):
        x, y, p = kouho[j]
        if (i >> j) & 1:
            xroad.append(x)
            yroad.append(y)
    for j in range(n):
        x, y, p = kouho[j]
        xtemp = p * abs(y - 0)
        ytemp = p * abs(x - 0)
        for a in xroad:
            xtemp = min(xtemp, p * abs(x - a))
        for b in yroad:
            ytemp = min(ytemp, p * abs(y - b))
        x_kouho[i][j] = xtemp
        y_kouho[i][j] = ytemp


ans = [infi] * (n + 1)

for idx in range(pow(3, n)):
    xnum = 0
    ynum = 0
    cnt = n
    for i in range(n):
        kumi = idx % 3
        if kumi == 1:
            xnum += 1 << i
        elif kumi == 2:
            ynum += 1 << i
        else:
            cnt -= 1
        idx //= 3
    res = 0
    for i in range(n):
        res += min(x_kouho[xnum][i], y_kouho[ynum][i])
    ans[cnt] = min(ans[cnt], res)
print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
from itertools import product
import sys

input = sys.stdin.buffer.readline

n = int(input())
infi = 10 ** 10
kouho = []
for _ in range(n):
    x, y, p = map(int, input().split())
    kouho.append((x, y, p))

num = 1 << n
x_kouho = [[infi] * (n) for _ in range(num)]
y_kouho = [[infi] * (n) for _ in range(num)]

for i in range(num):
    xres = 0
    yres = 0
    xroad = [0]
    yroad = [0]
    for j in range(n):
        x, y, p = kouho[j]
        if (i >> j) & 1:
            xroad.append(x)
            yroad.append(y)
    for j in range(n):
        x, y, p = kouho[j]
        xtemp = p * abs(y - 0)
        ytemp = p * abs(x - 0)
        for a in xroad:
            xtemp = min(xtemp, p * abs(x - a))
        for b in yroad:
            ytemp = min(ytemp, p * abs(y - b))
        x_kouho[i][j] = xtemp
        y_kouho[i][j] = ytemp


kumiawase = list(product([0, 1, 2], repeat=n))

ans = [infi] * (n + 1)

for kumi in kumiawase:
    xnum = 0
    ynum = 0
    cnt = n
    for i in range(n):
        if kumi[i] == 1:
            xnum += 1 << i
        elif kumi[i] == 2:
            ynum += 1 << i
        else:
            cnt -= 1
    res = 0
    for i in range(n):
        res += min(x_kouho[xnum][i], y_kouho[ynum][i])
    ans[cnt] = min(ans[cnt], res)
print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
from itertools import product
import sys

input = sys.stdin.buffer.readline

n = int(input())
infi = 10 ** 10
kouho = []
for _ in range(n):
    x, y, p = map(int, input().split())
    kouho.append((x, y, p))

num = 1 << n
x_kouho = [[infi] * (n) for _ in range(num)]
y_kouho = [[infi] * (n) for _ in range(num)]

for i in range(num):
    xres = 0
    yres = 0
    xroad = [0]
    yroad = [0]
    for j in range(n):
        x, y, p = kouho[j]
        if (i >> j) & 1:
            xroad.append(x)
            yroad.append(y)
    for j in range(n):
        x, y, p = kouho[j]
        xtemp = p * abs(y - 0)
        ytemp = p * abs(x - 0)
        for a in xroad:
            xtemp = min(xtemp, p * abs(x - a))
        for b in yroad:
            ytemp = min(ytemp, p * abs(y - b))
        x_kouho[i][j] = xtemp
        y_kouho[i][j] = ytemp


kumiawase = list(product([0, 1, 2], repeat=n))

ans = [infi] * (n + 1)

for kumi in kumiawase:
    xnum = 0
    ynum = 0
    cnt = n
    for i in range(n):
        if kumi[i] == 1:
            xnum += 1 << i
        elif kumi[i] == 2:
            ynum += 1 << i
        else:
            cnt -= 1
    res = 0
    for i in range(n):
        res += min(x_kouho[xnum][i], y_kouho[ynum][i])
    ans[cnt] = min(ans[cnt], res)
print(len(ans))
print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
from itertools import product

N = int(input())
towns = [tuple(map(int, input().split(' '))) for _ in range(N)]

x_costs = {}
y_costs = {}

for bits in product((False, True), repeat=N):
    x_points = {x for bit, (x, _, _) in zip(bits, towns) if bit}
    y_points = {y for bit, (_, y, _) in zip(bits, towns) if bit}
    x_points.add(0)
    y_points.add(0)

    for index, (x, y, p) in enumerate(towns):
        x_cost = 10 ** 9
        y_cost = 10 ** 9

        for x_point in x_points:
            x_cost = min(x_cost, abs(x - x_point))
        for y_point in y_points:
            y_cost = min(y_cost, abs(y - y_point))

        x_costs[(index, bits)] = x_cost * p
        y_costs[(index, bits)] = y_cost * p

min_costs = [10 ** 9] * (N + 1)
for xyz in product(('x', 'y', 'z'), repeat=N):
    k = sum([z != 'z' for z in xyz])
    if k > N:
        continue

    x_bits = tuple(x == 'x' for x in xyz)
    y_bits = tuple(y == 'y' for y in xyz)

    cost = 0
    for index in range(N):
        cost += min(x_costs[(index, x_bits)], y_costs[(index, y_bits)])

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

for k in range(N + 1):
    print(min_costs[k])

Submission status: Wrong Answer

Solution code:
# WA出そう
INF = 10**18


def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f

def main():
    N = int(input())
    T = [[int(i) for i in input().split()] for j in range(N)]
    X_dist = [[0]*N for _ in range(1 << N)]
    Y_dist = [[0]*N for _ in range(1 << N)]

    # bitという道路群を建設したときの，各集落の評価距離
    for bit in range(1 << N):
        cur_x = [0]
        cur_y = [0]
        for i in range(N):
            if bit & (1 << i):
                cur_x.append(T[i][0])
                cur_y.append(T[i][1])
        for i, (x, y, p) in enumerate(T):
            X_dist[bit][i] = p*min(abs(cx - x) for cx in cur_x)
            Y_dist[bit][i] = p*min(abs(cy - y) for cy in cur_y)

    answer = [INF] * (N+1)

    NUM = 1 << N

    for i in range(NUM):
        cnt = popcount(i)
        cur_d = INF
        sub = i
        while True:
            x_bit = sub
            y_bit = i - x_bit
            tmp_d = 0
            for j in range(N):
                tmp_d += min(X_dist[x_bit][j], Y_dist[y_bit][j])
            cur_d = min(cur_d, tmp_d)
            if sub == i:
                break
            sub = (sub - 1) & i


        answer[cnt] = min(answer[cnt], cur_d)

    for ans in answer:
      print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# WA出そう
INF = 10**18


def main():
    import sys
    input = sys.stdin.readline
    from collections import defaultdict
    N = int(input())
    T = [[int(i) for i in input().split()] for j in range(N)]
    pow_2 = [pow(2, i) for i in range(N+1)]
    X_dist = [[0]*N for _ in range(1 << N)]
    Y_dist = [[0]*N for _ in range(1 << N)]

    # bitという道路群を建設したときの，各集落の評価距離
    for bit in range(1 << N):
        cur_x = [0]
        cur_y = [0]
        for i in range(N):
            if bit & (1 << i):
                cur_x.append(T[i][0])
                cur_y.append(T[i][1])
        for i, (x, y, p) in enumerate(T):
            X_dist[bit][i] = p*min(abs(cx - x) for cx in cur_x)
            Y_dist[bit][i] = p*min(abs(cy - y) for cy in cur_y)

    answer = [INF] * (N+1)
    
    NUM = 3 * 15
    for bit in range(NUM):
        x_bit = 0
        y_bit = 0
        cnt = 0
        
        for i in range(N):
            b = bit % 3
            bit //= 3
            if b == 2:
                # x = a
                x_bit |= (1<< i)
                cnt += 1
            elif b == 1:
                # y = a
                y_bit |= (1 << i)
                cnt += 1
        # 距離の評価
        cur_d = 0
        for i in range(N):
            cur_d += min(X_dist[x_bit][i], Y_dist[y_bit][i])
        answer[cnt] = min(answer[cnt], cur_d)

    for ans in answer:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def hersCode(n, f):
  l = 10001
  r = -10000
  for i in range(N):
    if (1 << i) & n:
      x, y, p = XYP[i]
      if f:
        l = min(l, x)
        r = max(r, x)
      else:
        l = min(l, y)
        r = max(r, y)
  r += 1

  R = {}
  phi = (1 + 5 ** (1 / 2)) / 2
  while abs(l - r) > 2:
    d1 = int((l * phi + r) // (1 + phi))
    d2 = int((l + r * phi) // (1 + phi))
    if d1 in R:
      dd1 = R[d1]
    else:
      dd1 = exe(n, f, d1)
      R[d1] = dd1
    if d2 in R:
      dd2 = R[d2]
    else:
      dd2 = exe(n, f, d2)
      R[d2] = dd2
    if dd1 > dd2:
      r = d2
    else:
      l = d1
  ans = 0
  for i in range(l, r):
    if i in R:
      ans = max(ans, R[i])
    else:
      ans = max(ans, exe(n, f, i))
  return ans

def exe(n, f, m):
  q = 0
  for i in range(N):
    if (1 << i) & n:
      x, y, p = XYP[i]
      if f == True:
        if min(abs(x), abs(y)) > abs(x - m):
          q += L[i] - abs(x - m) * p
      else:
        if min(abs(x), abs(y)) > abs(y - m):
          q += L[i] - abs(y - m) * p
  return q

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

if N == 1:
  x, y, p = XYP[0]
  print(min(abs(x), abs(y)) * p)
  print(0)
  exit()

L = [0] * N
c = 0
t = 0
for x, y, p in XYP:
  t += min(abs(x), abs(y)) * p
  L[c] = min(abs(x), abs(y)) * p
  c += 1

D = [0] * (1 << N)

for i in range(1, 1 << N):
  D[i] = max(hersCode(i, True), hersCode(i, False))

DP = [[0] * (1 << N) for _ in range(2)]

for i in range(1 << N):
  DP[0][i] = D[i]

print(t)
print(t - max(DP[0]))

dt = 1
Ans = [0] * N
for i in range(2, N):
  for j in range(1 << N):
    x = j
    while x >= 0:
      x &= j
      d = j ^ x
      DP[dt][j] = max(DP[dt][j], D[d] + DP[1 - dt][x])
      x -= 1
  print(t - max(DP[dt]))
  dt = 1 - dt

print(0)

Submission status: Wrong Answer

Solution code:
def hersCode(n, f):
  l = 10001
  r = -10000
  for i in range(N):
    if (1 << i) & n:
      x, y, p = XYP[i]
      if f:
        l = min(l, x)
        r = max(r, x)
      else:
        l = min(l, y)
        r = max(r, y)
  r += 1

  R = {}
  phi = (1 + 5 ** (1 / 2)) / 2
  while abs(l - r) > 2:
    d1 = (l * phi + r) // (1 + phi)
    d2 = (l + r * phi) // (1 + phi)
    if d1 in R:
      dd1 = R[d1]
    else:
      dd1 = exe(n, f, d1)
      R[d1] = dd1
    if d2 in R:
      dd2 = R[d2]
    else:
      dd2 = exe(n, f, d2)
      R[d2] = dd2
    if dd1 > dd2:
      r = int(d2)
    else:
      l = int(d1)
  ans = 0
  for i in range(l, r):
    if i in R:
      ans = max(ans, R[i])
    else:
      ans = max(ans, exe(n, f, i))
  return ans

def exe(n, f, m):
  q = 0
  for i in range(N):
    if (1 << i) & n:
      x, y, p = XYP[i]
      if f == True:
        if min(abs(x), abs(y)) > abs(x - m):
          q += L[i] - abs(x - m) * p
      else:
        if min(abs(x), abs(y)) > abs(y - m):
          q += L[i] - abs(y - m) * p
  return q

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

if N == 1:
  x, y, p = XYP[0]
  print(min(abs(x), abs(y)) * p)
  print(0)
  exit()

L = [0] * N
c = 0
t = 0
for x, y, p in XYP:
  t += min(abs(x), abs(y)) * p
  L[c] = min(abs(x), abs(y)) * p
  c += 1

D = [0] * (1 << N)

for i in range(1, 1 << N):
  D[i] = max(hersCode(i, True), hersCode(i, False))

DP = [[0] * (1 << N) for _ in range(2)]

for i in range(1 << N):
  DP[0][i] = D[i]

print(t)
print(t - max(DP[0]))

dt = 1
Ans = [0] * N
for i in range(2, N):
  for j in range(1 << N):
    x = j
    while x >= 0:
      x &= j
      d = j ^ x
      DP[dt][j] = max(DP[dt][j], D[d] + DP[1 - dt][x])
      x -= 1
  print(t - max(DP[dt]))
  dt = 1 - dt

print(0)

Submission status: Wrong Answer

Solution code:
def popcount(x):
    x = (x & 0x5555555555555555) + (x >> 1  & 0x5555555555555555)
    x = (x & 0x3333333333333333) + (x >> 2  & 0x3333333333333333)
    x = (x & 0x0f0f0f0f0f0f0f0f) + (x >> 4  & 0x0f0f0f0f0f0f0f0f)
    x = (x & 0x00ff00ff00ff00ff) + (x >> 8  & 0x00ff00ff00ff00ff)
    x = (x & 0x0000ffff0000ffff) + (x >> 16 & 0x0000ffff0000ffff)
    x = (x & 0x00000000ffffffff) + (x >> 32 & 0x00000000ffffffff)
    return x

n = int(input())

x = [0] * n
y = [0] * n
p = [0] * n
s0 = 0
for i in range(n):
    x[i], y[i], p[i] = map(int, input().split())
    s0 += min(abs(x[i]), abs(y[i])) * p[i]
min_s = [s0] * (n + 1)

abs_x = list(map(abs, x))
abs_y = list(map(abs, y))

dist_x = [abs_x[:] for _ in range(1 << n)]
dist_y = [abs_y[:] for _ in range(1 << n)]
'''
for bit in range(1 << n):
    for i in range(n):
        if bit & 1 << i:
            for j in range(n):
                dist_x[bit][j] = min(dist_x[bit][j], abs(x[j] - x[i]))
                dist_y[bit][j] = min(dist_y[bit][j], abs(y[j] - y[i]))
    for i in range(n):
        dist_x[bit][i] *= p[i]
        dist_y[bit][i] *= p[i]
'''
for bit in range(1, 1 << n):
    i = popcount(bit)
    bit_x = bit
    while True:
        bit_y = bit_x ^ bit
        s = 0
        for j in range(n):
            s += min(dist_x[bit_x][j], dist_y[bit_y][j])
        if min_s[i] > s:
            min_s[i] = s
        if bit_x == 0:
            break
        bit_x = (bit_x - 1) & bit

for s in min_s:
    print(s)

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
from functools import lru_cache
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    P = read_lists(N)

    C = [10 ** 12] * (N + 1)
    CX = [[0] * N for _ in range(2 ** N)]
    CY = [[0] * N for _ in range(2 ** N)]
    LX = [0] * (2 ** N)
    LY = [0] * (2 ** N)
    for T in range(2 ** N):
        X = {0}
        Y = {0}
        S = set()
        for i in range(N):
            if (T >> i) & 1:
                X.add(P[i][0])
                Y.add(P[i][1])
                S.add(i)

        for j, (xp, yp, _) in enumerate(P):
            if j in S:
                continue
            CX[T][j] = min(abs(xp - x) for x in X)
            CY[T][j] = min(abs(yp - y) for y in Y)
        LX[T] = len(X)
        LY[T] = len(Y)

    for T in itertools.product((0, 1, 2), repeat=N):
        tx = 0
        ty = 0
        for i, t in enumerate(T):
            if t == 1:
                tx |= (1 << i) 
            elif t == 2:
                ty |= (1 << i) 

        p = LX[tx] + LY[ty] - 2
        if C[p] == 0:
            continue

        c = 0
        for n in range(N):
            c += min(CX[tx][n], CY[ty][n]) * P[n][2]
        C[p] = min(C[p], c)

    print("\n".join(map(str, C)))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def make_key(d):
    c = 0
    xkey,ykey = [],[]
    for i in range(n):
        if d[i]==0:
            xkey.append(0)
            ykey.append(0)
        elif d[i]==1:
            xkey.append(1)
            ykey.append(0)
            c += 1
        else:
            xkey.append(0)
            ykey.append(1)
            c += 1
    return c,tuple(xkey),tuple(ykey)

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

ans = [10**10]*(n+1)

import itertools 
data = list(itertools.product(range(2), repeat = n))

costx = dict()
costy = dict()

for d in data:
    tempx = []
    tempy = []
    for x,y,p in xyp:
        mx = my = min(abs(x),abs(y))
        for i in range(n):
            if d[i]==0:continue
            mx = min(mx,abs(x-xyp[i][0]))
            my = min(my,abs(y-xyp[i][1]))
        tempx.append(mx*p)
        tempy.append(my*p)
    costx[d] = tempx
    costy[d] = tempy

data = list(itertools.product(range(3), repeat = n))

for d in data:
    c,xkey,ykey = make_key(d)
    temp = 0
    for i in range(n):
        temp += min(costx[xkey][i],costy[ykey][i])
    ans[c] = min(ans[c],temp)

for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
def make_key(d):
    c = 0
    xkey,ykey = [],[]
    for i in range(n):
        if d[i]==0:
            xkey.append(0)
            ykey.append(0)
        elif d[i]==1:
            xkey.append(1)
            ykey.append(0)
            c += 1
        else:
            xkey.append(0)
            ykey.append(1)
            c += 1
    return c,tuple(xkey),tuple(ykey)
    
n = int(input())
xyp = [list(map(int,input().split())) for i in range(n)]
reach = [False]*n

ans = [10**10]*(n+1)

import itertools 
data = list(itertools.product(range(2), repeat = n))

costx = dict()
costy = dict()

for d in data:
    temp = []
    for x,y,p in xyp:
        m = min(abs(x),abs(y))
        for i in range(n):
            if d[i]==0:continue
            m = min(m,abs(x-xyp[i][0]))
        temp.append(m*p)
    costx[d] = temp

for d in data:
    temp = []
    for x,y,p in xyp:
        m = min(abs(x),abs(y))
        for i in range(n):
            if d[i]==0:continue
            m = min(m,abs(y-xyp[i][1]))
        temp.append(m*p)
    costy[d] = temp

data = list(itertools.product(range(3), repeat = n))

for d in data:
    c,xkey,ykey = make_key(d)
    temp = 0
    for i in range(n):
        temp += min(costx[xkey][i],costy[ykey][i])
    ans[c] = min(ans[c],temp)

for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
def make_key(d):
    c = 0
    xkey,ykey = [],[]
    for i in range(n):
        if d[i]==0:
            xkey.append(0)
            ykey.append(0)
        elif d[i]==1:
            xkey.append(1)
            ykey.append(0)
            c += 1
        else:
            xkey.append(0)
            ykey.append(1)
            c += 1
    return c,tuple(xkey),tuple(ykey)


n = int(input())
xyp = [list(map(int,input().split())) for i in range(n)]
reach = [False]*n

ans = [10**10]*(n+1)

import itertools 
data = list(itertools.product(range(2), repeat = n))

costx = dict()
costy = dict()

for d in data:
    temp = []
    for x,y,p in xyp:
        m = min(abs(x),abs(y))
        for i in range(n):
            if d[i]==0:continue
            m = min(m,abs(x-xyp[i][0]))
        temp.append(m*p)
    costx[d] = temp

for d in data:
    temp = []
    for x,y,p in xyp:
        m = min(abs(x),abs(y))
        for i in range(n):
            if d[i]==0:continue
            m = min(m,abs(y-xyp[i][1]))
        temp.append(m*p)
    costy[d] = temp

data = list(itertools.product(range(3), repeat = n))

for d in data:
    c,xkey,ykey = make_key(d)
    temp = 0
    for i in range(n):
        temp += min(costx[xkey][i],costy[ykey][i])
    ans[c] = min(ans[c],temp)

for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys

INF = 1 << 62


def solve(N, X, Y, P):

    xsel = [[None] * N for x in range(2**N)]
    ysel = [[None] * N for x in range(2**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():

    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)

    ans = solve(N, X, Y, P)
    for i in range(N):
        print(ans[i])
    return


main()