Buggy python submission (verdict: Time Limit Exceeded):
# $B$1$s$A$g$s%V%m%0(B
# FROM : http://drken1215.hatenablog.com/entry/2019/02/03/224200 1
# from functools import reduce
# from operator import xor,add
# from collections import Counter

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

res = 0

for d in range(60,-2,-1):
    if d != -1 and not K & (1<<d):
        # K $B$K$=$N%S%C%H$,$J$$$H$-$O%9%-%C%W$9$k(B
        # d $B$O2?$r<($7$F$$$k!)%+%&%s%H!)(B
        # d $B$N0LCV$GJQ2=$9$k$H$7$?$i!"$H$$$&$3$H(B
        continue

    tmp = 0
    for e in range(60, -1, -1):
        # $B$3$l$O(Bmask $B$rMxMQ$7$F!"(BA[i] $B$KBP$7$F%+%&%s%H$7$F$$$k!#(B
        mask = 1<<e
        count = 0
        count += len(list(filter(lambda x:x&mask, A)))

        # $B%+%&%s%H=*N;(B


        # e$B$H(Bd($B%1%?0LCV(B)$B$rHf3S$7$F$$$k(B
        # e$B$NJ}$,Bg$-$$$H$-!"$3$l$OC1=c$KHf3S$G$-$J$$!#!!(B
        # K$B$K(Bd$B$N0LCV$K(B
        if e > d:
            if K & mask:
                tmp += mask * (N - count) # unseen $B$J$b$N(B
            else:
                tmp += mask * count  # seen $B$J$b$N(B
        elif e == d:
            # $B$b$7(Be$B$H(Bd$B$,F1$8$H$-!"$D$^$j$3$3$G$$$&(Bd$B$H$$$&$N$O!"(B
            # -1 $B$G$O$J$/!"$+$D$=$N%1%?$K(BK$B$K%S%C%H$,N)$C$F$$$k$b$N$G$"$k$,!"(B
            # $BJL$K(Be$B$H(Bd$B$,Aj0c$7$F$$$k!"$H$$$&$o$1$G$O$J$$(B
            # $B$3$l$^$C$?$/$o$+$i$J$$!#(B
            # e $B$H$$$&$N$H(B2$B=E%k!<%W$K$J$C$F$$$k$N$G!"(B
            # e $B$N0LCV$GAj0c$7$?>l9g!"$H$$$&$h$&$J46$8$N>l9g$o$1!)(B
            # d $B$O(B
            # $B$3$l$I$C$A$G$bF1$8$J$N$G$O!""-$K=q$+$l$k$h$&$K(B

            # count $B$GF~$l$k$H$$$&$3$H$O!"%^%9%/$rN)$F$J$$!"$H$$$&$d$jJ}(B

            tmp += mask * count # d $B$H$7$F$O!"$=$3$K%S%C%H$,N)$C$F$$$k$b$N$@$19MN8$7$F$$$k$N$G!"$3$N(B
            # tmp += mask * (N-count) # d $B$H$7$F$O!"$=$3$K%S%C%H$,N)$C$F$$$k$b$N$@$19MN8$7$F$$$k$N$G!"$3$N(B
            # $B%S%C%H$r%+%&%s%H$7$F$b$h$$(B $B"*!!$"$l!)$3$l%+%&%s%H$7$?$i(Bd K==1, X==0 $B$K$J$i$J$/$J$$!)(B

        else: # e $B$,>.$5$$$H$-$O<+M3(B
              # $BF~$l$F$bF~$l$J$/$F$bBg$-$$J}$r9MN8$9$k(B
              # $B:GBgCM$@$19MN8$9$k$N$G!"(Bcount$B$+(BN-count$B$+$O9M$($J$$(B
            tmp += mask * max(count, N-count)
    res = max(res, tmp)
print(res)
                 

Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
from functools import reduce
from operator import xor,add
from collections import Counter

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

mlist = [0]
powlist = []
bitcount = Counter()
bitunseencount = {}
s = 0
x = 2**s

while x <= M:
    powlist.append(x)
    s += 1
    x = 2**s

for a in A:
    for i,p in enumerate(powlist):
        bitcount[i] += (a&(1<<i))>>i

for i, p in enumerate(powlist):
    bitunseencount[i] = N - bitcount[i]

importance = []
for i,p in enumerate(powlist):
    importance.append( (1<<i) * bitcount[i])

unseenimportance = []
for i,p in enumerate(powlist):
    unseenimportance.append( (1<<i) * bitunseencount[i])


MASK = 0
for i,p in enumerate(powlist):
    if unseenimportance[i] > importance[i]:
        if M >= MASK|(1<<i):
            MASK |= 1<<i

ans = reduce(add, map(lambda x: x^MASK, A))
# -------------------
MASK = 0
for i,p in list(enumerate(powlist))[::-1]:
    # print(i,p)
    if unseenimportance[i] > importance[i]:
        if M >= MASK|(1<<i):
            MASK |= 1<<i

ans = max(ans, reduce(add, map(lambda x: x^MASK, A)))
print(ans)
exit(0)
# 
#     
#    
# 
# print("bit count : ", bitcount)
# print("bitunseen : ", bitunseencount)
# print("importance: ", importance)
# print("unseen rtance: ", unseenimportance)
#         
#         
# exit(0)
# 
# ans = 0
# for m in mlist:
#     # print("m : ", m )
#     # print("sum: " , reduce(add,  map(lambda x: x^m, A)))
#     ans = max(ans, reduce(add,  map(lambda x: x^m, A)))
# 
# print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# $B$1$s$A$g$s%V%m%0(B
# FROM : http://drken1215.hatenablog.com/entry/2019/02/03/224200 1
# from functools import reduce
# from operator import xor,add
# from collections import Counter

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

