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:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

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

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

alphabets = "abcdefghijklmnopqrstuvwxyz"

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

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

    anss = [INF] * (N+1)

    preX, preY = {}, {}
    for pattern in itertools.product([0, 1], repeat=N):
        X, Y = [0], [0]
        keyX, keyY = 0, 0
        tmpX, tmpY = [], []
        for i, bit in enumerate(pattern):
            keyX |= bit << i
            keyY |= bit << i
            if bit:
                X.append(XYP[i][0])
                Y.append(XYP[i][1])
        for x, y, p in XYP:
            min_distX = abs(y)
            min_distY = abs(x)
            for rx in X:
                if min_distX > abs(x - rx):
                    min_distX = abs(x - rx)
            for ry in Y:
                if min_distY > abs(y - ry):
                    min_distY = abs(y - ry)
            tmpX.append(min_distX * p)
            tmpY.append(min_distY * p)
        preX[keyX] = tmpX
        preY[keyY] = tmpY

    for pattern in itertools.product([0, 1, 2], repeat=N):
        keyX, keyY = 0, 0
        root = 0
        for i, which in enumerate(pattern):
            if which == 1:
                keyX |= which << i
                root += 1
            elif which == 2:
                keyY |= int(bool(which)) << i
                root += 1

        tmp = 0
        for vx, vy in zip(preX[keyX], preY[keyY]):
            tmp += min(vx, vy)

        if anss[root] > tmp:
            anss[root] = tmp

    for ans in anss:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def main():
    INF = 10 ** 15
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    r = []
    for a, b, c in zip(X, Y, P):
        r.append(min(abs(a), abs(b)) * c)

    hor = [copy.copy(r) for _ in range(2 ** N)]
    ver = [copy.copy(r) for _ in range(2 ** N)]

    for i in range(2 ** N):
        k = i
        for b in range(N):
            if k % 2 == 1:
                for j in range(N):
                    hor[i][j] = min(hor[i][j], abs(X[j] - X[b]) * P[j])
                    ver[i][j] = min(ver[i][j], abs(Y[j] - Y[b]) * P[j])
            k //= 2

    ans = [INF] * (N + 1)
    for i in range(3 ** N):
        cnt:int = 0
        bx:int = 0
        by:int = 0
        bit:int = 1
        for b in range(N):
            if i % 3 == 1:
                bx += bit
                cnt += 1
            elif i % 3 == 2:
                by += bit
                cnt += 1
            bit *= 2
            i //= 3
        s:int = 0
        for b in range(N):
            s += min(hor[bx][b], ver[by][b])
        ans[cnt] = min(ans[cnt], s)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

alphabets = "abcdefghijklmnopqrstuvwxyz"

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

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

    anss = [INF] * (N+1)

    preX, preY = {}, {}
    for pattern in itertools.product([0, 1], repeat=N):
        X, Y = [0], [0]
        keyX, keyY = 0, 0
        tmpX, tmpY = [], []
        for i, bit in enumerate(pattern):
            keyX |= bit << i
            keyY |= bit << i
            if bit:
                X.append(XYP[i][0])
                Y.append(XYP[i][1])
        for x, y, p in XYP:
            min_distX = abs(y)
            min_distY = abs(x)
            for rx in X:
                if min_distX > abs(x - rx):
                    min_distX = abs(x - rx)
            for ry in Y:
                if min_distY > abs(y - ry):
                    min_distY = abs(y - ry)
            tmpX.append(min_distX * p)
            tmpY.append(min_distY * p)
        preX[keyX] = tmpX
        preY[keyY] = tmpY

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

        tmp = 0
        for vx, vy in zip(preX[keyX], preY[keyY]):
            tmp += min(vx, vy)

        if anss[root] > tmp:
            anss[root] = tmp

    for ans in anss:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

