You need to smuggle n gems from Algoville to Byteland. Initially, Byteland has no gems. There's a heavily supervised checkpoint at the border, so you'll need to bribe the guards.
Your plan is as follows: first, smuggle a single gem from Algoville to Byteland without being noticed. After this initial trip, every subsequent journey from Byteland to Algoville will require bribing the guards to gain entry, and you will return with more gems.
The guards have devised a clever system to maximize their earnings. Each time you travel from Byteland to Algoville, you must bring a gem with you to deposit with them as a safety measure. You don't pay anything at this point. Then, you will collect exactly one new gem from Algoville and return to Byteland. Upon your return journey, the guards will give back a bribe equal to the absolute difference between the bitwise XOR and bitwise AND of the prices of the two gems (the deposited gem and the new gem collected).
Given an array p1, p2, ..., pn representing the prices of the n gems, determine the minimum total bribe you must pay the guards to successfully smuggle all n gems across the border.