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 Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Main {
	private static final int MAX = 43;
	
	public Main() {
		FastScanner scanner = new FastScanner();
		int N = scanner.nextInt();
		
		long A = scanner.nextLong(), B = scanner.nextLong();
		long S = A + B;
		long X = 0;
		for (int i = 2; i < N; i++) {
			X ^= scanner.nextLong();
		}
		
		//桁数、繰り上がり、Aより大きいフラグ：aの最大値
		long[][][] DP = new long[MAX][2][2];
		
		for (int i = 0; i < MAX; i++) {
			for (int c = 0; c < 2; c++) {
				for (int f = 0; f < 2; f++) {
					DP[i][c][f] = Long.MIN_VALUE;
				}
			}
		}
		
		DP[0][0][0] = 0;
		
		//a xor b = Xかつa + b = Sを満たすaの最大値を求める
		long D = 1;
		for (int i = 0; i < MAX-1; i++) {
			long DX = X & 1;
			long DS = S & 1;
			long DA = (A >> i) & 1;
			
			// 繰り上り
			for (int c = 0; c < 2; c++) {
				// Aより大きいフラグ
				for (int f = 0; f < 2; f++) {
					//DP[i][c][f]を展開しDP[i+1][][]を最大化する
					
					if ( DP[i][c][f] < 0 ) continue;
					
					for (long na = 0; na < 2; na++) {
						for (long nb = 0; nb < 2; nb++) {
							if ( (na ^ nb) != DX ) continue;
							if ( ((na + nb + c) & 1) != DS ) continue;
							
							int nc = (na + nb + c > 1) ? 1 : 0;
							int nf;
							if (DA > na) {
								nf = 0;
							} else if(DA < na) {
								nf = 1;
							} else {
								nf = f;
							}
							
							DP[i+1][nc][nf] = Math.max(DP[i+1][nc][nf], DP[i][c][f] + na * D);
						}
					}
					
					
				}
			}
			
			X >>= 1;
			S >>= 1;
			D <<= 1;
		}
		
		long max = DP[MAX-1][0][0];
		if (max <= 0) {
			System.out.println(-1);
		} else {
			System.out.println(A - max);
		}
	}
	
	public static void main(String[] args) {
		new Main();
	}
}

class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;
	
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	
	private int readByte() {
		if (hasNextByte()) return buffer[ptr++];
		else return -1;
	}
	
	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}
	
	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
		return hasNextByte();
	}
	
	public String next() {
		if (!hasNext()) throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	public long nextLong() {
		if (!hasNext()) throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	
	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
		return (int) nl;
	}
	
	public double nextDouble() {
		return Double.parseDouble(next());
	}
}