alphabets = "abcdefghijklmnopqrstuvwxyz"

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

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

    anss = [INF] * (N+1)

    preX = {}
    for pattern in itertools.product([0, 1], repeat=N):
        X = [0]
        key = 0
        tmp = []
        for i, bit in enumerate(pattern):
            key |= bit << i
            if bit: X.append(XYP[i][0])
        for x, y, p in XYP:
            min_dist = abs(y)
            for rx in X:
                if min_dist > abs(x - rx):
                    min_dist = abs(x - rx)
            tmp.append(min_dist * p)
        preX[key] = tmp

    preY = {}
    for pattern in itertools.product([0, 1], repeat=N):
        Y = [0]
        key = 0
        tmp = []
        for i, bit in enumerate(pattern):
            key |= bit << i
            if bit: Y.append(XYP[i][1])
        for x, y, p in XYP:
            min_dist = abs(x)
            for ry in Y:
                if min_dist > abs(y - ry):
                    min_dist = abs(y - ry)
            tmp.append(min_dist * p)
        preY[key] = tmp

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

        tmp = 0
        for vx, vy in zip(preX[keyX], preY[keyY]):
            tmp += min(vx, vy)

        if anss[root] > tmp:
            anss[root] = tmp

    for ans in anss:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def distance(X, Y, P, N, L):
    D = 0
    for i in range(N):
        D += moyori(X[i], Y[i], P[i], L)
    return D
def moyori(x, y, p, L):
    moyori = min(abs(x), abs(y))
    if len(L) >= 1:
        for i in range(0, len(L)):
            if L[i][1] == 'x':
                moyori = min(moyori, abs(x-L[i][0]))
            elif L[i][1] == 'y':
                moyori = min(moyori, abs(y-L[i][0]))
    return moyori * p
def solve(X, Y, P, N):
    dis = [float('inf') for K in range(N+1)]
    for i in range(2**(2*N)):
        L = []
        for j in range(2*N):
            if ((i >> j)  & 1):
                if j <= N - 1:
                    L.append([X[j], 'x'])
                elif N <= j <= 2*N - 1:
                    L.append([Y[j-N], 'y'])
            if len(L) <= N:
                dis[len(L)] = min(dis[len(L)], distance(X, Y, P, N, L))
    return dis
N = int(input())
X = [] 
Y = []
P = []
for i in range(N):
    x, y, p = map(int, input().split())
    X.append(x)
    Y.append(y)
    P.append(p)
a = solve(X, Y, P, N)
for res in a:
    print(res)

Submission status: Time Limit Exceeded

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

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

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

alphabets = "abcdefghijklmnopqrstuvwxyz"

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

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

    anss = [INF] * (N+1)

    preX = {}
    for pattern in itertools.product([0, 1], repeat=N):
        X = [0]
        key = 0
        tmp = []
        for i, bit in enumerate(pattern):
            key |= bit << i
            if bit: X.append(XYP[i][0])
        for x, y, p in XYP:
            min_dist = abs(y)
            for rx in X:
                min_dist = min(min_dist, abs(x - rx))
            tmp.append(min_dist * p)
        preX[key] = tmp

    preY = {}
    for pattern in itertools.product([0, 1], repeat=N):
        Y = [0]
        key = 0
        tmp = []
        for i, bit in enumerate(pattern):
            key |= bit << i
            if bit: Y.append(XYP[i][1])
        for x, y, p in XYP:
            min_dist = abs(x)
            for ry in Y:
                min_dist = min(min_dist, abs(y - ry))
            tmp.append(min_dist * p)
        preY[key] = tmp

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

        tmp = 0
        for vx, vy in zip(preX[keyX], preY[keyY]):
            tmp += min(vx, vy)

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

    for ans in anss:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

alphabets = "abcdefghijklmnopqrstuvwxyz"

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

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

    anss = [INF] * (N+1)

    preX = {}
    for pattern in itertools.product([0, 1], repeat=N):
        X = [0]
        key = 0
        tmp = []
        for i, bit in enumerate(pattern):
            key |= bit << i
            if bit: X.append(XYP[i][0])
        for x, y, _ in XYP:
            min_dist = abs(y)
            for rx in X:
                min_dist = min(min_dist, abs(x - rx))
            tmp.append(min_dist)
        preX[key] = tmp

    preY = {}
    for pattern in itertools.product([0, 1], repeat=N):
        Y = [0]
        key = 0
        tmp = []
        for i, bit in enumerate(pattern):
            key |= bit << i
            if bit: Y.append(XYP[i][1])
        for x, y, _ in XYP:
            min_dist = abs(x)
            for ry in Y:
                min_dist = min(min_dist, abs(y - ry))
            tmp.append(min_dist)
        preY[key] = tmp

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

        tmp = 0
        distX = preX[keyX]
        distY = preY[keyY]
        for i, (dx, dy) in enumerate(zip(distX, distY)):
            tmp += min(dx, dy) * XYP[i][2]

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

    for ans in anss:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

