XOR Necklace
InterviewTime limit3sMemory limit1024 MB
Given a necklace of N beads, remove some beads to maximize the sum of XORs between each pair of adjacent beads around the cycle.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Frograms, Inc. has been developing necklaces with a new design and feature. Every programmer at Frograms now wears one. A necklace is made of several beads strung on a thread. Each bead has an integer from to (inclusive).

Most programmers left for summer vacation, so only a few remain in the office today. They decided to play a game with their necklaces to decide who buys lunch. For each pair of consecutive beads, take the sum of their numbers. A programmer's score is the XOR () of all those sums. The programmer with the lowest score loses.
If there are beads with values , , , in order around the necklace, the score is:
You will cheat by removing zero or more beads to maximize the score. What is the maximum score you can get by removing some of the beads? You cannot reorder the beads, and the necklace must keep at least two beads.
Input
The input consists of test cases. The first line contains .
Each test case starts with a line containing a single integer (), the number of beads. The next line contains integers , , , , separated by single spaces.
Output
For each test case, print the maximum score you can get on a single line.