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):
import sys

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7

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

K = 43


A = Arr[0]
B = Arr[1]

S = A + B
X = 0
for i in range(2, len(Arr)):
    X ^= Arr[i]









dp = [[[-1 for x in [0, 1]] for _ in [0, 1]] for _ in range(K)]

dp[0][0][0] = 0  

v = 1
for i in range(K - 1):  
    cx = X & 1  
    cs = S & 1  
    ca = A & 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):
                    ni = i + 1
                    nj = 0
                    nk = k
                    if na ^ nb != cx:  
                        continue

                    ns = 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[K - 1][0][0]
if a == -1 or a == 0:
    print(-1)
else:
    print(Arr[0] - a)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

XOR=0
for a in A[2:]:
    XOR^=a

S=A[0]+A[1]

DP=[[[1<<60]*2 for i in range(2)] for j in range(60)]
DP[0][0][1]=0

for i in range(59):
    CX=0
    if XOR & (1<<i)!=0:
        CX=1

    CS=0
    if S & (1<<i)!=0:
        CS=1

    CA=0
    if A[1] & (1<<i)!=0:
        CA=1
    
    for j in range(2):
        for k in range(2):
            if DP[i][j][k]==1<<60:
                continue

            for na,nb in [(0,0),(0,1),(1,0),(1,1)]:
                if na^nb != CX:
                    continue
                if (na+nb+j)%2 != CS:
                    continue
                
                if na+nb+j>=2:
                    nj=1
                else:
                    nj=0

                if CA<nb:
                    nk=1
                elif CA==nb:
                    nk=k
                else:
                    nk=0

                DP[i+1][nj][nk]=min(DP[i+1][nj][nk],DP[i][j][k]|((1<<i)*nb))
                

ANS=DP[59][0][1]-A[1]

if ANS>=A[0]:
    print(-1)
else:
    print(ANS)

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

al = 0
for i in range(2,n):
    al ^= a[i]
ans = 0
s = a[0]+a[1]
if s-al<0 or (s-al)%2:
    ans = -1
else:
    d = (s-al)//2
    now = d
    for i in range(65,-1,-1):
        if (1 << i)&al and (1 << i)+now<=a[0]:
            now += 1 << i
    if now==0 or now>a[0] or d&al!=0:
        ans = -1
    else:
        ans = a[0]-now

print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

S = A[0] + A[1]
X = 0
for i in range(2, N) :
  X ^= A[i]

dp = [[[-1] * 2 for _ in range(2)] for _ in range(60)]
dp[0][0][0] = 0

for i in range(59) :
  cs = (S >> i) & 1
  cx = (X >> i) & 1
  ca = (A[0] >> i) & 1
  for j in (0, 1) :
    for k in (0, 1) :
      if dp[i][j][k] < 0 : continue
      for na in (0, 1) :
        for nb in (0, 1) :
          ni = i + 1
          nj = 0
          nk = k
          if na ^ nb != cx : continue
          ns = 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] | (na << i))
          
if 1 <= dp[-1][0][0] <= A[0] :
  print(A[0] - dp[-1][0][0])
else :
  print(-1)

Solution (Python):
import sys

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    _ = int(sys.stdin.readline().rstrip())
    Arr = [int(x) for x in sys.stdin.readline().rstrip().split()]

    K = 43

    
    A = Arr[0]
    B = Arr[1]

    S = A + B
    X = 0
    for i in range(2, len(Arr)):
        X ^= Arr[i]

    

    
    

    
    
    
    dp = [[[-1 for x in [0, 1]] for _ in [0, 1]] for _ in range(K)]

    dp[0][0][0] = 0  

    v = 1
    for i in range(K - 1):  
        cx = X & 1  
        cs = S & 1  
        ca = A & 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):
                        ni = i + 1
                        nj = 0
                        nk = k
                        if na ^ nb != cx:  
                            continue

                        ns = 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[K - 1][0][0]
    if a == -1 or a == 0:
        print(-1)
    else:
        print(Arr[0] - a)


