Superbull
Time limit1sMemory limit256 MB
Pick N minus 1 pairings that connect all team IDs into one group so the sum of pairwise XOR values is as large as possible.
- Level
Medium6 of 10
- Topics
- Minimum spanning tree, Graph, Bit manipulation
- Solved
- No attempts yet
Problem
Bessie and her friends play hoofball in the annual Superbull championship, and Farmer John runs the tournament. He wants it to be as exciting as possible.
teams enter the Superbull. Each team gets an integer ID between and , and no two teams share an ID. The Superbull is an elimination tournament. After each game Farmer John picks which of the two teams that just played is eliminated, and an eliminated team plays no further games. The tournament ends when one team remains.
Farmer John noticed an unusual rule about the scores. In every game, the combined score of the two teams equals the bitwise exclusive or (XOR) of their IDs. For example, if the team with ID plays the team with ID , that game produces points, because XOR .
Farmer John considers a game more exciting when it produces more points, so he wants to arrange the games so that the total number of points scored in the whole tournament is as large as possible. He can freely choose which two teams meet in each game and which of them is eliminated. Help him find the largest possible total.
Input
The first line contains the number of teams . ()
Each of the next lines contains one team ID. The IDs are distinct integers between and .
Output
Print the maximum total number of points that can be scored in the Superbull, on one line.
Note
The bitwise exclusive or (XOR) compares two integers bit by bit in binary. A result bit is when exactly one of the two input bits is , and when both are or both are . For example, (decimal ) XOR (decimal ) (decimal ). Many languages write this operation with the ^ operator.