Editorial
Let be the OR value of a chosen non-empty set. We want the maximum possible such that .
Consider integer comparison in binary. For , one of the following must hold.
- .
- At the highest bit where and differ, has and has , while all higher bits are equal.
In the second case, lower bits below can be arbitrary. Therefore, every value is a bitwise submask of one of the following masks.
- itself.
- A value obtained by changing one set bit of to and changing all lower bits to .
Call such a candidate mask . For the final OR value to be a submask of , every chosen must also be a submask of . This is equivalent to
For a fixed , if we OR all values satisfying this condition, the result is still a submask of , so it is at most and therefore at most . Also, bitwise OR never decreases when more values are chosen, so choosing all usable values is optimal for that .
Thus, for every candidate mask , compute
The answer is the maximum among candidates where at least one value was usable. If no candidate allows any value, print .
There are at most candidate masks, and for each one we scan all values. The time complexity is . Apart from the input array, the extra memory usage is .