Why Hobawoo Was Late for School 6
Time limit1sMemory limit1024 MB
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 must be turned into a magic key of length exactly , and the power of the key must be made as large as possible.
A magic key of length has a sequence of non-negative integers . The power of the key is the sum of the numbers in the sequence.
For a key of length , you can shorten it by applying one of the following 4 spells.
- Remove and , then add to the front, producing a key of length . The sequence changes as .
- Remove and , then add to the back, producing a key of length . The sequence changes as .
- Remove , then add to the front, producing a key of length . The sequence changes as .
- Remove , then add to the back, producing a key of length . The sequence changes as .
Input
The first line contains and separated by a space. ()
The second line contains numbers separated by spaces. ()
Output
Print the maximum power among the keys of length that can be made.
Hint
is the bitwise XOR operation, applied bit by bit.
Bitwise XOR
- For each bit position of two numbers, the result bit is if the two bits differ, and if they are the same.
- Example: