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# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var n=sc.I;
		DateTime st=DateTime.Now;
		var a=sc.La;
		long p=0,d=0;
		for(int i = 0;i<n;i++) {
			p^=a[i];
			if(i>1){d^=a[i];}
		}
		int m=Binary(d,0).Length;
		var b0=a[0];
		var b1=a[1];
		var td=Binary(d,m);
		long inf=long.MaxValue;
		long ans=0,ans2=long.MaxValue;
		if(d==0){
			if((a[0]-a[1])%2==0&&a[0]>=a[1]){
				ans=(a[0]-a[1])/2;
				if(a[0]-ans>0){
					Console.WriteLine(Min(ans,ans2));
					return;
				}
			}
			Console.WriteLine(ans2!=inf?ans2:-1);
			return;
		}
		while(true){
			var t0=Binary(b0,m);
			var t1=Binary(b1,m);
			if(b0<=0){
				Console.WriteLine(ans2!=inf?ans2:-1);
				return;
			}
			if((b0^b1^d)==0){
				Console.WriteLine(Min(ans,ans2));
				return;
			}
			if(t1.Length>td.Length){
				Console.WriteLine(ans2!=inf?ans2:-1);
				return;
			}
			if(t0.Length>td.Length){
				var z=1L<<(t0.Length-1);
				z--;
				z=(b0&z)+1;
				Console.WriteLine("a {0}",z);
				b0-=z;
				b1+=z;
				ans+=z;
				continue;
			}
			for(int i = 0;i<m;i++) {
				long z=1L<<(m-i-1);
				if(td[i]=='1'){
					if(((b0^b1)&z)==0){
						var x=(b0&(z-1))+1;
						x=Min(x,(z-((z-1)&b1)));
						b0-=x;
						b1+=x;
						ans+=x;
						break;
					}
				}
				else{
					if(((b0^b1)&z)!=0){
						var x=(b0&(z-1))+1;
						x=Min(x,(z-((z-1)&b1)));
						b0-=x;
						b1+=x;
						ans+=x;
						break;
					}
				}
			}
		}
	}
	static string Binary(long a,int b){
		string s=Convert.ToString(a,2);
		return (b>s.Length?new string('0',b-s.Length):"")+s;
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}