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>
#define lli long long int
#define pb push_back
#define mod 1000000007
#pragma GCC optimize ("-O2")
#define mod2 998244353
#define MAXN 1000000000
#define v32 vector<int>
#define v64 vector<lli>
#define v1024 vector <vector <int>>
#define v4096 vector <vector <lli>>
#define vt vector
#define f(x, y, z) for (lli x = y; x < z; x++)
#define fd(x, y, z) for (lli x = y; x > z; x--)
#define lb lower_bound
#define ld long double
#define m64 map<lli,lli>
#define m32 map<int,int>
#define m64it map<lli,lli>::iterator
#define m32it map<int,int>::iterator
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ist insert
#define endl "\n"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> 
#define p_q priority_queue 
#define min_p_q priority_queue <pair<lli,int>,vt <pair<lli,int>>,greater <pair<lli,int>>>
using namespace std;
using namespace __gnu_pbds; 

//USE AT YOUR OWN RISK
lli tmod(lli x,lli m=mod){return (x%m+m)%m;}

template <typename T> 
void DEBUG_ARR(vt<T> a,char c)
{
	f(i,0,a.size())cout<<a[i]<<c;
	cout<<"\n";
}

//make sure x is below mod
lli power(lli x, lli y,lli m=mod) 
{
    lli res = 1;  
   
    while (y > 0) 
    { 
        if (y & 1) 
            res = (res*x)%m; 
        y = y>>1; 
        x = (x*x)%m;  
    }
    if(res<0)
       res+=m; 
    return res; 
}

lli fact[2000009];
lli ncr(lli n,lli r){
    lli ans=fact[n];
    if(r>n)return 1;
    if(r<0)return 0;
    if(n<0)return 0;
    ans=(ans*power(fact[n-r],mod-2))%mod;
    ans=(ans*power(fact[r],mod-2))%mod;
    return ans;
}
void normalize(lli &a,lli &b){
    if(a==0 && b==0)return;
    if(a<0){
        a=-a;
        b=-b;
    }
    else if(a==0){
        if(b<0)b=-b;
    }
    lli g=__gcd(abs(a),abs(b));
    if(g!=0){
        a/=g;
        b/=g;
    }
}
const int N=2e5+5;

//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// int smallestprime[(int)1e7+10];
// int nodivisors[(int)1e7+5];
int main() 
{ 
    fact[0]=1;
    f(i,1,2000001)fact[i]=(i*fact[i-1])%mod;
    // f(i,2,10000001){
    //     if(smallestprime[i]==0)
    //     for(lli j=1;j*i<=10000000;j++){
    //         if(smallestprime[j*i]==0)
    //         smallestprime[j*i]=i;
    //     }
    // }
    // nodivisors[1]=1;
    fastio;
    int t=1;
    // cin>>t;
    // int temp=t;
    // cout<<smvalallestprime[5]<<"\n";
	while(t--){
        // cout<<"Case #"<<temp-t<<": ";
        // clock_t t=clock();
        // ifstream in("cave.in");
        // ofstream out("cave.out");
        lli n;cin>>n;
        lli a[n];f(i,0,n)cin>>a[i];
        lli xorx=0;
        f(i,2,n)xorx^=a[i];
        // cout<<a[0]<<" "<<a[1]<<" "<<xorx<<"\n";
        if((a[0]+a[1]-xorx)%2==0 && a[0]+a[1]>=xorx){
            lli val1=0,val2=0;
            lli aval=(a[0]+a[1]-xorx)/2;
            fd(i,59,-1){
                lli ca,cx;
                if((((1ll)<<i)&aval))ca=1;
                else ca=0;
                if((((1ll)<<i)&xorx))cx=1;
                else cx=0;
                if(ca==0 && cx==0){
                    continue;
                }
                else if(ca==1 && cx==0){
                    val1+=((1ll)<<i);
                    val2+=((1ll)<<i);
                }
                else if(ca==0 && cx==1){
                    continue;
                }
                else{
                    cout<<-1<<"\n";
                    return 0;
                }
                // cout<<val1<<" sd f\n";
            }
            fd(i,59,-1){
                lli ca,cx;
                if((((1ll)<<i)&aval))ca=1;
                else ca=0;
                if((((1ll)<<i)&xorx))cx=1;
                else cx=0;
                if(ca==0 && cx==1){
                    if(((1ll)<<i)+val1<=a[0])val1+=(1ll)<<i;
                    else val2+=(1ll)<<i;
                }
            }
            // cout<<val1<<"\n";
            if(val1==0){
                cout<<"-1\n";
                return 0;
            }
            cout<<a[0]-val1<<"\n";
        }
        else  cout<<-1<<"\n";
        // cout<<clock()-t<<"\n";
    }   
    return 0; 	
}