Under construction

Still being built. Things may change

Why Hobawoo Was Late for School 6

Time limit1sMemory limit1024 MB

Summary
Shrink an array of N non-negative integers to exactly M elements using four XOR-merge spells, maximizing the sum of the final elements.
Level

Hard8 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

Hobawoo is almost at the Demon King's throne, but the door to the throne is locked and he cannot get in.

To open the door, a magic key of length NN must be turned into a magic key of length exactly MM, and the power of the key must be made as large as possible.

A magic key of length ℓ\ell has a sequence of ℓ\ell non-negative integers a1,a2,a3,⋯ ,aℓa_{1}, a_{2}, a_{3}, \cdots, a_{\ell}. The power of the key is the sum of the numbers in the sequence.

For a key of length ℓ\ell, you can shorten it by applying one of the following 4 spells.

  1. Remove a1a_{1} and a2a_{2}, then add a1⊕a2a_{1}\oplus a_{2} to the front, producing a key of length ℓ−1\ell-1. The sequence changes as (a1,a2,a3,⋯ ,aℓ)⇒(a1⊕a2,a3,⋯ ,aℓ)(a_{1}, a_{2}, a_{3}, \cdots, a_{\ell}) \Rightarrow (a_{1}\oplus a_{2}, a_{3}, \cdots, a_{\ell}).
  2. Remove aℓ−1a_{\ell-1} and aℓa_{\ell}, then add aℓ−1⊕aℓa_{\ell-1}\oplus a_{\ell} to the back, producing a key of length ℓ−1\ell-1. The sequence changes as (a1,⋯ ,aℓ−2,aℓ−1,aℓ)⇒(a1,⋯ ,aℓ−2,aℓ−1⊕aℓ)(a_{1}, \cdots, a_{\ell-2}, a_{\ell-1}, a_{\ell}) \Rightarrow (a_{1}, \cdots, a_{\ell-2}, a_{\ell-1}\oplus a_{\ell}).
  3. Remove a1,a2,a3,a4a_{1}, a_{2}, a_{3}, a_{4}, then add a2⊕a3,a1⊕a4a_{2}\oplus a_{3}, a_{1}\oplus a_{4} to the front, producing a key of length ℓ−2\ell-2. The sequence changes as (a1,a2,a3,a4,a5,⋯ ,aℓ)⇒(a2⊕a3,a1⊕a4,a5,⋯ ,aℓ)(a_{1}, a_{2}, a_{3}, a_{4}, a_{5}, \cdots, a_{\ell}) \Rightarrow (a_{2}\oplus a_{3}, a_{1}\oplus a_{4}, a_{5}, \cdots, a_{\ell}).
  4. Remove aℓ−3,aℓ−2,aℓ−1,aℓa_{\ell-3}, a_{\ell-2}, a_{\ell-1}, a_{\ell}, then add aℓ−3⊕aℓ,aℓ−2⊕aℓ−1a_{\ell-3}\oplus a_{\ell}, a_{\ell-2}\oplus a_{\ell-1} to the back, producing a key of length ℓ−2\ell-2. The sequence changes as (a1,⋯ ,aℓ−4,aℓ−3,aℓ−2,aℓ−1,aℓ)⇒(a1,⋯ ,aℓ−4,aℓ−3⊕aℓ,aℓ−2⊕aℓ−1)(a_{1}, \cdots, a_{\ell-4}, a_{\ell-3}, a_{\ell-2}, a_{\ell-1}, a_{\ell}) \Rightarrow (a_{1}, \cdots, a_{\ell-4}, a_{\ell-3}\oplus a_{\ell}, a_{\ell-2}\oplus a_{\ell-1}).

Input

The first line contains NN and MM separated by a space. (4≤M≤N≤3 0004 \le M \le N \le 3\,000)

The second line contains NN numbers a1,a2,a3,⋯ ,aNa_{1}, a_{2}, a_{3}, \cdots, a_{N} separated by spaces. (0≤ai≤1090 \le a_{i} \le 10^{9})

Output

Print the maximum power among the keys of length MM that can be made.

Hint

⊕\oplus is the bitwise XOR operation, applied bit by bit.

Bitwise XOR

  • For each bit position of two numbers, the result bit is 11 if the two bits differ, and 00 if they are the same.
  • Example:

01102=6⊕ 11002=1210102=10\begin{aligned} 0110_{2} &= 6 \\ \oplus \ 1100_{2} &= 12 \\ 1010_{2} &= 10 \end{aligned}

Examples2

  1. Example 1

    Input
    6 4
    1 2 3 4 5 6
    
    Expected output
    17
    
  2. Example 2

    Input
    8 5
    12 45 71 23 53 7 25 9
    
    Expected output
    217