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# (Mono-csc 3.5.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
  class Program
  {
    static void Main(string[] args)
    {
      ABC172.F();
    }
    class ABC172
    {
      public static void A()
      {
        var A = Read.Int();
        Console.WriteLine(A+A*A+A*A*A);
      }
      public static void B()
      {
        var S = Read.Str();
        var T = Read.Str();
        int count = 0;
        for(int i = 0; i < S.Length; ++i)
        {
          if (S[i] != T[i]) { count++; }
        }
        Console.WriteLine(count);
      }
      public static void C()
      {
        var N = Read.Int();
        var M = Read.Int();
        var K = Read.Long();
        var A = Read.LongN();
        var B = Read.LongN();
        var SA = Util.ASum(A);
        var SB = Util.ASum(B);
        int max = 0;
        int right = M;
        for (int i = 0; i <= N; ++i)
        {
          if (SA[i] > K) { break; }
          while (right > 0 && SA[i] + SB[right] > K) { right--; }
          max = Math.Max(max, i + right);
        }
        Console.WriteLine(max);
      }
      public static void D()
      {
        var N = Read.Int();
        long sum = 0;
        for(long i = 1; i <= N; ++i)
        {
          var d = N / i;
          sum += i * d * (d + 1) / 2;
        }
        Console.WriteLine(sum);
      }
      public static void E()
      {
        var N = Read.Int();
        var M = Read.Int();

        var comb = Util.Combin(500010, MOD.P);
        var fact = new long[500001];
        fact[0] = 1;
        for(int i = 1; i <= 500000; ++i)
        {
          fact[i] = (fact[i - 1] * i) % MOD.P;
        }
        MOD ans = 0;
        //for(int )
      }
      public static void F()
      {
        var N = Read.Int();
        var A = Read.LongN();
        long res = 0;
        for(int i = 2; i < N; ++i)
        {
          res ^= A[i];
        }
        var sum = A[0] + A[1];
        long only1 = 0;
        for(int i = 0; i < 50; ++i)
        {
          if((res & (1L << i)) > 0)
          {
            only1 += 1L << i;
          }
        }
        if (only1 > sum)
        {
          Console.WriteLine(-1); return;
        }
        long both = sum - only1;
        if (both % 2 > 0) { Console.WriteLine(-1); return; }
        var both1 = new bool[50];
        for (int i = 49; i >= 0; --i)
        {
          if ((1L << i) <= both / 2 && both / 2 < (1L << i) * 2)
          {
            both1[i] = true;
            both -= (1L << i) * 2;
          }
        }
        if (both > 0) { Console.WriteLine(-1); return; }
        long result1 = 0;
        for(int i = 0; i < 50; ++i)
        {
          if (both1[i]) { result1 += 1L << i; }
        }
        var SUM1 = new long[51];
        for (int i = 0; i < 50; ++i)
        {
          SUM1[i + 1] = SUM1[i] + ((res & (1L << i)) > 0 ? (1L << i) : 0);
        }
        if (result1 + SUM1[50] < A[1]) { Console.WriteLine(-1); return; }
        for (int i = 49; i >= 0; --i)
        {
          if (((res & (1L << i)) > 0))
          {
            var diff = result1 - A[1];
            if (diff < 0 && SUM1[i] < -diff)
            {
              result1 += 1L << i;
            }
          }
        }
        if (result1 >= sum) { Console.WriteLine(-1); return; }
        Console.WriteLine(result1 - A[1]);
      }
    }

    class Util
    {
      public static T[,] Gen<T>(int r, int c, T v)
      {
        var matrix = new T[r, c];
        for (int i = 0; i < r; ++i)
        {
          for (int j = 0; j < c; ++j)
          {
            matrix[i, j] = v;
          }
        }
        return matrix;
      }
      public static long[] ASum(long[] array)
      {
        var res = new long[array.Length + 1];
        for (int i = 0; i < array.Length; ++i)
        {
          res[i + 1] = res[i] + array[i];
        }
        return res;
      }
      public static long[,] ASum(long[,] matrix)
      {
        var N = matrix.GetLength(0);
        var M = matrix.GetLength(1);
        var res = new long[N + 1, M + 1];
        for(int i = 0; i < N; ++i)
        {
          for(int j = 0; j < M; ++j)
          {
            res[i + 1, j + 1] = res[i, j + 1] + res[i + 1, j] - res[i, j] + matrix[i, j];
          }
        }
        return res;
      }
      public static Dictionary<T, long> Frequency<T>(T[] array)
      {
        var dict = new Dictionary<T, long>();
        foreach (var a in array)
        {
          if (!dict.ContainsKey(a)) { dict.Add(a, 0); }
          dict[a]++;
        }
        return dict;
      }

      public static long Power(long a, long n, long p)
      {
        long res = 1;
        long mult = a;
        for (int i = 0; i < 63; ++i)
        {
          if ((n & ((long)1 << i)) > 0)
          {
            res = (res * mult) % p;
          }
          mult = (mult * mult) % p;
        }
        return res;
      }
      public static long Inverse(long a, long p)
      {
        long b = p, c = 1, d = 0, e = 0, f = 0;
        while (b != 0)
        {
          e = a / b;
          f = b; b = a - e * b; a = f;
          f = d; d = c - e * d; c = f;
        }
        c %= p;
        return c + (c < 0 ? p : 0);
      }

      public static long[] Facts(int n, long p)
      {
        var facts = new long[n + 1];
        facts[0] = 1;
        for (int i = 1; i <= n; ++i)
        {
          facts[i] = (i * facts[i - 1]) % p;
        }
        return facts;
      }
      public static Func<int, int, long> Combin(int n, long P)
      {
        var fact = Facts(n, P);
        var inv = fact.Select(x => Inverse(x, P)).ToArray();
        return (N, M) =>
        {
          if (N < 0 || N < M || M < 0) { return 0; }
          return (((fact[N] * inv[M]) % P) * inv[N - M]) % P;
        };
      }
    }

    struct MOD
    {
      public static long P = 1000000007;
      public readonly long V;

      public MOD(long a) { this.V = a % P; }
      public static implicit operator MOD(long a) { return new MOD(a); }
      public static MOD operator +(MOD a, MOD b) { return new MOD((a.V + b.V) % P); }
      public static MOD operator -(MOD a, MOD b) { return new MOD((a.V - b.V + P) % P); }
      public static MOD operator *(MOD a, MOD b) { return new MOD((a.V * b.V) % P); }
      public static MOD operator /(MOD a, MOD b) { return new MOD((a.V * b.Inverse().V) % P); }
      public MOD Inverse()
      {
        long a = V, p = P; long b = p, c = 1, d = 0, e = 0, f = 0;
        while (b != 0) { e = a / b; f = b; b = a - e * b; a = f; f = d; d = c - e * d; c = f; }
        c %= p;
        return c + (c < 0 ? p : 0);
      }
      public MOD Power(long n)
      {
        long a = V, p = P; long res = 1; long mult = a;
        for (int i = 0; i < 63; ++i) { if ((n & ((long)1 << i)) > 0) { res = (res * mult) % p; } mult = (mult * mult) % p; }
        return res;
      }
      public override string ToString() { return V.ToString(); }
    }

    static class Read
{
static Queue<string> queue = new Queue<string>();
static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
public static ulong[] ULongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToUInt64(s)).ToArray(); }
public static string[] StrN() { return Console.ReadLine().Split(' '); }
public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; }
public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
{
Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
        for (int i = 0; i < M; ++i) { var ab = Read.IntN(); if (!dict[ab[0]].Contains(ab[1])) { dict[ab[0]].Add(ab[1]); } if (!isDirectional && !dict[ab[1]].Contains(ab[0])) { dict[ab[1]].Add(ab[0]); } }
        return dict;
}
public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
{
Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); }
for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); dict[ab[1]].Add(ab[0], ab[2]); }
return dict;
}
}
    }
}