A total of N (1 <= N <= 2000) teams are playing in the Superbull. Each team has a distinct integer team ID in the range 1...2^30-1. The Superbull is an elimination tournament -- after every game, Farmer John chooses which team to eliminate, and the eliminated team can no longer play. The Superbull ends when only one team remains.
In any game, the combined score of the two teams always ends up being the bitwise exclusive OR (XOR) of the two team IDs. FJ wants to choose a series of games such that the total number of points scored in the Superbull is maximized. Please help Farmer John organize the matches.
Input
Line 1: N. The following N lines contain the N team IDs.
Output
The maximum possible number of points that can be scored in the Superbull.
Example 1
4 3 6 9 10
37
Output details: (3 XOR 9) + (6 XOR 9) + (6 XOR 10) = 10 + 15 + 12 = 37.
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.