P21336 [CodeChef] CNCTCT
提高+
题目描述

The country of Berland initially has N isolated cities, the i-th city has a significance of A_{i}. The President of Berland wants to connect all the cities. He can construct a bidirectional road of length L (L > 0) from city X to city Y if (A_{X} \& A_{Y} \& L) = L, where \& represents the [bitwise AND] operator.

What is the minimum total length of roads he has to construct, in order to connect all the cities in Berland? Print -1 if it is impossible.

Note: City X and City Y are said to be connected if there exists a sequence of cities $C_{1}, C_{2}, \ldots, C_{K} (K$ ≥ 1) such that C_{1} = X, C_{K} = Y, and there exists a road from C_{i} to C_{i+1}, (1 ≤ i < K). All the cities in Berland are said to be connected when every pair of cities in Berland are connected.

------ Input Format ------

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two lines of input.
  • The first line of each test case contains N — the number of cities.
  • The second line contains $N space-separated integers A_{1}, A_{2}, \ldots, A_{N}$ — the significance of each of the N cities.

------ Output Format ------

For each test case, output on a new line the minimum total length of roads to connect all the cities in Berland, or -1 if it is impossible.

------ Constraints ------

$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} ≤ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3 \cdot 10^{5}$.

----- Sample Input 1 ------
2
4
4 4 4 4
3
2 4 6
----- Sample Output 1 ------
12
6

----- explanation 1 ------
Test case 1: The following roads with minimum total length can be constructed to connect Berland:
- A road with $L=4$ between city $1$ and city $2$, since ($A_{1} \& A_{2} \& 4) = 4$.
- A road with $L=4$ between city $2$ and city $3$, since ($A_{2} \& A_{3} \& 4) = 4$.
- A road with $L=4$ between city $3$ and city $4$, since ($A_{3} \& A_{4} \& 4) = 4$.

Test case 2: The following roads with minimum total length can be constructed to connect Berland:
- A road with $L=2$ between city $1$ and city $3$, since ($A_{1} \& A_{3} \& 2) = 2$.
- A road with $L=4$ between city $2$ and city $3$, since ($A_{2} \& A_{3} \& 4) = 4$.

输入格式

见题面

输出格式

见题面

样例输入
2 4 4 4 4 4 3 2 4 6
样例输出
12 6