P4098 [HEOI2013] ALO
Description
Welcome to ALO (Arithmetic and Logistic Online). This is a VR MMORPG. As the name suggests, it is full of math puzzles.
You have $n$ gems. The $i$-th gem has an energy density $a_i$, and all energy densities are pairwise distinct. You may select a contiguous segment of gems (with at least two gems) to fuse. Suppose their energy densities are $a_i, a_{i+1}, \cdots, a_j$. The fused gem’s energy density is the maximum value of the bitwise XOR between the second largest energy density in this segment and any other gem in the same segment. That is, if the second largest in this segment is $k$, the generated energy density equals $\max\{k \oplus a_p \mid a_p \ne k,\ i \le p \le j\}$.
Find how to choose the segment to maximize the fused gem’s energy density.
Input Format
The first line contains an integer $n$, the number of gems.
The second line contains $n$ integers, $a_1$ through $a_n$, the energy density of each gem. It is guaranteed that for $i \ne j$ we have $a_i \ne a_j$.
Output Format
Output one integer, the maximum possible fused gem energy density.
Explanation/Hint
### Sample Explanation
Choose the segment $[1, 5]$. The maximum is $7 \oplus 9 = 14$.
### Constraints
- For $20\%$ of the testdata, $n \le 100$.
- For $50\%$ of the testdata, $n \le 2000$.
- For $100\%$ of the testdata, $1 \le n \le 50000$, $0 \le a_i \le 10^9$.
2023-04-28: Added two hack testdata, not scored.
Translated by ChatGPT 5