Choose B in [0, N-1] to XOR every element of A, then find the maximum possible count of index pairs i < j with C_i < C_j.
You are given an integer NNN and a sequence AAA of length MMM. NNN is a power of two, and every element of AAA is an integer between 000 and N−1N-1N−1.
You may pick one integer BBB between 000 and N−1N-1N−1 and build a new sequence CCC. For every iii, set Ci=Ai⊕BC_i = A_i \oplus BCi=Ai⊕B, where ⊕\oplus⊕ is bitwise exclusive or (XOR).
Then count the pairs (i,j)(i, j)(i,j) with i<ji < ji<j and Ci<CjC_i < C_jCi<Cj in CCC.
Write a program that finds the largest possible number of such pairs over every choice of BBB.
The first line contains NNN. (2≤N≤2302 \le N \le 2^{30}2≤N≤230, NNN is a power of two)
The second line contains the size MMM of the sequence AAA. (2≤M≤1310722 \le M \le 1310722≤M≤131072)
The third line contains A1,A2,…,AMA_1, A_2, \dots, A_MA1,A2,…,AM separated by spaces. (0≤Ai≤N−10 \le A_i \le N-10≤Ai≤N−1)
Print the largest possible number of pairs (i,j)(i, j)(i,j) with i<ji < ji<j and Ci<CjC_i < C_jCi<Cj.
For N=4N = 4N=4 and A=[3,2,1,0,3,2]A = [3, 2, 1, 0, 3, 2]A=[3,2,1,0,3,2], choosing B=3B = 3B=3 gives C=[0,1,2,3,0,1]C = [0, 1, 2, 3, 0, 1]C=[0,1,2,3,0,1]. That sequence has 8 pairs with i<ji < ji<j and Ci<CjC_i < C_jCi<Cj.