Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
OneNum = [0] * 40
for a in A:
    for i in range(40):
        if a & (1 << i) == 2**i: OneNum[-1-i] += 1

def digDP(digit, rem, memo):
    if digit == 39:
        if rem == 0: return OneNum[-1]
        else: return max(OneNum[-1], N - OneNum[-1])
    try: return memo[(digit, rem)]
    except KeyError:
        if 2 ** (39 - digit) > rem:
            ans = 2 ** (39-digit) * OneNum[digit] + digDP(digit+1, rem, memo)
            memo[(digit, rem)] = ans
            return ans
        else:
            useDigit = 2 ** (39-digit) * (N-OneNum[digit]) + digDP(digit + 1, rem - (2**(39-digit)), memo)
            notUseDigit = 2 ** (39-digit) * OneNum[digit] + digDP(digit + 1, rem, memo)
            memo[(digit, rem)] = max(useDigit, notUseDigit)
            return max(useDigit, notUseDigit)

memo = {}
print(digDP(0, K, memo))


Fixed submission:
N, K = map(int, input().split())
bitK = format(K, "b").zfill(41)
A = [int(a) for a in input().split()]
DP = [[0, 0] for _ in range(41)] #DP[i][0]は上位i bit目までKと一致、DP[i][1]はK以下
Ones = [0] * 41
for a in A:
    bita = format(a, "b").zfill(41)
    for i in range(41):
        Ones[i] += int(bita[i])

if bitK[0] == "1":
    DP[0][0] = (2 ** 40) * (N - Ones[0])
    DP[0][1] = (2 ** 40) * Ones[0]
else:
    DP[0][0] = (2 ** 40) * Ones[0]
    DP[0][1] = 0

for i in range(1, 41):
    oneNum = Ones[i]
    digit = 2**(40-i)
    if bitK[i] == "1":
        DP[i][0] = DP[i-1][0] + digit * (N - oneNum)
        DP[i][1] = DP[i-1][0] + digit*oneNum
        if DP[i-1][1] > 0:
            DP[i][1] = max(DP[i][1], DP[i-1][1] + digit*max(oneNum, N-oneNum))
    else:
        DP[i][0] = DP[i-1][0] + digit*oneNum
        if DP[i-1][1] > 0:
            DP[i][1] = DP[i-1][1] + digit*max(oneNum, N-oneNum)

print(max(DP[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
OneNum = [0] * 40
for a in A:
    for i in range(40):
        if a & (1 << i) == 2**i: OneNum[-1-i] += 1

def digDP(digit, rem, memo):
    if digit == 39:
        if rem == 0: return OneNum[-1]
        else:
            return max(OneNum[-1], N - OneNum[-1])
    try: return memo[(digit, rem)]
    except KeyError:
        if 2 ** (39 - digit) > rem:
            ans = 2 ** (39-digit) * OneNum[digit] + digDP(digit+1, rem, memo)
            memo[(digit, rem)] = ans
            return ans
        else:
            useDigit = 2 ** (39-digit) * (N-OneNum[digit]) + digDP(digit + 1, rem - (2**(39-digit)), memo)
            notUseDigit = 2 ** (39-digit) * OneNum[digit] + digDP(digit + 1, rem, memo)
            memo[(digit, rem)] = max(useDigit, notUseDigit)
            return max(useDigit, notUseDigit)

memo = {}
print(digDP(0, K, memo))

Fixed submission:
N, K = map(int, input().split())
bitK = format(K, "b").zfill(41)
A = [int(a) for a in input().split()]
DP = [[0, 0] for _ in range(41)] #DP[i][0]は上位i bit目までKと一致、DP[i][1]はK以下
Ones = [0] * 41
for a in A:
    bita = format(a, "b").zfill(41)
    for i in range(41):
        Ones[i] += int(bita[i])

if bitK[0] == "1":
    DP[0][0] = (2 ** 40) * (N - Ones[0])
    DP[0][1] = (2 ** 40) * Ones[0]
else:
    DP[0][0] = (2 ** 40) * Ones[0]
    DP[0][1] = 0

for i in range(1, 41):
    oneNum = Ones[i]
    digit = 2**(40-i)
    if bitK[i] == "1":
        DP[i][0] = DP[i-1][0] + digit * (N - oneNum)
        DP[i][1] = DP[i-1][0] + digit*oneNum
        if DP[i-1][1] > 0:
            DP[i][1] = max(DP[i][1], DP[i-1][1] + digit*max(oneNum, N-oneNum))
    else:
        DP[i][0] = DP[i-1][0] + digit*oneNum
        if DP[i-1][1] > 0:
            DP[i][1] = DP[i-1][1] + digit*max(oneNum, N-oneNum)

print(max(DP[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
OneNum = [0] * 40
for a in A:
    for i in range(40):
        OneNum[-1-i] += a%2
        a//=2
        if a == 0: break

def digDP(digit, rem, memo):
    if digit == 39:
        if rem == 0: return OneNum[-1]
        else:
            return max(OneNum[-1], N - OneNum[-1])
    try: return memo[(digit, rem)]
    except KeyError:
        if 2 ** (39 - digit) > rem:
            ans = 2 ** (39-digit) * OneNum[digit] + digDP(digit+1, rem, memo)
            memo[(digit, rem)] = ans
            return ans
        else:
            useDigit = 2 ** (39-digit) * (N-OneNum[digit]) + digDP(digit + 1, rem - (2**(39-digit)), memo)
            notUseDigit = 2 ** (39-digit) * OneNum[digit] + digDP(digit + 1, rem, memo)
            memo[(digit, rem)] = max(useDigit, notUseDigit)
            return max(useDigit, notUseDigit)

memo = {}
print(digDP(0, K, memo))

Fixed submission:
N, K = map(int, input().split())
bitK = format(K, "b").zfill(41)
A = [int(a) for a in input().split()]
DP = [[0, 0] for _ in range(41)] #DP[i][0]は上位i bit目までKと一致、DP[i][1]はK以下
Ones = [0] * 41
for a in A:
    bita = format(a, "b").zfill(41)
    for i in range(41):
        Ones[i] += int(bita[i])

if bitK[0] == "1":
    DP[0][0] = (2 ** 40) * (N - Ones[0])
    DP[0][1] = (2 ** 40) * Ones[0]
else:
    DP[0][0] = (2 ** 40) * Ones[0]
    DP[0][1] = 0

for i in range(1, 41):
    oneNum = Ones[i]
    digit = 2**(40-i)
    if bitK[i] == "1":
        DP[i][0] = DP[i-1][0] + digit * (N - oneNum)
        DP[i][1] = DP[i-1][0] + digit*oneNum
        if DP[i-1][1] > 0:
            DP[i][1] = max(DP[i][1], DP[i-1][1] + digit*max(oneNum, N-oneNum))
    else:
        DP[i][0] = DP[i-1][0] + digit*oneNum
        if DP[i-1][1] > 0:
            DP[i][1] = DP[i-1][1] + digit*max(oneNum, N-oneNum)

print(max(DP[-1]))