main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
xor = 0
if n == 2:
  a,b = a
  if a >= b and (a+b)%2 == 0:
    print((a+b)//2-b)
  else:
    print(-1)
  exit()
for i in range(2,n):
  xor ^= a[i]
a,b = a[0],a[1]
sm = a+b
ls = []
smt = 0
for i in range(50)[::-1]:
  if xor&1<<i:
    ls.append(1)
    smt += 1<<i
  else:
    if smt+(2<<i) > sm:
      ls.append(0)
    else:
      ls.append(2)
      smt += 2<<i
if smt != sm:
  print(-1)
  exit()
mn = 0
mx = 0
for i in range(50):
  if ls[49-i] == 1:
    mx += 1<<i
  elif ls[49-i] == 2:
    mn += 1<<i
    mx += 1<<i
if not mn <= a:
  print(-1)
  exit()
ans = mn
for i in range(50):
  if ls[i] == 1:
    if ans+(1<<(49-i)) > a:
      continue
    else:
      ans += 1<<(49-i)
if ans == 0:
  print(-1)
else:
  print(a-ans)

Solution (Python):
import sys

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    _ = int(sys.stdin.readline().rstrip())
    Arr = [int(x) for x in sys.stdin.readline().rstrip().split()]

    K = 43

    
    A = Arr[0]
    B = Arr[1]

    S = A + B
    X = 0
    for i in range(2, len(Arr)):
        X ^= Arr[i]

    

    
    

    
    
    
    dp = [[[-1 for x in [0, 1]] for _ in [0, 1]] for _ in range(K)]

    dp[0][0][0] = 0  

    v = 1
    for i in range(K - 1):  
        cx = X & 1  
        cs = S & 1  
        ca = A & 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):
                        ni = i + 1
                        nj = 0
                        nk = k
                        if na ^ nb != cx:  
                            continue

                        ns = 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[K - 1][0][0]
    if a == -1 or a == 0:
        print(-1)
    else:
        print(Arr[0] - a)


main()

Solution (Python):


N = int(input())
A = list(int(a) for a in input().split())

a = A[0]
b = A[1]
S = a + b
X = 0
for i in range(2, N):
    X ^= A[i]

dp = [[[-1] * 2 for i in range(2)] for j in range(50)]
dp[0][0][0] = 0

for i in range(49):
    cur_s = S>>i&1
    cur_x = X>>i&1
    cur_a = a>>i&1
    for j, k in ((0, 0), (0, 1), (1, 0), (1, 1)):
        if dp[i][j][k] == -1:
            continue
        for p, q in ((0, 0), (0, 1), (1, 0), (1, 1)):
            if p ^ q != cur_x:
                continue
            if (p + q + j)&1 != cur_s:
                continue
            ni = i + 1
            nj = 0
            if (p + q + j) >= 2:
                nj = 1
            if cur_a < p:
                nk = 1
            elif cur_a > p:
                nk = 0
            else:
                nk = k
            dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]|(1<<i)*p)

tmp = dp[49][0][0]
if tmp == 0 or tmp == -1:
    print(-1)
else:
    print(a - tmp)

Solution (Python):
import random
N = int(input())
A = list(map(int, input().split()))
S = A[0] + A[1]
X = 0
for a in A[2:]:
  X ^= a
D = (S - X) >> 1
if S != X + (D << 1) or D > A[0] or (X & D) != 0:
  print(-1)
  exit(0)
a = D
b = 2 ** 64
while b > 0:
  if (X & b) != 0 and (a | b) <= A[0]:
    a |= b
  b >>= 1
if a == 0:
  print(-1)
  exit(0)
print(A[0] - a)

Solution (Python):
from functools import reduce, lru_cache

INF = float("inf")

N, a, b, *A = map(int, open(0).read().split())
c = reduce(lambda a, b: a ^ b, A, 0)