res = 0

for d in range(60,-2,-1):
    if d != -1 and not K & (1<<d):
        # K $B$K$=$N%S%C%H$,$J$$$H$-$O%9%-%C%W$9$k(B
        # d $B$O2?$r<($7$F$$$k!)%+%&%s%H!)(B
        # d $B$N0LCV$GJQ2=$9$k$H$7$?$i!"$H$$$&$3$H(B
        continue

    tmp = 0
    for e in range(60, -1, -1):
        # $B$3$l$O(Bmask $B$rMxMQ$7$F!"(BA[i] $B$KBP$7$F%+%&%s%H$7$F$$$k!#(B
        mask = 1<<e
        count = 0
        count += len(list(filter(lambda x:x&mask, A)))
        # for i in range(N):
            # if A[i] & mask : 
            #     count += 1

        # $B%+%&%s%H=*N;(B


        # e$B$H(Bd($B%1%?0LCV(B)$B$rHf3S$7$F$$$k(B
        # e$B$NJ}$,Bg$-$$$H$-!"$3$l$OC1=c$KHf3S$G$-$J$$!#!!(B
        # K$B$K(Bd$B$N0LCV$K(B
        if e > d:
            if K & mask:
                tmp += mask * (N - count) # unseen $B$J$b$N(B
            else:
                tmp += mask * count  # seen $B$J$b$N(B
        elif e == d:
            # $B$b$7(Be$B$H(Bd$B$,F1$8$H$-!"$D$^$j$3$3$G$$$&(Bd$B$H$$$&$N$O!"(B
            # -1 $B$G$O$J$/!"$+$D$=$N%1%?$K(BK$B$K%S%C%H$,N)$C$F$$$k$b$N$G$"$k$,!"(B
            # $BJL$K(Be$B$H(Bd$B$,Aj0c$7$F$$$k!"$H$$$&$o$1$G$O$J$$(B
            # $B$3$l$^$C$?$/$o$+$i$J$$!#(B
            # e $B$H$$$&$N$H(B2$B=E%k!<%W$K$J$C$F$$$k$N$G!"(B
            # e $B$N0LCV$GAj0c$7$?>l9g!"$H$$$&$h$&$J46$8$N>l9g$o$1!)(B
            # d $B$O(B
            # $B$3$l$I$C$A$G$bF1$8$J$N$G$O!""-$K=q$+$l$k$h$&$K(B

            # count $B$GF~$l$k$H$$$&$3$H$O!"%^%9%/$rN)$F$J$$!"$H$$$&$d$jJ}(B

            tmp += mask * count # d $B$H$7$F$O!"$=$3$K%S%C%H$,N)$C$F$$$k$b$N$@$19MN8$7$F$$$k$N$G!"$3$N(B
            # tmp += mask * (N-count) # d $B$H$7$F$O!"$=$3$K%S%C%H$,N)$C$F$$$k$b$N$@$19MN8$7$F$$$k$N$G!"$3$N(B
            # $B%S%C%H$r%+%&%s%H$7$F$b$h$$(B $B"*!!$"$l!)$3$l%+%&%s%H$7$?$i(Bd K==1, X==0 $B$K$J$i$J$/$J$$!)(B

        else: # e $B$,>.$5$$$H$-$O<+M3(B
              # $BF~$l$F$bF~$l$J$/$F$bBg$-$$J}$r9MN8$9$k(B
              # $B:GBgCM$@$19MN8$9$k$N$G!"(Bcount$B$+(BN-count$B$+$O9M$($J$$(B
            tmp += mask * max(count, N-count)
    res = max(res, tmp)
print(res)

Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
from functools import reduce
from operator import xor,add
from collections import Counter

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

mlist = [0]
powlist = []
bitcount = Counter()
bitunseencount = {}
s = 0
x = 2**s

while x <= M:
    powlist.append(x)
    s += 1
    x = 2**s

for a in A:
    for i,p in enumerate(powlist):
        bitcount[i] += (a&(1<<i))>>i

for i, p in enumerate(powlist):
    bitunseencount[i] = N - bitcount[i]

importance = []
for i,p in enumerate(powlist):
    importance.append( (1<<i) * bitcount[i])

unseenimportance = []
for i,p in enumerate(powlist):
    unseenimportance.append( (1<<i) * bitunseencount[i])


MASK = 0
for i,p in enumerate(powlist):
    if unseenimportance[i] > importance[i]:
        if M >= MASK|(1<<i):
            MASK |= 1<<i

ans = reduce(add, map(lambda x: x^MASK, A))
# -------------------
MASK = 0
for i,p in list(enumerate(powlist))[::-1]:
    # print(i,p)
    if unseenimportance[i] > importance[i]:
        if M >= MASK|(1<<i):
            MASK |= 1<<i

ans = max(ans, reduce(add, map(lambda x: x^MASK, A)))
print(ans)
exit(0)
# 
#     
#    
# 
# print("bit count : ", bitcount)
# print("bitunseen : ", bitunseencount)
# print("importance: ", importance)
# print("unseen rtance: ", unseenimportance)
#         
#         
# exit(0)
# 
# ans = 0
# for m in mlist:
#     # print("m : ", m )
#     # print("sum: " , reduce(add,  map(lambda x: x^m, A)))
#     ans = max(ans, reduce(add,  map(lambda x: x^m, A)))
# 
# print(ans)