Looking for Taste
Time limit3sMemory limit512 MB
Given N numbers, choose at most K of them so their bitwise OR is as large as possible.
- Level
Medium5 of 10
- Topics
- Bit manipulation, Greedy, Brute force, Math
- Solved
- No attempts yet
Problem
Fouad went to a restaurant and, while reading the menu, found an interesting piece of information about each meal: the tastes each meal contains.
The menu contains N meals, and each meal is represented by an integer Ai. The jth bit in the binary representation of Ai represents whether taste j is available in this meal (1 means you will feel the jth taste while eating it, and 0 means you will not).
You feel taste j if you eat some meals such that at least one of them has bit j equal to 1. In other words, the tastes you get by eating meals Ai and Aj are the tastes of the number Ai | Aj, where | is the bitwise OR operation.
Fouad wants to eat many meals so that he can enjoy the food as much as possible, but he cannot eat more than K meals because of his strict diet. Since not all tastes are the same, he asks for your help to select a subset of at most K meals such that the number representing the overall food is as large as possible (the bitwise OR of the chosen meals is as large as possible).
Input
The first line of the input contains a single integer T, the number of test cases.
Each test case starts with a line containing two space-separated integers N, K (where 20 ≤ K ≤ N ≤ 105).
The second line of the test case contains N space-separated integers A1, ..., AN (where for all i we have 0 ≤ Ai ≤ 106).
Output
For each test case, print a single line containing a single integer X representing the maximum value of tastes Fouad can feel by eating a subset of at most K meals. If the subset of meals Fouad will eat contains the ith taste, then the ith bit of the number X must be 1; otherwise, the ith bit must be 0.
Hint
In the sample, he can eat all the meals, so the result is the bitwise OR of all the given numbers.