@lru_cache(None)
def solve(a, b, c):
    if (a % 2) ^ (b % 2) != (c % 2):
        return INF
    if c == 0:
        return INF if a < b else (a - b) // 2

    return min(
        2 * solve(a // 2, b // 2, c // 2),
        1 + 2 * solve((a - 1) // 2, (b + 1) // 2, c // 2)
    )

print(-1 if (res := solve(a, b, c)) >= a else res)

Solution (Python):
from functools import lru_cache

def main():
    N = int(input())
    A = list(map(int, input().split()))
    x = 0
    for a in A[2:]:
        x ^= a
    a, b = A[0], A[1]
    if (a ^ b ^ x) & 1 != 0:
        return -1

    @lru_cache(None)
    def helper(a, b, x):
        if a == 0:
            return 0 if a ^ b == x else None
        if (a ^ b) & 1 != x & 1:
            return None
        t1 = helper(a // 2, b // 2, x // 2)
        t2 = helper((a - 1) // 2, (b + 1) // 2, x // 2)
        if t1 is None and t2 is None:
            return None
        if t1 is None:
            return t2 * 2 + 1
        if t2 is None:
            return t1 * 2
        t1 = t1 * 2
        t2 = t2 * 2 + 1
        return min(t1, t2)
    r = helper(a, b, x)
    if r is None or r >= a:
        return -1
    return r

print(main())

Solution (Python):
N = map(int, input().split())
As = list(map(int, input().split()))

added = As[0] + As[1]
xored = 0
for A in As[2:]:
    xored ^= A

anded = added - xored
if anded % 2 == 1:
    print(-1)
    exit()
else:
    anded >>= 1

left_stones_l_A0 = '' 
left_stones_e_A0 = '' 
for i, (a, x) in enumerate(zip(format(anded, '045b'), format(xored, '045b'))):
    if x == '0':
        if a == '0':
            left_stones_l_A0 += '0'
            left_stones_e_A0 += '0'
        else:
            left_stones_l_A0 += '1'
            left_stones_e_A0 += '1'
    else:
        if a == '0':
            if int(left_stones_l_A0 + '1' + '1' * (44 - i), base = 2) <= As[0]:
                left_stones_l_A0 += '1'
            else:
                left_stones_l_A0 += '0'
            if int(left_stones_e_A0 + '1' + '1' * (44 - i), base = 2) <= As[0]:
                left_stones_e_A0 += '1'
                left_stones_l_A0 = left_stones_e_A0
            elif int(left_stones_e_A0 + '1' + '0' * (44 - i), base = 2) <= As[0]:
                left_stones_l_A0 = left_stones_e_A0 + '0'
                left_stones_e_A0 += '1'
            elif int(left_stones_e_A0 + '0' + '0' * (44 - i), base = 2) <= As[0]:
                left_stones_e_A0 += '0'
            else:
                left_stones_e_A0 = left_stones_l_A0
        else:
            print(-1)
            exit()

if int(left_stones_e_A0, base = 2) <= As[0]:
    ans = As[0] - int(left_stones_e_A0, base = 2)
else:
    ans = As[0] - int(left_stones_l_A0, base = 2)

if 0 <= ans < As[0]:
    print(ans)
else:
    print(-1)

Solution (Python):
from collections import defaultdict
mem = defaultdict(lambda : None)
mem[(0,0,0)] = 0
def dp(a,b,s):
  if mem[(a,b,s)] is not None:
    return mem[(a,b,s)]
  x = a&1
  y = b&1
  xor = s&1
  res = float("inf")
  if x^y != xor:
    mem[(a,b,s)] = res
    return res
  else:
    res = 2*dp(a>>1,b>>1,s>>1)
    if a > 1:
      pants = 2*dp((a-1)>>1,(b+1)>>1,s>>1)+1
      if pants < res:
        res = pants
  mem[(a,b,s)] = res
  return res

n = int(input())
a = list(map(int, input().split()))
s = 0
for i in a[2:]:
  s ^= i
b = a[1]
a = a[0]
ans = dp(a,b,s)
if ans == float("inf"):
  ans = -1
print(ans)

Solution (Python):
n=int(input())
A=[int(i) for i in input().split()]

A1=A[0]
A2=A[1]

c=0
for ele in A[2:]:
    c^=ele

d=A1+A2-c
if d%2==1:
    print(-1)
else:
    d=d//2
    
    flgs=[]
    
    for i in range(41):
        fand= d & (1<<i)
        fxor= c & (1<<i)
        
        if fand & fxor:
            print(-1)
            break
        elif fand:
            flgs.append(1)
        elif fxor:
            flgs.append(2)
        else:
            flgs.append(0)
    else:
        flgs=flgs[::-1]
        
        ans=0
        for i in range(len(flgs)):
            if flgs[i]==1:
                ans+=2**(41-i-1)
        
        if ans>A1:
            print(-1)
        else:
            
            for i in range(len(flgs)):
                if flgs[i]==2:
                    if ans + 2**(41-i-1) <= A1:
                        ans+=2**(41-i-1)
            
            
            
            if ans==0:
                print(-1)
            else:
                print(A1-ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

K = 43


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    a1 = a[0]
    a2 = a[1]
    s = a1 + a2
    x = 0
    for i in range(2, n):
        x ^= a[i]

    dp = [[[-1 for _ in range(2)] for _ in range(2)] for _ in range(K)]
    dp[0][0][0] = 0
    v = 1
    for i in range(K-1):
        cx = x & 1
        cs = s & 1
        ca = a1 & 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):
                        ni = i + 1
                        nj = 0
                        nk = k
                        if na ^ nb != cx:
                            continue
                        ns = 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
        a1 >>= 1
        v <<= 1

    ans = dp[K-1][0][0]
    if ans == -1 or ans == 0:
        print(-1)
    else:
        print(a[0]-ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N = NI()
    A = LI()

    x = 0
    for i in A[2:]:
        x ^= i

    s = A[0] + A[1]
    if s - x < 0 or (s - x) % 2:
        print(-1)
        return

    a = (s-x)//2
    if a & x:
        print(-1)
        return

    b = 1 << (A[0].bit_length() - 1)
    while b > 0:
        if b & x and a + b <= A[0]:
            a += b
        b >>= 1
    if 0 < a <= A[0]:
        print(A[0] - a)
    else:
        print(-1)


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):
n = int(input())
l = list(map(int,input().split()))

x = 0
s = l[0]+l[1]
for i in l[2:]:
    x ^= i
if x > s or (s-x)%2 or ((s-x)//2)&x:
    print(-1)
    exit()
a = (s-x)//2
if a > l[0]:
    print(-1)
    exit()
for i in range(50,-1,-1):
    if 1<<i & x:
        if a+ (1<<i) <= l[0]:
            a += 1<<i
if a == 0:
    print(-1)
else:
    print(l[0]-a)

Solution (Python):
from functools import lru_cache

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

tmp = 0
for a in A[2:]:
    tmp ^= a

INF = 10 ** 18

@lru_cache(None)
def F(a, b, xor):
    if (a & 1) ^ (b & 1) != (xor & 1): 
        return INF
    if xor == 0:
        return INF if a < b else (a - b) // 2 
    
    x = 2 * F(a // 2, b // 2, xor // 2)
    
    y = 2 * F((a - 1) // 2, (b + 1) // 2, xor // 2) + 1 

    x = min(x, y)
    if x >= INF:
        x = INF
    
    return x

x = F(A[0], A[1], tmp)
if x >= A[0]:
    x = -1

print (x)

Solution (Python):

def resolve():
    
    
    
    N = int(input())
    A = list(map(int, input().split()))

    
    S = A[0] + A[1]

    
    xor = 0
    for a in A[2:]:
        xor ^= a

    
    
    S -= xor
    if S%2 == 1:
        return print(-1)

    S //= 2
    
    if (S&xor) != 0 or S > A[0]:
        return print(-1)

    
    
    b = S
    for i in range(41, -1, -1):
        if xor>>i & 1 == 1:
            if b + (1<<i) <= A[0]:
                b += 1<<i

    if b == 0:
        print(-1)
    else:
        print(A[0] - b)


if __name__ == "__main__":
    resolve()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
P = 41

X = 0
for i in range(2, N):
    X ^= A[i]

S = A[0] + A[1]
A1 = A[0]

D, M = divmod(S - X, 2)
ans = -1
if M % 2 == 0 and D & X == 0 and D <= A1:
    Y = 0
    for p in range(P, -1, -1):
        if (X >> p) & 1 == 1:
            new_Y = Y ^ (1 << p)
            if D ^ new_Y <= A1:
                Y = new_Y

    ans = A1 - (D ^ Y)
    if ans == A1:
        ans = -1

print(ans)

Solution (Python):
import random
N = int(input())
A = list(map(int, input().split()))
 
S = A[0]+A[1]
X = 0
for a in A[2:]:
  X ^= a
 
D = (S-X)>>1
if S!=X+(D<<1) or D>A[0] or (X&D)!=0:
  print(-1)
  exit(0)
a = D
b = 2**64
while b>0:
  if (X&b)!=0 and (a|b)<=A[0]:
    a |= b
  b >>= 1
if a==0:
  print(-1)
  exit(0)
print(A[0]-a)

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):
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):
import sys

n = int(input())
a = [int (x) for x in input().split()]

T = 0
for i in range(2,n):
    T ^= a[i]

S = a[0] + a[1]
D = (S-T)//2
if S<T or (S-T)%2 or D&T:
    print(-1)
    sys.exit()

for i in range(T.bit_length()+1,-1,-1):
    if T&(1<<i)|D <= a[0]:
        D |= T&(1<<i)

if 0<D<=a[0]:
    print(a[0]-D)
else:
    print(-1)

Solution (Python):
def compute(S, X): 
  if (S-X)%2==1:
    print(-1)
    return
  A = (S - X)//2
  a = 0
  ambiguous=[0]*64
  for i in range(63,-1,-1): 
    Xi = (X & (1 << i)) 
    Ai = (A & (1 << i)) 
    if (Xi == 0 and Ai == 0): 
      pass
    elif (Xi == 0 and Ai > 0): 
      a = ((1 << i) | a)
    elif (Xi > 0 and Ai == 0): 
      ambiguous[i]=1
    else:
      print(-1) 
      return
  for i in range(63,-1,-1):
    if ambiguous[i]==1:
      R=((1 << i) | a) 
      if R<=a1[0]:
        a=R
      else:
        a = ((0 << i) | a)
  if a>a1[0]:
    print(-1)
  else:
    if a==0 and a1[0]!=0:
      print(-1)
    else:
      print(a1[0]-a)
    
  
import sys
n=int(input())
a1=list(map(int,input().split()))
nimsum=0
for i in range(2,n):
  nimsum=a1[i]^nimsum
compute(a1[0]+a1[1],nimsum)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def dp(a,b,s):
        if mem[(a,b,s)] is not None:
            return mem[(a,b,s)]
        x = a&1
        y = b&1
        z = s&1
        if x^y != z:
            return float("inf")
        res = 2*dp(a>>1,b>>1,s>>1)
        if a > 1:
            nr = 2*dp((a-1)>>1,(b+1)>>1,s>>1)+1
            if nr < res:
                res = nr
        mem[(a,b,s)] = res
        return res
    n = I()
    a = LI()
    s = 0
    for i in a[2:]:
        s ^= i
    b = a[1]
    a = a[0]
    mem = defaultdict(lambda : None)
    mem[(0,0,0)] = 0
    ans = dp(a,b,s)
    if ans == float("inf"):
        ans = -1
    print(ans)
    return


if __name__ == "__main__":
    solve()

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

Solution (Python):
from sys import exit
n = int(input())
a = list(map(int, input().split()))
X = 0
for i in range(2, n):
	X ^= a[i]
A = (a[0] + a[1] - X) // 2
if (a[0] + a[1] - X) != A * 2 or A > a[0] or A < 0 or X & A: 
	print(-1)
	exit()
res = A
for i in range(42, -1, -1):
	if (X >> i) & 1:
		if res + (1 << i) <= a[0]:
			res += (1 << i)
if res == 0:
	print(-1)
	exit()
print(a[0]-res)

Solution (Python):
import sys
N, = map(int, input().split())
X = list(map(int, input().split()))
S = X[0]+X[1]
T = 0
for i in range(2, N):
    T ^= X[i]
if S<T or (S-T)%2:
    print(-1)
    sys.exit()

D = (S-T)//2
if D&T:
    print(-1)
    sys.exit()

Y = D
for i in range(T.bit_length()+1, -1, -1):
    if T&(1<<i)|Y <= X[0]:
        Y |= T&(1<<i)
if 0<Y<=X[0]:
    print(X[0]-Y)
else:
    print(-1)

Solution (Python):
import sys
N, = map(int, input().split())
X = list(map(int, input().split()))
S = X[0]+X[1]
T = 0
for i in range(2, N):
    T ^= X[i]
if S<T or (S-T)%2:
    print(-1)
    sys.exit()

D = (S-T)//2
if D&T:
    print(-1)
    sys.exit()

Y = D
for i in range(T.bit_length()+1, -1, -1):
    if T & (1<<i) and (1<<i)|Y <= X[0]:
        Y |= 1<<i

if 0<Y<=X[0]:
    print(X[0]-Y)
else:
    print(-1)

Solution (Python):
import sys
N, = map(int, input().split())
X = list(map(int, input().split()))
S = X[0]+X[1]
T = 0
for i in range(2, N):
    T ^= X[i]
if S<T or (S-T)%2:
    print(-1)
    sys.exit()

D = (S-T)//2
if D&T:
    print(-1)
    sys.exit()

M = T.bit_length()+1
if D > X[0]:
    print(-1)
    sys.exit()

Y = D
for i in range(M, -1, -1):
    if T & (1<<i) and (1<<i)|Y <= X[0]:
        Y |= 1<<i

if Y == 0:
    print(-1)
else:
    print(X[0]-Y)

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):
import sys
N, = map(int, input().split())
X = list(map(int, input().split()))
S = X[0]+X[1]
T = 0
for i in range(2, N):
    T ^= X[i]
if S<T or (S-T)%2:
    print(-1)
    sys.exit()
D = (S-T)//2
if D&T:
    print(-1)
    sys.exit()
M = T.bit_length()+1
if D > X[0]:
    print(-1)
    sys.exit()
Y = 0
for i in range(M, -1, -1):
    if T & (1<<i) and ((1<<i)|Y)^D <= X[0]:
        Y = (1<<i)|Y
if Y^D == 0:
    print(-1)
else:
    print(X[0]-(Y^D))

Solution (Python):
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    aa=LI()
    x=0
    for a in aa[2:]:x^=a
    t=aa[0]+aa[1]-x
    if t<0 or t&1:
        print(-1)
        exit()
    t>>=1
    
    a0=a1=0
    for i in range(40,-1,-1):
        dt=t>>i&1
        dx=x>>i&1
        if (dt,dx)==(1,1):
            print(-1)
            exit()
        if (dt,dx)==(1,0):
            a0|=1<<i
            a1|=1<<i

    for i in range(40,-1,-1):
        dt=t>>i&1
        dx=x>>i&1
        if (dt,dx)==(0,1):
            if (a0|1<<i)<=aa[0]:a0|=1<<i
            else:a1|=1<<i

    
    if a0>0 and a0<=aa[0]:print(aa[0]-a0)
    else:print(-1)

main()

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
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))