INF = float('inf')

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

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

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

Submission status: Time Limit Exceeded

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


def main():
    INF = 10 ** 15
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    r = []
    for a, b, c in zip(X, Y, P):
        r.append(min(abs(a), abs(b)) * c)

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

    for i in range(2 ** N):
        hor[i] = copy.copy(r)
        ver[i] = copy.copy(r)
        k = i
        for b in range(N):
            if k % 2 == 1:
                for j in range(N):
                    hor[i][j] = min(hor[i][j], abs(X[j] - X[b]) * P[j])
                    ver[i][j] = min(ver[i][j], abs(Y[j] - Y[b]) * P[j])
            k //= 2

    ans = [INF] * (N + 1)
    for i in range(3 ** N):
        cnt:int = 0
        bx:int = 0
        by:int = 0
        bit:int = 1
        for b in range(N):
            if i % 3 == 1:
                bx += bit
                cnt += 1
            elif i % 3 == 2:
                by += bit
                cnt += 1
            bit *= 2
            i //= 3
        s:int = 0
        for b in range(N):
            s += min(hor[bx][b], ver[by][b])
        ans[cnt] = min(ans[cnt], s)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def main():
    INF = 10 ** 15
    LI = lambda: [int(x) for x in sys.stdin.readline().split()]
    NI = lambda: int(sys.stdin.readline())

    N = NI()
    X = []
    Y = []
    P = []

    for _ in range(N):
        a, b, c = LI()
        X.append(a)
        Y.append(b)
        P.append(c)

    r = []
    for a, b, c in zip(X, Y, P):
        r.append(min(abs(a), abs(b)) * c)

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

    for i in range(2 ** N):
        hor[i] = copy.copy(r)
        ver[i] = copy.copy(r)
        k = i
        for b in range(N):
            if k % 2 == 1:
                for j in range(N):
                    hor[i][j] = min(hor[i][j], abs(X[j] - X[b]) * P[j])
                    ver[i][j] = min(ver[i][j], abs(Y[j] - Y[b]) * P[j])
            k //= 2

    ans = [INF] * (N + 1)
    for i in range(3 ** N):
        cnt = 0
        bx = 0
        by = 0
        bit = 1
        for b in range(N):
            if i % 3 == 1:
                bx += bit
                cnt += 1
            elif i % 3 == 2:
                by += bit
                cnt += 1
            bit *= 2
            i //= 3
        s = 0
        for b in range(N):
            s += min(hor[bx][b], ver[by][b])
        ans[cnt] = min(ans[cnt], s)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

INF = float('inf')

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

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

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

Submission status: Time Limit Exceeded

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

INF = float('inf')

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

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

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

Submission status: Time Limit Exceeded

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

INF = float('inf')

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

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

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

Submission status: Time Limit Exceeded

Solution code:
def distance(X, Y, P, N, L):
    D = 0
    for i in range(N):
        D += moyori(X[i], Y[i], P[i], L)
    return D
def moyori(x, y, p, L):
    moyori = min(abs(x), abs(y))
    if len(L) >= 1:
        for i in range(0, len(L)):
            if L[i][1] == 'x':
                moyori = min(moyori, abs(x-L[i][0]))
            elif L[i][1] == 'y':
                moyori = min(moyori, abs(y-L[i][0]))
    return moyori * p
def solve(X, Y, P, N):
        for K in range(N+1):
            dis = float('inf')
            for i in range(2**(2*N)):
                L = []
                for j in range(2*N):
                    if ((i >> j)  & 1):
                        if j <= N - 1:
                            L.append([X[j], 'x'])
                        elif N <= j <= 2*N - 1:
                            L.append([Y[j-N], 'y'])
                if len(L) == K:
                    dis = min(dis, distance(X, Y, P, N, L))
            print(dis)
N = int(input())
X = []
Y = []
P = []
for i in range(N):
    x, y, p = map(int, input().split())
    X.append(x)
    Y.append(y)
    P.append(p)
solve(X, Y, P, N)

Submission status: Time Limit Exceeded

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

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

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

alphabets = "abcdefghijklmnopqrstuvwxyz"

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

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

    anss = [INF] * (N+1)

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

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

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

    for ans in anss:
        print(ans)

if __name__ == '__main__':
    main()