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)

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2050;
const int K = 46;
const int M = 1e9;
const int mod = 1e9 + 7;
int n;
int a[N], b[N];
int solve(int x, int y, int bt){
    x = x ^ (x & (1LL << (bt + 1)));
    y = y ^ (y & (1LL << (bt + 1)));
    int b1 = (1 & (x >> bt));
    int b2 = (1 & (y >> bt));
   // cout << x << " " << y << " " << bt << " " << b1 << " " << b2 << " " << b[bt] << endl;
    if (bt == 0){
        if (x == y && x == b[bt]){
            return 0;
        } else
        if (x != y && b[bt] == 2){
            return 0;
        } else{
            return 1e14;
        }
    }
    if (b[bt] == 0){
        if (b1 == 0 && b2 == 0) return solve(x, y, bt - 1);
        if (b2 == 1) return 1e14;
        if (b1 == 1){
            int s = x - (1LL << bt) + 1;
            if (!(1 & ((s + y) >> bt))) return s + solve(x - s, y + s, bt - 1);
            else return 1e14;
        }
    } else
    if (b[bt] == 1){
        if (b1 == 1 && b2 == 1) return solve(x, y, bt - 1);
      //  cout << "!!" << endl;
        if (b1 == 0) return 1e14;
        int s = (1LL << bt) - y;
        if ((x > s) && (1 & ((x - s) >> bt))) return s + solve(x - s, y + s, bt - 1);
        else return 1e14;
    } else
    if (b[bt] == 2){
        if (b1 == 0 && b2 == 1) return solve(x, y, bt - 1);
        if (b1 == 0 && b2 == 0){
            int s = (1LL << bt) - y;
            if (s <= x){
                return s + solve(x - s, y + s, bt - 1);
            } else{
                return 1e14;
            }
        }
        if (b1 == 1 && b2 == 1){
            int s = x - (1LL << bt) + 1;
            int v1 = 1e14;
            int v2 = 1e14;
            if (1 & ((s + y) >> bt)) v1 = s + solve(x - s, y + s, bt - 1);
           // cout << "s = " << s << " v1 = " << v1 << " " << x - s << " " << y + s << " " << bt - 1 << endl;
          //  s = (1LL << (bt + 1)) - y;
          //  if (x >= s && (1 & ((x - s) >> bt))) v2 = s + solve(x - s, 0, bt - 1);
            return v1;
        }
        if (b1 == 1 && b2 == 0){
            int v1 = solve(x, y, bt - 1);
            //cout << "v1 = " << v1 << " " << x << " " << y << endl;
            int s = x - (1LL << bt) + 1;
            int v2 = 1e14;
            if ((1 & ((s + y) >> bt)) && (s != (1 << bt))) v2 = s + solve(x - s, y + s, bt - 1);
            return min(v1, v2);
        }
    }
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
         cin >> a[i];
    }
    int x = 0;
    for (int i = 3; i <= n; i++) x ^= a[i];
    int s = a[1] + a[2];
    for (int i = 0; i < K; i++){
        if ((1LL << i) & x) b[i] = 2;
        else{
            if ((1LL << (i + 1)) & (s - x)) b[i] = 1;
        }
    }
 //   for (int i = 0; i < K; i++) cout << b[i] << " ";
  //  cout << endl;
    int ans = solve(a[1], a[2], K - 1);
    if (ans >= a[1]){
        cout << -1;
    } else{
        cout << ans << endl;
      //  cout << ((a[1] - ans) ^ (a[1] + ans) ^ x) << endl;
       // cout << solve(3, 0, 1) << endl;
    }
}