#P10512. Maximum Bitwise AND After Merging Operations
Maximum Bitwise AND After Merging Operations
Maximum Bitwise AND After Merging Operations
Given a sequence of n nonnegative integers $$a_1, a_2, \ldots, a_n$$, you are allowed to perform exactly k operations. In each operation, you choose an index $$i$$ (where $$1 \le i < n$$ at that moment) and merge the two adjacent numbers into one by taking their bitwise OR. Formally, in one operation you replace the subsequence
$$a_1, a_2, \ldots, a_i, a_{i+1}, \ldots, a_n$$
with
$$a_1, a_2, \ldots, (a_i \lor a_{i+1}), a_{i+2}, \ldots, a_n.$$
After exactly k operations, the sequence will have n - k numbers. Let the resulting numbers be $$S_1, S_2, \ldots, S_{n-k}$$ (each being the bitwise OR of a contiguous subsequence of the original sequence). Your task is to maximize the value of the bitwise AND of all these numbers, i.e., maximize
$$S_1 \land S_2 \land \cdots \land S_{n-k}.$$
Determine the maximum possible value of this bitwise AND.
inputFormat
The first line contains two integers n and k (n is the length of the sequence and k is the number of operations).
The second line contains n nonnegative integers: $$a_1, a_2, \ldots, a_n$$, representing the sequence.
outputFormat
Output a single integer: the maximum possible value of the bitwise AND of the n - k numbers after performing exactly k operations.
sample
3 1
1 2 3
3
</p>