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 (Python):
def digit(x):
    ret = 0
    while x > 0:
        x //= 2
        ret += 1
    return ret

def main():
    N = int(input())
    A = list(map(int, input().split()))
    g = 0
    for i in range(2, N):
        g ^= A[i]
    a0 = A[0]
    a1 = A[1]
    ans = 0
    half = 2**20
    if a0 < half:
        for i in range(a0):
            t0 = a0 - i
            t1 = a1 + i
            if t0 ^ t1 == g:
                print(i)
                return
        print(-1)
        return

    
    ug = g >> 20
    b0 = a0 >> 20
    b1 = a1 >> 20
    upper_ans_0 = -1 
    for i in range(b0):
        t0 = b0 - i
        t1 = b1 + i
        if t0 ^ t1 == ug:
            upper_ans_0 = i
            break

    upper_ans_1 = -1 
    for i in range(b0):
        t0 = b0 - i
        t1 = b1 + 1 + i
        if t0 ^ t1 == ug:
            upper_ans_1 = i
            break

    upper_ans_2 = -1 
    for i in range(b0-1):
        t0 = b0 - 1 - i
        t1 = b1 + i
        if t0 ^ t1 == ug:
            upper_ans_2 = i
            break

    upper_ans_3 = -1 
    for i in range(b0-1):
        t0 = b0 - 1 - i
        t1 = b1 + 1 + i
        if t0 ^ t1 == ug:
            upper_ans_3 = i
            break

    
    
    lower_answers = []
    for i in range(half):
        t0 = (a0 - i) % half
        t1 = (a1 + i) % half
        if t0 ^ t1 == (g % half):
            lower_answers.append(i)
    if len(lower_answers) == 0:
        print(-1)
        return
    
    
    answers = []
    for l in lower_answers:
        if (a0 % half) >= l:
            
            if (a1 % half) + l < half:
                
                if upper_ans_0 != -1:
                    answers.append((upper_ans_0 << 20) + l)
            else:
                
                if upper_ans_1 != -1:
                    answers.append((upper_ans_1 << 20) + l)
        else:
            
            if (a1 % half) + l < half:
                
                if upper_ans_2 != -1:
                    answers.append((upper_ans_2 << 20) + l)
            else:
                
                if upper_ans_3 != -1:
                    answers.append((upper_ans_3 << 20) + l)
    if len(answers) == 0:
        print(-1)
        return
    answers.sort()
    print(answers[0])

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n=int(input())
    A=list(map(int,input().split()))
    x=A[0]^A[1]
    for i in A:
        x^=i
    s=A[0]+A[1]
    dp=[[[-1]*2 for _ in range(2)] for _ in range(43)]
    dp[0][0][0]=0
    v=1
    a=A[0]
    for i in range(42):
        cx,ca,cs=x&1,a&1,s&1
        for j in range(2):
            for k in range(2):
                if dp[i][j][k]==-1:
                    continue
                for na in range(2):
                    for nb in range(2):
                        if na^nb!=cx:
                            continue
                        ni,nj,nk,ns=i+1,0,k,na+nb+j
                        if ns%2!=cs:
                            continue
                        if ns>=2:
                            nj=1
                        if ca<na:
                            nk=1
                        elif ca==na:
                            nk=k
                        else:
                            nk=0
                        dp[ni][nj][nk]=max(dp[ni][nj][nk],dp[i][j][k]|(v*na))
        x>>=1
        s>>=1
        a>>=1
        v<<=1
    a=dp[42][0][0]
    if a==0 or a==-1:
        ans=-1
    else:
        ans=A[0]-a
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from functools import lru_cache
N, = map(int, input().split())
X = list(map(int, input().split()))
t = 0
for x in X:
    t = t^x
inf = float('inf')
@lru_cache(None)
def it(x, y, z):
    xm, ym, zm = x%2, y%2, z%2
    if xm ^ ym != zm:
        return inf
    if x^y == z:
        return 0
    Rz = 2*it(x>>1, y>>1, z>>1)
    Ro = inf
    if x:
        Ro = 2*it((x-1)>>1, (y+1)>>1, z>>1)+1
    Rz = min(Rz, Ro)
    return Rz

