CF BUDDY
← Problems·

1669H · Maximal AND

1300 · bitmasks, greedy, math

Problem: Let AND\mathsf{AND} denote the bitwise AND operation, and OR\mathsf{OR} denote the bitwise OR operation.

You are given an array aa of length nn and a non-negative integer kk. You can perform at most kk operations on the array of the following type:

  • Select an index ii (1in1 \leq i \leq n) and replace aia_i with aia_i OR\mathsf{OR} 2j2^j where jj is any integer between 00 and 3030 inclusive. In other words, in an operation you can choose an index ii (1in1 \leq i \leq n) and set the jj-th bit of aia_i to 11 (0j300 \leq j \leq 30).

Output the maximum possible value of a1a_1 AND\mathsf{AND} a2a_2 AND\mathsf{AND} \dots AND\mathsf{AND} ana_n after performing at most kk operations.

Input Format: The first line of the input contains a single integer tt (1t1001 \le t \le 100) — the number of test cases. The description of test cases follows.

The first line of each test case contains the integers nn and kk (1n21051 \le n \le 2 \cdot 10^5, 0k1090 \le k \le 10^9).

Then a single line follows, containing nn integers describing the arrays aa (0ai<2310 \leq a_i < 2^{31}).

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output a single line containing the maximum possible AND\mathsf{AND} value of a1a_1 AND\mathsf{AND} a2a_2 AND\mathsf{AND} \dots AND\mathsf{AND} ana_n after performing at most kk operations.

Note: For the first test case, we can set the bit 11 (212^1) of the last 22 elements using the 22 operations, thus obtaining the array [22, 33, 33], which has AND\mathsf{AND} value equal to 22.

For the second test case, we can't perform any operations so the answer is just the AND\mathsf{AND} of the whole array which is 44.

Sample Cases

Case 1

Input

4
3 2
2 1 1
7 0
4 6 6 28 6 6 12
1 30
0
4 4
3 1 3 1

Output

2
4
2147483646
1073741825

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback