Problem:


Score: 500 points

Problem Statement

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

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

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

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

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

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

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

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

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

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

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

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

Constraints

1 \leq N \leq 15

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

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

1 \leq P_i \leq 1 \ 000 \ 000

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print the answer in N+1 lines.

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

Sample Input 1

3
1 2 300
3 3 600
1 4 800

Sample Output 1

2900
900
0
0

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

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

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

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

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

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

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

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

Sample Input 2

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

Sample Output 2

13800
1600
0
0
0
0

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

Sample Input 3

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

Sample Output 3

26700
13900
3200
1200
0
0
0

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

Sample Input 4

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

Sample Output 4

2576709
1569381
868031
605676
366338
141903
0
0
0

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



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


Sample output:
2900
900
0
0

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.buffer.readline

n = int(input())
infi = 10 ** 20
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 = abs(y - 0)
        ytemp = abs(x - 0)
        for a in xroad:
            xtemp = min(xtemp, abs(x - a))
        for b in yroad:
            ytemp = min(ytemp, abs(y - b))
        x_kouho[i][j] = p * xtemp
        y_kouho[i][j] = p * 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: Time Limit Exceeded

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 ** 12
        y_cost = 10 ** 12

        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 ** 12] * (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: Time Limit Exceeded

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

  while abs(l - r) > 2:
    d1 = l + (r - l) // 3
    d2 = r - (r - l) // 3
    dd1 = exe(n, f, d1)
    dd2 = exe(n, f, d2)
    if dd1 < dd2:
      r = d2
    else:
      l = d1
  ans = 10 ** 14
  for i in range(l, r):
    ans = min(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:
        q += abs(x - m) * p
      else:
        q += 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)
DP = [[0] * (1 << N) for _ in range(2)]

for i in range(1, 1 << N):
  D[i] = min(hersCode(i, True), hersCode(i, False))
  for j in range(N):
    if (1 << j) & i:
      DP[0][i] += L[j]

print(DP[0][-1])
dt = 1
for i in range(1, N):
  for j in range(1 << N):
    x = j
    DP[dt][j] = 10 ** 14
    while x >= 0:
      x &= j
      d = j ^ x
      DP[dt][j] = min(DP[dt][j], D[d] + DP[1 - dt][x])
      x -= 1
  k = DP[dt][-1]
  print(k)
  if k == 0:
    for j in range(i + 1, N):
      print(0)
    break
  dt = 1 - dt

print(0)

Submission status: Time Limit Exceeded

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>>i)&1:
                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)

            sub = (sub - 1) & i
            if sub == i:
                break

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

    for ans in answer:
      print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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>>i)&1:
                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
        for k in range(1000000):
            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==0:
                break
            sub = (sub - 1) & i
            if sub == i:
                break

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

    for ans in answer:
      print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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==0:
                break
            sub = (sub - 1) & i
            if sub == i:
                break

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

    for ans in answer:
      print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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 = 0
        for j in range(N):
            cnt += (i >> j) & 1
        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==0:
                break
            sub = (sub - 1) & i
            if sub == i:
                break

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

    for ans in answer:
      print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

  while abs(l - r) > 2:
    d1 = l + (r - l) // 3
    d2 = r - (r - l) // 3
    dd1 = exe(n, f, d1)
    dd2 = exe(n, f, d2)
    if dd1 < dd2:
      r = d2
    else:
      l = d1
  ans = 10 ** 18
  for i in range(l, r):
    ans = min(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:
        q += abs(x - m) * p
      else:
        q += 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)
DP = [[0] * (1 << N) for _ in range(2)]

for i in range(1, 1 << N):
  D[i] = min(hersCode(i, True), hersCode(i, False))
  for j in range(N):
    if (1 << j) & i:
      DP[0][i] += L[j]

print(DP[0][-1])
dt = 1
for i in range(1, N):
  for j in range(1 << N):
    x = j
    DP[dt][j] = 10 ** 18
    while x >= 0:
      x &= j
      d = j ^ x
      DP[dt][j] = min(DP[dt][j], D[d] + DP[1 - dt][x])
      x -= 1
  k = DP[dt][-1]
  print(k)
  if k == 0:
    for j in range(i + 1, N):
      print(0)
    break
  dt = 1 - dt

print(0)