r = it(X[0], X[1], t^X[0]^X[1])
if 0 <= r < X[0]:
    print(r)
else:
    print(-1)

Solution (Python):
N, = map(int, input().split())
X = list(map(int, input().split()))
t = 0
for x in X:
    t = t^x
inf = float('inf')
d = dict()
def it(x, y, z):
    if (x, y, z) in d:
        return d[(x, y, z)]
    xm, ym, zm = x%2, y%2, z%2
    if xm ^ ym != zm:
        d[(x, y, z)] = inf
        return inf
    if x^y == z:
        d[(x, y, z)] = 0
        return 0
    Rz = 2*it(x>>1, y>>1, z>>1)
    Ro = inf
    if x:
        Ro = 2*it((x-1)>>1, (y+1)>>1, z>>1)+1
    Rz = min(Rz, Ro)
    d[(x, y, z)] = Rz
    return Rz

r = it(X[0], X[1], t^X[0]^X[1])
if 0 <= r < X[0]:
    print(r)
else:
    print(-1)

Solution (Python):
INF=10**18
def calc(a,b,xor):
  if (a,b,xor) in memo:
    return memo[(a,b,xor)]
  if (a&1)^(b&1)!=(xor&1):
    return INF
  if xor==0:
    if a<b:
      return INF
    else:
      return (a-b)//2
  x=2*calc(a//2,b//2,xor//2)
  y=2*calc((a-1)//2,(b+1)//2,xor//2)+1
  memo[(a,b,xor)]=min(x,y)
  return memo[(a,b,xor)]

n=int(input())
arr=list(map(int,input().split()))
a,b=arr[0],arr[1]
xor=0
for i in range(2,n):
  xor^=arr[i]
memo={}
ans=calc(a,b,xor)
if ans>=a:
  print(-1)
else:
  print(ans)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
a = A[0]
b = A[1]
x = 0
for i in A[2:]:
    x ^= i

d = {}
def F(a,b,xor):
    if (a,b,xor) in d:
        return d[(a,b,xor)]
    if a&1 ^ b&1 != xor&1:
        return float("INF")
    if xor == 0:
        if a<b:
            return float("INF")
        else:
            return (a-b)//2
    c1 = 2*F(a//2,b//2,xor//2)
    c2 = 2*F((a-1)//2,(b+1)//2,xor//2) + 1
    f = min(c1,c2)
    d[(a,b,xor)] = f
    return f

ans = F(a,b,x)
if ans >= a:
    print(-1)
else:
    print(ans)

Solution (Python):
from functools import reduce
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))

r = reduce(lambda x,y : x^y , A[2:],0)

a,b = A[:2]

def answer(ta,tb,tr):
    for i in range(ta):
        if (ta-i)^(tb+i) == tr:
            ans = i
            break
    else:
        ans = -1
    return ans

def solve(a,b,r):
    orga = a

    ans = 0
    l = max(map(lambda x : len(bin(x)), [a,b,r])) - 2
    cand = {(a,b,r):0}
    for i in range(l):
        temp = defaultdict(lambda : 10**20)
        for (a,b,r),s in cand.items():
            x,y,z = a%2,b%2,r%2
            if not x^y == z:
                continue
            else:
                if s < temp[(a//2,b//2,r//2)]:
                    temp[(a//2,b//2,r//2)] = s
                a -= 1
                b += 1
                if a < 0:
                    continue
                s += (1<<i)
                if s < temp[(a//2,b//2,r//2)]:
                    temp[(a//2,b//2,r//2)] = s
        cand = temp

    if not (0,0,0) in cand:
        ans = -1
    else:
        ans = cand[(0,0,0)]
        if ans == orga:
            ans = -1
    return ans

print(solve(a,b,r))

Solution (Python):

import sys
def main(N, A):
    xor = 0
    for i in range(2, N):
        xor ^= A[i]
    p = A[0] + A[1]
    if (p - xor) & 1:
        return -1
    a = (p - xor) >> 1

    
    
    bl = max(A[0], xor).bit_length()
    dp = [[-1] * 2 for _ in range(bl + 1)]
    
    dp[0][0] = 0

    az = A[0]
    for i in range(bl):
        ni = i + 1
        azb = az & 1
        xorb = xor & 1
        ab = a & 1
        for j in range(2):
            if dp[i][j] < 0: continue
            for nazb in range(2):
                if nazb > azb:
                    nj = 1
                elif nazb < azb:
                    nj = 0
                else:
                    nj = j
                for nbzb in range(2):
                    if nazb ^ nbzb == xorb and nazb & nbzb == ab:
                        break
                else:
                    continue
                dp[ni][nj] = max(dp[ni][nj], dp[i][j] | (nazb << i))
        az >>= 1
        xor >>= 1
        a >>= 1

    ans = dp[bl][0]
    if ans <= 0:
        return - 1
    return A[0] - ans

if __name__ == '__main__':
    input = sys.stdin.readline
    N = int(input())
    *A, = map(int, input().split())
    print(main(N, A))

Solution (Python):
import math

import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    x = 0
    for i in a[2::]:
        x ^= i
    s = a[0]+a[1]
    d = (s-x)//2
    if d < 0 or d&x or (s-x)&1 or d > a[0]:
        print(-1)
        exit()
    a1 = d
    xi = 1
    kx = x
    while kx > 0:
        xi *= 2
        kx >>= 1
    xi //= 2
    while xi > 0:
        if x&xi:
            if a1+xi <= a[0]:
                a1 += xi
            x -= xi
        xi //= 2
    if a1 == 0:
        print(-1)
        exit()
    else:
        print(a[0]-a1)

    return

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int,readline().split()))
s = a[0]+a[1]
if n == 2:
  if a[0] < a[1] or s%2 == 1:
    print(-1)
  else:
    print(a[0]-s//2)
  exit()
  
c = 0
for i in a[2:]:
  c ^= i

sc = (s-c)//2
if s < c or (s-c)%2 == 1 or c&sc:
  print(-1)
  exit()

bb = a[0].bit_length()
x = ''
chk = 0
for i in range(bb-1,-1,-1):
  if not (c>>i&1)|(sc>>i&1):
    x += '0'
  elif not c>>i&1 and sc>>i&1:
    x += '1'
    chk += 2**i
  else:
    x += '?'
  
for i in range(bb):
  if x[i] == '?':
    if a[0] >= chk+2**(bb-1-i):
      chk += 2**(bb-1-i)

ans = a[0]-chk
print(ans if a[0] > ans >= 0 else -1)

Solution (Python):

import math
N = int(input())
A = list(map(int, input().split()))

all_xor = 0
for i in range(N):
  all_xor = all_xor ^ A[i]

fore = A[0] ^ A[1]
back = fore ^ all_xor 



a_and_b = (A[0] + A[1] - back) / 2


if (a_and_b % 1 != 0) or (a_and_b < 0):
  print(-1)
  quit()
a_and_b = int(a_and_b)



if (a_and_b & back) != 0:
  print(-1)
  quit()

ans = a_and_b

if back == 0:
  if (ans == 0):
    print(-1)
  else:
    print(A[0] - ans)
  quit()
else:
  if ans > A[0]:
    print(-1)
    quit()
  nn = int(math.log2(back)) + 1
  
  
  
  for i in range(nn + 1):
    
    if ((back >> (nn - i)) & 1):
      
      k = ans + (1 << (nn - i))
      
      if k <= A[0]:
        ans = k 
  if (ans == 0):
    print(-1)
  else:
    print(A[0] - ans)

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
xsm = 0
sm = a[0] + a[1]

for i in range(2, N): xsm ^= a[i]
t = sm - xsm
table = [0] * 41
for i in range(40, -1, -1):
  if t >= 1 << (i + 1):
    t -= 1 << (i + 1)
    table[i] = 1
if t:
  print(-1)
  exit(0)


t = a[0]
res = 0
for i in range(40, -1, -1):
  if t >= 1 << i:
    if table[i]:
      t -= 1 << i
      res += 1 << i
for i in range(40, -1, -1):
  if t >= 1 << i:
    if xsm & (1 << i):
      t -= 1 << i
      res += 1 << i
if res == 0 or (res ^ (sm - res) != xsm): print(-1)
else: print(a[0] - res)

Solution (Python):
def f_unfair_nim():
    from functools import reduce
    from operator import xor
    N = int(input())
    A = [int(i) for i in input().split()]

    s = sum(A[:2])
    x = reduce(xor, A[2:], 0)
    if (s - x) % 2 != 0 or (((s - x) // 2) & x) != 0:
        return -1

    a = (s - x) // 2
    for i in range(60, -1, -1):
        if (x & (1 << i)) != 0 and (a ^ (1 << i) <= A[0]):
            a ^= 1 << i
    return A[0] - a if A[0] >= a > 0 else -1

print(f_unfair_nim())

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd, sqrt
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
a = LI()

y = 0
flg = 0

for ai in a[2:]:
    y = y ^ ai

x = a[0] + a[1] - y
if x < 0 or x % 2:
    flg = 1

x //= 2
a1 = x

if a1 > a[0]:
    flg = 1

for i in reversed(range((a[0] + a[1]).bit_length())):
    if y & (1 << i):
        if x & (1 << i):
            flg = 1
        if a1 + 2 ** i <= a[0]:
            a1 += 2 ** i

if a1 == 0:
    flg = 1

print(a[0] - a1 if flg == 0 else -1)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd, sqrt
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = int(input())
a = list(map(int, input().split()))

t = 0
flg = 0

for ai in a[2:]:
    t = t ^ ai

x = a[0] + a[1] - t
if x < 0 or x % 2:
    flg = 1

x //= 2
a1 = x

if a1 > a[0]:
    flg = 1

for i in reversed(range((a[0] + a[1]).bit_length())):
    if t & (1 << i):
        if x & (1 << i):
            flg = 1
        if a1 + 2 ** i <= a[0]:
            a1 += 2 ** i

if a1 == 0:
    flg = 1

print(a[0] - a1 if flg == 0 else -1)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd, sqrt
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = int(input())
a = list(map(int, input().split()))

t = 0
flg = 0

for ai in a[2:]:
    t = t ^ ai

b1 = a[0] + a[1] - t
if b1 < 0 or b1 % 2:
    flg = 1

b1 //= 2
a1 = b1

if a1 > a[0]:
    flg = 1

for i in reversed(range((a[0] + a[1]).bit_length())):
    if t & (1 << i):
        if b1 & (1 << i):
            flg = 1
        if a1 + 2 ** i <= a[0]:
            a1 += 2 ** i

if a1 == 0:
    flg = 1

print(a[0] - a1 if flg == 0 else -1)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd, sqrt
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = int(input())
a = list(map(int, input().split()))
c = a[0] + a[1]

t = 0
flg = 0

for ai in a[2:]:
    t = t ^ ai

b1 = c - t
if b1 < 0 or b1 % 2:
    flg = 1

b1 //= 2
a1 = b1

if a1 > a[0]:
    print(-1)
    sys.exit()

for i in reversed(range((a[0] + a[1]).bit_length())):
    if t & (1 << i):
        if b1 & (1 << i):
            print(-1)
            sys.exit()
        if a1 + 2 ** i <= a[0]:
            a1 += 2 ** i

if a1 == 0:
    flg = 1

print(a[0] - a1 if flg == 0 else -1)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd, sqrt
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = int(input())
a = list(map(int, input().split()))
c = a[0] + a[1]

t = 0
flg = 0

for ai in a[2:]:
    t = t ^ ai

if c < t:
    flg = 1

if (c - t) % 2:
    flg = 1

b1 = (c - t) // 2

a1 = b1

if a1 > a[0]:
    print(-1)
    sys.exit()

for i in reversed(range((a[0] + a[1]).bit_length())):
    if t & (1 << i):
        if b1 & (1 << i):
            print(-1)
            sys.exit()
        if a1 + 2 ** i <= a[0]:
            a1 += 2 ** i

if a1 == 0:
    flg = 1

print(a[0] - a1 if flg == 0 else -1)

Solution (Python):
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    f, s, *a = nm()
    x = 0
    for c in a: x ^= c
    if (f + s - x) % 2 or ((f + s - x) // 2) & x:
        print(-1)
        return
    y = (f + s - x) // 2
    for i in range(40, -1, -1):
        if x & (1 << i) and y | (1 << i) <= f:
            y |= 1 << i
    print(f - y if f >= y > 0 else -1)
    return

solve()

Solution (Python):
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    f, s, *a = nm()
    x = 0
    for c in a: x ^= c
    if (f + s - x) % 2 or ((f + s - x) // 2) & x:
        print(-1)
        return
    y = (f + s - x) // 2
    for i in range(40, -1, -1):
        if x & (1 << i) and y | (1 << i) < f:
            y |= 1 << i
    print(f - y if f >= y > 0 else -1)
    return

solve()

Solution (Python):
import pprint
N=int(input())
A=list(map(int,input().split()))
bitlen=42
flag=1
exor=0
andsum=0
ex=[0]*bitlen
an=[0]*bitlen
num=0
for i in range(2,N):
    exor=exor^A[i]
andsum=(A[0]+A[1])-exor

if (andsum%2!=0)or(andsum<0):
    flag=0
else:
    andsum=andsum//2
    for i in range(bitlen):
        if (exor>>i)&1:
            ex[i]=1
        if (andsum>>i)&1:
            an[i]=1
    num=andsum
    for i in range(bitlen):
        if (an[i]==1)and(ex[i]==1):
            flag=0
            break
    if num>A[0]:
        flag=0
    for i in range(bitlen-1,-1,-1):
        if ex[i]==1:
            if num+pow(2,i)<=A[0]:
                num=num+pow(2,i)
    
    if num==0:
        flag=0
if flag==0:
    print(-1)
else:
    print(A[0]-num)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int,readline().split()))
s = a[0]+a[1]
if n == 2:
  if a[0] < a[1] or s%2 == 1:
    print(-1)
  else:
    print(a[0]-s//2)
  exit()
  
c = 0
for i in a[2:]:
  c ^= i

sc = (s-c)//2
if s < c or (s-c)%2 == 1 or c&sc != 0:
  print(-1)
  exit()

bb = a[0].bit_length()
x = ''
chk = 0
for i in range(bb-1,-1,-1):
  if (c>>i&1)|(sc>>i&1) == 0:
    x += '0'
  elif c>>i&1 == 0 and sc>>i&1 == 1:
    x += '1'
    chk += 2**i
  else:
    x += 'p'
    
if chk > a[0]:
  print(-1)
  exit()
  
for i in range(bb):
  if x[i] == 'p':
    if a[0] >= chk+2**(bb-1-i):
      chk += 2**(bb-1-i)
      
if chk == 0:
  print(-1)
else:
  print(a[0]-chk)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
S=A[0]+A[1]
X=0
for i in range(2,N):
    X=X^A[i]
if (S-X)%2==1 or S<X:
    print(-1)
    exit()
D=(S-X)//2
if X&D!=0:
    print(-1)
    exit()
a=0

B=[0 for i in range(42)]
for k in range(41,-1,-1):
    x=(X&(1<<k))>>k
    d=(D&(1<<k))>>k
    if (x,d)==(0,0):
        pass
    elif (x,d)==(0,1):
        a=a^(1<<k)
    elif (x,d)==(1,0):
        B[k]=1
    else:
        pass
for k in range(41,-1,-1):
    if B[k]==0:
        continue
    if a^(1<<k)<A[0]:
        a=a^(1<<k)
if 0<a<=A[0]:
    print(A[0]-a)
else:
    print(-1)

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque


from collections import Counter

from itertools import accumulate,combinations,permutations

from heapq import heapify,heappop,heappush


from functools import lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    

    N = int(input())
    
    A = tuple(map(int, input().split())) 
    
    
    ref=0
    for i in range(2,N):
        ref^=A[i]
    
    a0=A[0]
    a1=A[1]
    s=a0+a1

    
    
    
    
    
    
    
    
        
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    dp=[[[-1]*2 for _ in range(2)] for _ in range(43)]
    refi=ref
    si=s
    dp[0][0][0]=0
    shift=1
    a=a0
    for i in range(42):
        ni=i+1
        for j in range(2):
            for k in range(2):
                cur=dp[i][j][k]
                if cur==-1:
                    continue
                for ai in range(0,2):
                    bi=(refi&1)^ai
                    if (bi+j+ai)&1!=si&1:
                        continue
                    if ai>a&1:
                        nk=1
                    elif ai==a&1:
                        nk=k
                    else:
                        nk=0

                    nj=0
                    if bi+j+ai>1:
                        nj=1
                    
                    dp[ni][nj][nk]=max(dp[ni][nj][nk],cur|shift*ai)
                    
        refi=(refi>>1)
        si>>=1
        a>>=1
        shift<<=1
    x=dp[-1][0][0]
    if x==-1 or x==0:
        print(-1)
    else:
        print(a0-x)


                





if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque


from collections import Counter

from itertools import accumulate,combinations,permutations

from heapq import heapify,heappop,heappush


from functools import lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    

    N = int(input())
    
    A = tuple(map(int, input().split())) 
    
    
    ref=0
    for i in range(2,N):
        ref^=A[i]
    
    a0=A[0]
    a1=A[1]
    s=a0+a1

    
    
    
    
    
    
    
    
        
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    dp=[[[-1]*2 for _ in range(2)] for _ in range(43)]
    refi=ref
    si=s
    dp[0][0][0]=0
    shift=1
    a=a0
    for i in range(42):
        ni=i+1
        for j in range(2):
            for k in range(2):
                cur=dp[i][j][k]
                if cur==-1:
                    continue
                for ai in range(0,2):
                    bi=(refi&1)^ai
                    if (bi+j+ai)&1!=si&1:
                        continue
                    if ai>a&1:
                        nk=1
                    elif ai==a&1:
                        nk=k
                    else:
                        nk=0

                    nj=0
                    if bi+j+ai>1:
                        nj=1
                    
                    dp[ni][nj][nk]=max(dp[ni][nj][nk],cur|shift*ai)
                    
        refi=(refi>>1)
        si>>=1
        a>>=1
        shift<<=1
    x=dp[-1][0][0]
    if x==-1 or x==0:
        print(-1)
    else:
        print(a0-x)


                





if __name__ == "__main__":
    main()

Solution (Python):
import sys
from functools import lru_cache


def solve(n, aaa):
    if n == 2:
        h, m = divmod(sum(aaa), 2)
        if m == 1 or h > aaa[0]:
            return -1
        return aaa[0] - h

    x = 0
    for a in aaa[2:]:
        x ^= a
    a0, a1 = aaa[:2]
    s = a0 + a1

    if s & 1 != x & 1:
        return -1

    b = 1 << 40
    MASK = (b << 1) - 1

    @lru_cache(maxsize=None)
    def dfs(p, b):
        if b == 0:
            return p
        m = (MASK ^ (b - 1)) << 1
        q = p ^ x & m
        if x & b == 0:
            if s >= p + q + 2 * b:
                if p | b <= a0:
                    return dfs(p | b, b >> 1)
                else:
                    return -1
            return dfs(p, b >> 1)
        else:
            if s < p + q + b:
                return -1
            if p | b <= a0:
                ret = dfs(p | b, b >> 1)
                if ret != -1:
                    return ret
            return dfs(p, b >> 1)

    ret = dfs(0, b)

    if ret <= 0:
        return -1
    return a0 - ret


n, *aaa = map(int, sys.stdin.buffer.read().split())
print(solve(n, aaa))