#P9696. Maximizing Bitwise AND Split Sum with One Swap
Maximizing Bitwise AND Split Sum with One Swap
Maximizing Bitwise AND Split Sum with One Swap
Given a non‐negative integer sequence \( A = a_1, a_2, \dots, a_n \) of length \( n \), define
\[ F(A)=\max_{1\le k<n}\Bigl((a_1\,\&\,a_2\,\&\,\cdots\,\&\,a_k)+(a_{k+1}\,\&\,a_{k+2}\,\&\,\cdots\,\&\,a_n)\Bigr) \]
where \( \& \) is the bitwise-and operator.
You are allowed to perform at most one swap operation. That is, you may choose two indices \( i \) and \( j \) with \( 1 \le i < j \le n \) and swap \( a_i \) with \( a_j \).
Your task is to compute the maximum possible value of \( F(A) \) after performing at most one swap.
inputFormat
The first line contains a single integer \( n \) (\( n\ge2 \)), the length of the sequence.
The second line contains \( n \) non-negative integers \( a_1, a_2, \dots, a_n \) separated by spaces.
outputFormat
Output a single integer, representing the maximum possible value of \( F(A) \) after performing at most one swap.
sample
3
1 2 3
3
</p>