You are on page 1of 2

Binary Lock

Input file: standard input


Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

(This story is continued from the problem chickensoup. It is recommended you read the story of
chickensoup first.)
It turns out that Chicken isn’t very good at making chicken soup. In fact, the chicken soup prepared by
Chicken was so bad that it caused all the IO mafia members to faint in disgust after trying it. Knowing
that he wouldn’t be spared once they wake up, he runs away and tries to escape the secret IO mafia
hideout he is currently located in. With a stroke of luck, he has managed to find the exit. Unfortunately,
the door is secured with the newest, most secure lock in town: The Binary Lock (which made the Rotary
Lock obsolete)! Not only is cracking the Binary Lock as sanity-draining as the Rotary Lock, it also requires
a good amount of bitwise operation knowledge (which most people do not have).
The Binary Lock consists of N integers from 0 to 230 − 1 inclusive. In each operation, one can enter an
integer x from 0 to 230 − 1 inclusive, causing each integer Ai to be replaced with Ai ⊕ x (⊕ denotes
bitwise XOR). The lock is unlocked if all numbers on the lock have the same number of 1s in their
binary representations. As entering the number is a tedious process, Chicken only has enough time to do
1 operation. Is it possible for him to escape?

Input
The first line of input contains an integer N .
The second line of input contains N integers, representing the initial numbers on the lock.

Output
If there exists a number x (from 0 to 230 − 1 inclusive) that Chicken can enter into the lock to unlock it,
output x. Note that there might be multiple values of x that work.
Otherwise Chicken will be turned into chicken soup. Output -1.

Scoring
It is guaranteed that

• 2 ≤ N ≤ 100
• 0 ≤ Ai ≤ 230 − 1
• All values in input are integers

Subtask Score Constraints


1 100 No additional constraints
2 0 Sample Testcases

Examples
standard input standard output
2 1
7 2
3 -1
1 2 3
4 5
3 17 6 0

Page 1 of 2
Note
For the first sample testcase, 7 ⊕ 1 = 6 which has a binary representation of 110 while 2 ⊕ 1 = 3 which
has a binary representation of 11. Both have 2 1s in their binary representation.

Page 2 of 2

You might also like