Problem:
Score : 600 points

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

Aoki and Takahashi are about to use them to play the following game:

Starting with Aoki, the two players alternately do the following operation:

Operation: Choose one pile of stones, and remove one or more stones from it.

When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

2 \leq N \leq 300

1 \leq A_i \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.

Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.

Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>

using namespace __gnu_pbds;
using namespace std;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pi pair<ll,ll>
#define pii pair<ll,pi >
#define ppi pair<pi,ll>
#define ppp pair<pi,pi >
#define ex(str,a,b) str.substr((int)(a),min((int)str.size()-a,(int)(b-a+1))) 
#define all(X) X.begin(),X.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// for pair comparison function(ascending order) use return (i1.ff < i2.ff);

/* string operations :
   str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
   __builtin_popcount(n) : no. of set bits in n.
*/

const int M = (1<<20)+5;
const int md = 1e9+7;

priority_queue<ll,vector<ll >,greater<ll > > pq;

ll pwr(ll a,ll n,ll m)
{
	ll p=1;
	while(n>0)
	{
		if(n%2==1)
			p=(p*a)%m;
		a=(a*a)%m;
		n=n/2;
	}
	return p;
}

ll ar[1000];
vector<ll> adj;

bool check(ll f,ll s,ll v)
{
	f = f+(1ll<<v);
	if(f<=ar[1])
		return 1;
	else
		return 0;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	ll n,i,x = 0,s,a,f = 0;

	cin >> n;
	for(i=1;i<=n;++i)
	{
		cin >> ar[i];
		if(i>=3)
			x = x^ar[i];
	}
	
	if((ar[1]^ar[2]^x) == 0 )
	{
		cout << 0 << "\n";
		return 0;
	}

	for(i=1;i<min((ll)(1e7),ar[1]);++i)
	{
		if(((ar[1]-i)^(ar[2]+i)^x) == 0)
		{
			cout << i << "\n";
			return 0;
		}
	}

	s = ar[1]+ar[2];
	if(s < x || (s-x)%2 == 1)
	{
		cout << -1 << "\n";
		return 0;
	}
	a = (s-x)/2;
	s = 0;
	i = 0;
	while(a>0 || x>0)
	{
		if(a%2 == 1 && x%2 == 1)
		{
			cout << -1 << "\n";
			return 0;
		}
		else if(a%2 == 1 && x%2==0)
		{
			f = f+(1ll<<i);
			s = s+(1ll<<i);
		}
		else if(a%2 == 0 && x%2 == 1)
			adj.pb(i);
		
		i++;
		a = a/2;
		x = x/2;
	}

	reverse(all(adj));
	for(auto v:adj)
	{
		if(check(f,s,v))
			f = f+(1ll<<v);
		else
			s = s+(1ll<<v);
	}

	if(f>0)
		cout << ar[1]-f << "\n";
	else
		cout << -1 << "\n";
}