codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1368D.md (2125B)


      1 Source: https://codeforces.com/problemset/problem/1368/D
      2 
      3 D. AND, OR and square sum
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 512 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Gottfried learned about binary number representation. He then came up with this task and presented it to you.
     22 
     23 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).
     24 
     25 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?
     26 
     27 Input
     28 
     29 The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
     30 
     31 The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i \lt 2^{20}$).
     32 
     33 Output
     34 
     35 Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
     36 
     37 Examples
     38 
     39 Input
     40 
     41 ```
     42 1
     43 123
     44 ```
     45 
     46 Output
     47 
     48 ```
     49 15129
     50 ```
     51 
     52 Input
     53 
     54 ```
     55 3
     56 1 3 5
     57 ```
     58 
     59 Output
     60 
     61 ```
     62 51
     63 ```
     64 
     65 Input
     66 
     67 ```
     68 2
     69 349525 699050
     70 ```
     71 
     72 Output
     73 
     74 ```
     75 1099509530625
     76 ```
     77 
     78 Note
     79 
     80 In the first sample no operation can be made, thus the answer is $123^2$.
     81 
     82 In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
     83 
     84 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$.