import Data.Maybe
import Data.List
import Data.Bits
import qualified Data.ByteString.Char8 as BS

readInt = fst . fromJust . BS.readInteger
readIntList = map readInt . BS.words
getIntList = readIntList <$> BS.getLine

main = do
  [n,m] <- getIntList
  xs <- getIntList
  print $ solve m n xs

solve :: Integer -> Integer -> [Integer] -> Integer
solve m n xs = solve' cs where
  a = maximum (m:xs)
  bs = takeWhile (a >=) $ iterate (2*) 1
  cs = snd $ mapAccumR f 0 bs

  f :: Integer -> Integer -> (Integer, (Bool, Integer, Integer, Integer))
  f e b = (e', (isOn, offv, onv, e)) where
    isOn = (m .&. b) /= 0
    onCnt = genericLength $ filter (\x -> x .&. b /= 0) xs
    offv = b * onCnt
    onv = b * (n - onCnt)
    e' = e + max offv onv

solve' :: [(Bool, Integer, Integer, Integer)] -> Integer
solve' [] = 0
solve' ((False, offv, _, e): cs) = offv + solve' cs
solve' ((True, offv, onv, e): cs)
  | offv >= onv = w
  | offv + e < onv = z
  | otherwise = max w z
  where
    w = offv + e
    z = onv + solve' cs