Submission status: Time Limit Exceeded

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)
    for bit in range(3**15):
        x_bit = 0
        y_bit = 0
        cnt = 0
        b = bit
        for i in range(N):
            d = b % 3
            b //= 3
            if d == 2:
                # x = a
                x_bit += pow_2[i]
                cnt += 1
            elif d == 1:
                # y = a
                y_bit += pow_2[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: Time Limit Exceeded

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 ** N
    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: Time Limit Exceeded

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

  while abs(l - r) > 2:
    d1 = l + (r - l) // 3
    d2 = r - (r - l) // 3
    dd1 = exe(n, f, d1)
    dd2 = exe(n, f, d2)
    if dd1 > dd2:
      r = d2
    else:
      l = d1
  ans = 0
  for i in range(l, r):
    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)
DP = [[0] * (1 << N) for _ in range(2)]

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

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

dt = 1
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
  k = t - max(DP[dt])
  print(k)
  if k == 0:
    for j in range(i + 1, N):
      print(0)
    break
  dt = 1 - dt

print(0)

Submission status: Time Limit Exceeded

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)
    for bit in range(3**15):
        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 += pow_2[i]
                cnt += 1
            elif b == 1:
                # y = a
                y_bit += pow_2[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: Time Limit Exceeded

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

  while abs(l - r) > 2:
    d1 = l + (r - l) // 3
    d2 = r - (r - l) // 3
    dd1 = exe(n, f, d1)
    dd2 = exe(n, f, d2)
    if dd1 > dd2:
      r = d2
    else:
      l = d1
  ans = 0
  for i in range(l, r):
    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
  k = t - max(DP[dt])
  print(k)
  if k == 0:
    for j in range(i + 1, N):
      print(0)
    break
  dt = 1 - dt

print(0)

Submission status: Time Limit Exceeded

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

  while abs(l - r) > 2:
    d1 = l + (r - l) // 3
    d2 = r - (r - l) // 3
    dd1 = exe(n, f, d1)
    dd2 = exe(n, f, d2)
    if dd1 > dd2:
      r = d2
    else:
      l = d1
  ans = 0
  for i in range(l, r):
    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]

Ans = [0] * (N + 1)
Ans[0] = t
Ans[1] = t - max(DP[0])

dt = 1
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
  Ans[i] = t - max(DP[dt])
  dt = 1 - dt

Ans[N] = 0

for i in Ans:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from itertools import product
    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(pow_2[N])]
    Y_dist = [[0]*N for _ in range(pow_2[N])]

    # bitという道路群を建設したときの，各集落の評価距離
    for bit in range(pow_2[N]):
        cur_x = [0]
        cur_y = [0]
        for i in range(N):
            if bit & (pow_2[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 = [100_000_000_000] * (N+1)
    for bit in product(range(3), repeat=N):
        x_bit = 0
        y_bit = 0
        x_set = set()
        y_set = set()
        for i in range(N):
            if bit[i] == 2:
                # x = a
                x_bit += pow_2[i]
                x_set.add(T[i][0])
            elif bit[i] == 1:
                # y = a
                y_bit += pow_2[i]
                y_set.add(T[i][1])
        cnt = len(x_set) + len(y_set)
        # 距離の評価
        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: Time Limit Exceeded

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

  while abs(l - r) > 2:
    d1 = l + (r - l) // 3
    d2 = r - (r - l) // 3
    dd1 = exe(n, f, d1)
    dd2 = exe(n, f, d2)
    if dd1 > dd2:
      r = d2
    else:
      l = d1
  ans = 0
  for i in range(l, r):
    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: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from itertools import product
    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)

    pow_2 = [pow(2, i) for i in range(N)]

    answer = [100_000_000_000] * (N+1)
    x_set = set()
    y_set = set()
    for bit in product(range(3), repeat=N):
        x_bit = 0
        y_bit = 0
        x_set = set()
        y_set = set()
        for i in range(N):
            if bit[i] == 2:
                # x = a
                x_bit += pow_2[i]
                x_set.add(T[i][0])
            elif bit[i] == 1:
                # y = a
                y_bit += pow_2[i]
                y_set.add(T[i][1])
        cnt = len(x_set) + len(y_set)
        # 距離の評価
        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: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from itertools import product
    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)

    pow_2 = [pow(2, i) for i in range(N)]

    answer = [100_000_000_000] * (N+1)
    for bit in product(range(3), repeat=N):
        x_bit = 0
        y_bit = 0
        x_set = set()
        y_set = set()
        for i in range(N):
            if bit[i] == 2:
                # x = a
                x_bit += pow_2[i]
                x_set.add(T[i][0])
            elif bit[i] == 1:
                # y = a
                y_bit += pow_2[i]
                y_set.add(T[i][1])
        cnt = len(x_set) + len(y_set)
        # 距離の評価
        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()