Under construction

Still being built. Things may change

Merge Elements

Time limit1sMemory limit1024 MB

Summary
Given an array, merge adjacent elements with OR exactly K times, then find the maximum AND of the remaining N-K elements.
Level

Medium7 of 10

Topics
Bit manipulation, Greedy, Dynamic programming, Prefix sum
Solved
No attempts yet

Problem

You are given an array A=[A1,…,AN]A=[A_{1}, \dots, A_{N}] of NN non-negative integers. Perform the following operation KK times on AA.

  • Choose two adjacent elements that remain in the array, apply bitwise OR\textrm{OR} to them, and replace the two elements with the result. In other words, if the current length of the array is LL, choose an integer i(1≤i<L)i(1 \leq i < L) and change [A1,…,Ai,Ai+1,…,AL][A_1, \dots, A_i, A_{i+1}, \dots, A_{L}] to [A1,…,Ai∣Ai+1,…,AL][A_1, \dots, A_i \mid A_{i+1}, \dots, A_{L}]. Each operation reduces the length of the array by 11.

After KK operations, find the maximum possible value of the bitwise AND\textrm{AND} of all N−KN-K remaining elements.

Input

The first line contains NN and KK, separated by a space. (2≤N≤500 0002 \leq N \leq 500\,000; 1≤K≤N−11 \leq K \leq N-1)

The second line contains the elements A1,A2,…,ANA_1, A_2, \dots, A_{N}, separated by spaces. (0≤Ai≤1090 \leq A_{i} \leq 10^{9})

Output

Print the maximum value of the bitwise AND\textrm{AND} of the N−KN-K elements that remain after KK operations.

Hint

Bitwise operators work on each bit separately.

  • The bitwise AND\textrm{AND} operation (&\&) works on each bit of two numbers. The result bit is 11 only when both bits are 11, and 00 otherwise.

    • For example, 01012=5 00112=300012=1\begin{array}{rcl} 0101_2 & = & 5 \\ \\& \ 0011_2 & = & 3 \\ \hline 0001_2 & = & 1 \end{array}
  • The bitwise OR\textrm{OR} operation (∣|) works on each bit of two numbers. The result bit is 00 only when both bits are 00, and 11 otherwise.

    • For example, 01102=6 ∣ 11002=1211102=14\begin{array}{rcl} 0110_2 & = & 6 \\ \ | \ 1100_2 & = & 12 \\ \hline 1110_2 & = & 14 \end{array}

Examples1

  1. Example 1

    Input
    5 3
    1 4 2 5 1
    
    Expected output
    5