bzoj#P3943. [USACO 2015 Feb] SuperBull

[USACO 2015 Feb] SuperBull

Description

Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer John is in charge of making the tournament as exciting as possible.

A total of NN teams are playing in the Superbull. Each team is assigned a distinct integer team ID to distinguish it from the other teams. The Superbull is an elimination tournament -- after every game, Farmer John chooses which team to eliminate from the Superbull, and the eliminated team can no longer play in any more games. The Superbull ends when only one team remains.

Farmer John notices a very unusual property about the scores in matches! In any game, the combined score of the two teams al ways ends up being the bitwise exclusive OR (XOR) of the two team IDs. For example, if teams 1212 and 2020 were to play, then 2424 points would be scored in that game, since 01100 xor 10100=1100001100\text{ xor }10100 = 11000.

Farmer John believes that the more points are scored in a game, the more exciting the game is. Because of this, he wants to choose a series of games to be played such that the total number of points scored in the Superbull is maximized. Please help Farmer John organize the matches.

Input Format

The first line contains the single integer NN.

The following NN lines contain the NN team IDs.

Output Format

Output the maximum possible number of points that can be scored in the Superbull.

4
3
6
9
10
37

Limit and Hint

For 100%100\% of testcases, 1N20001\le N\le 2000 and all ID xx satisfy 1x23011\le x\le 2^{30}-1.

Source

USACO February 2015 Contest Silver T3