#P7973. Connected Components Value Sum

    ID: 21157 Type: Default 1000ms 256MiB

Connected Components Value Sum

Connected Components Value Sum

Given a graph with \(N\) nodes, each node \(i\) has a weight \(A_i\) and a value \(B_i\). An undirected edge exists between nodes \(x\) and \(y\) if and only if

\[ A_x \oplus A_y > \max(A_x, A_y) \]

where \(\oplus\) denotes the bitwise XOR operation. For each node \(i\) from \(1\) to \(N\), output the sum of values \(B\) for all nodes in the connected component containing node \(i\).

inputFormat

The first line contains an integer \(N\) indicating the number of nodes.

The second line contains \(N\) space-separated integers representing \(A_1, A_2, \dots, A_N\).

The third line contains \(N\) space-separated integers representing \(B_1, B_2, \dots, B_N\).

outputFormat

Output a single line containing \(N\) space-separated integers. The \(i\)-th integer should be the sum of \(B\) values in the connected component that contains node \(i\).

sample

3
1 2 3
1 2 3
3 3 3