1669H.md (2142B)
1 Source: https://codeforces.com/problemset/problem/1669/H 2 3 H. Maximal AND 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Let $\mathsf{AND}$ denote the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), and $\mathsf{OR}$ denote the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR). 22 23 You are given an array $a$ of length $n$ and a non-negative integer $k$. You can perform at most $k$ operations on the array of the following type: 24 25 * Select an index $i$ ($1 \leq i \leq n$) and replace $a_i$ with $a_i$ $\mathsf{OR}$ $2^j$ where $j$ is any integer between $0$ and $30$ inclusive. In other words, in an operation you can choose an index $i$ ($1 \leq i \leq n$) and set the $j$-th bit of $a_i$ to $1$ ($0 \leq j \leq 30$). 26 27 Output the maximum possible value of $a_1$ $\mathsf{AND}$ $a_2$ $\mathsf{AND}$ $\dots$ $\mathsf{AND}$ $a_n$ after performing at most $k$ operations. 28 29 Input 30 31 The first line of the input contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. 32 33 The first line of each test case contains the integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $0 \le k \le 10^9$). 34 35 Then a single line follows, containing $n$ integers describing the arrays $a$ ($0 \leq a_i \lt 2^{31}$). 36 37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 38 39 Output 40 41 For each test case, output a single line containing the maximum possible $\mathsf{AND}$ value of $a_1$ $\mathsf{AND}$ $a_2$ $\mathsf{AND}$ $\dots$ $\mathsf{AND}$ $a_n$ after performing at most $k$ operations. 42 43 Example 44 45 Input 46 47 ``` 48 4 49 50 3 2 51 52 2 1 1 53 54 7 0 55 56 4 6 6 28 6 6 12 57 58 1 30 59 60 0 61 62 4 4 63 64 3 1 3 1 65 ``` 66 67 Output 68 69 ``` 70 2 71 4 72 2147483646 73 1073741825 74 ``` 75 76 Note 77 78 For the first test case, we can set the bit $1$ ($2^1$) of the last $2$ elements using the $2$ operations, thus obtaining the array [$2$, $3$, $3$], which has $\mathsf{AND}$ value equal to $2$. 79 80 For the second test case, we can't perform any operations so the answer is just the $\mathsf{AND}$ of the whole array which is $4$.