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>
using namespace std;

#define fa(x,v) for(auto x:v)
#define f(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define spc " "
#define endl '\n'
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(s) (ll)int(s.length())
#define sz(v) (ll)int(v.size())
#define MOD 1000000007

void Fast_IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);
    freopen("debug.out","w",stderr);
    #endif
} 

// Code Here

vector <ll> decToBase(ll n, ll b)
{
	vector <ll> ret;
	if(n == 0) {
		ret.pb(0);
		return ret;
	}
	while(n > 0) {
		ll rem = n % b;
		ret.pb(rem);
		n /= b;
	}
	reverse(ret.begin(), ret.end());
	return ret;
}

ll solve(ll v1, ll v2, ll res, ll n)
{
	rf(i,n-1,0) {
		if(res & (1LL << i)) {
			if((v1 & (1LL << i)) != (v2 & (1LL << i))) continue;
			if((v1 & (1LL << i)) == 0) {
				ll L = 1, G = 0;
				f(j,0,i-1) {
					if(v1 & (1LL << j)) {
						L += (1LL << j);
					}
					if(v2 & (1LL << j)) {
						G += (1LL << j);
					}
				}
				G = (1LL << i) - G;
				if(L <= G) return -1;
				v1 -= G;
				v2 += G;
				if(v1 <= 0) return -1;
				continue;
			}
			ll L = 1, G = 0;
			f(j,0,i-1) {
				if(v1 & (1LL << j)) {
					L += (1LL << j);
				}
			} 
			f(j,0,i) {
				if(v2 & (1LL << j)) {
					G += (1LL << j);
				}
			}
			G = (1LL << (i+1)) - G;
			if(L >= G) return -1;
			v1 -= L;
			v2 += L;
			if(v1 <= 0) return -1;
		}
		else {
			if((v1 & (1LL << i)) == (v2 & (1LL << i))) continue;
			if((v1 & (1LL << i)) == 0) return -1;
			ll L = 1, G = 0;
			f(j,0,i-1) {
				if(v1 & (1LL << j)) {
					L += (1LL << j);
				}
			}
			f(j,0,i-1) {
				if(v2 & (1LL << j)) {
					G += (1LL << j);
				}
			}
			G = (1LL << i) - G;
			if(L == G) return -1;
			if(L < G) {
				v1 -= L;
				v2 += L;
				if(v1 <= 0) return -1;
			}
			else {
				v1 -= G;
				v2 += G;
				if(v1 <= 0) return -1;
			}
		}
	}
	return v1;
}

int main()
{
    //Fast_IO();
    ll n; cin >> n;
    ll a[n]; f(i,0,n-1) cin >> a[i];
    ll rest = 0;
    f(i,2,n-1) rest ^= a[i];
    vector <ll> v1 = decToBase(a[0], 2);
    vector <ll> v2 = decToBase(a[1], 2);
    vector <ll> v3 = decToBase(rest, 2);
    ll maxn = max({ sz(v1), sz(v2), sz(v3) });
    ll ans = solve(a[0], a[1], rest, maxn);
    if(ans == -1) cout << ans;
    else cout << a[0] - ans;
    return 0;
}