AND, OR and square sum
Contest: Codeforces Global Round 8
Difficulty rating: 1700
Tags: bitmasks, greedy, math

Gottfried learned about binary number representation. He then came up with this task and presented it to you.

You are given a collection of $$$n$$$ non-negative integers $$$a_1, \ldots, a_n$$$. You are allowed to perform the following operation: choose two distinct indices $$$1 \leq i, j \leq n$$$. If before the operation $$$a_i = x$$$, $$$a_j = y$$$, then after the operation $$$a_i = x~\mathsf{AND}~y$$$, $$$a_j = x~\mathsf{OR}~y$$$, where $$$\mathsf{AND}$$$ and $$$\mathsf{OR}$$$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).

After all operations are done, compute $$$\sum_{i=1}^n a_i^2$$$ — the sum of squares of all $$$a_i$$$. What is the largest sum of squares you can achieve?

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$).

The second line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$0 \leq a_i < 2^{20}$$$).

Output format:
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first sample no operation can be made, thus the answer is $$$123^2$$$.

In the second sample we can obtain the collection $$$1, 1, 7$$$, and $$$1^2 + 1^2 + 7^2 = 51$$$.

If $$$x$$$ and $$$y$$$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $$$x~\mathsf{AND}~y$$$ is set to $$$1$$$ if and only if both corresponding bits of $$$x$$$ and $$$y$$$ are set to $$$1$$$. Similarly, each bit of $$$x~\mathsf{OR}~y$$$ is set to $$$1$$$ if and only if at least one of the corresponding bits of $$$x$$$ and $$$y$$$ are set to $$$1$$$. For example, $$$x = 3$$$ and $$$y = 5$$$ are represented as $$$011_2$$$ and $$$101_2$$$ (highest bit first). Then, $$$x~\mathsf{AND}~y = 001_2 = 1$$$, and $$$x~\mathsf{OR}~y = 111_2 = 7$$$.

Example input:
1
123

Example output:
15129

Example input:
3
1 3 5

Example output:
51

Editorial:
Let's look at a single operation $$$x, y \to x~\mathsf{AND}~y, x~\mathsf{OR}~y$$$, let the last two be $$$z$$$ and $$$w$$$ respectively. We can notice that $$$x + y = z + w$$$. Indeed, looking at each bit separately we can see that the number of $$$1$$$'s in this bit is preserved.
Clearly $$$z \leq w$$$, and suppose also that $$$x \leq y$$$. Since the sum is preserved, we must have $$$z = x - d$$$, $$$w = y + d$$$ for some non-negative $$$d$$$. But then the sum of squares of all numbers changes by $$$z^2 + w^2 - x^2 - y^2$$$. Substituting for $$$z$$$ and $$$w$$$ and simplifying, this is equal to $$$2d(d + y - x)$$$, which is positive when $$$d > 0$$$.
Side note: an easier (?) way to spot the same thing is to remember that $$$f(x) = x^2$$$ is convex, thus moving two points on the parabola away from each other by the same amount increases the sum of values.
It follows that any operation increases the square sum (as long as any numbers change), and we should keep doing operations while we can.
When can we no longer make meaningful operations? At that point all numbers should be submasks of each other. The only way that could happen is when for any bit only several largest numbers have $$$1$$$ in that position. We also know that the number of $$$1$$$'s in each bit across all numbers is preserved. Thus, it's easy to recover the final configuration: for each bit count the number of $$$1$$$'s, and move all these $$$1$$$'s to the last (=greatest) numbers. For example, for the array $$$[1, 2, 3, 4, 5, 6, 7]$$$ there are four $$$1$$$'s in each of the smallest three bits, thus the final configuration is $$$[0, 0, 0, 7, 7, 7, 7]$$$. Finally, print the sum of squares of all these numbers.
The total complexity is $$$O(n \log_2 A)$$$, where $$$A$$$ is the largest possible number (thus $$$\log_2 A$$$ is roughly the number of bits involved).