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,k] <- getIntList
  as <- getIntList
  print $ solve k n as

solve k n as = d + solve' cs where
  (bs, t:_) = break (k <) $ iterate (2*) 1
  s = t - 1
  d = sum [(a .|. s) `xor` s| a <- as]
  cs = snd $ mapAccumR f 0 (reverse bs)

  f :: Integer -> Integer -> (Integer, (Bool, Integer, Integer, Integer))
  f best b = (best + max v0 v1, (bitTest k, v0, v1, best)) where
    bitTest x = (x .&. b) /= 0
    cnt = genericLength $ filter bitTest as
    v0 = b * cnt
    v1 = b * (n - cnt)

solve' :: [(Bool, Integer, Integer, Integer)] -> Integer
solve' cs = foldr f 0 cs where
  f (False, v0, _, best) accum = v0 + accum
  f (True, v0, v1, best) accum
    | v0 >= v1 = w
    | otherwise = max w (v1 + accum)
    where
      w = v0 + best
