import Control.Monad
import Data.Maybe
import Debug.Trace
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.IntMap.Strict as IntMap
import qualified Data.Set as Set
import qualified Data.IntSet as IntSet
import Data.Functor
import Data.Array

main :: IO ()
main = do
  (n:k:_) <- fmap (map read . words) getLine :: IO [Integer]
  as <- fmap (map read . words) getLine :: IO [Integer]
  print $ solve k as

solve k as = (count (m-1) True kbin . transpose . map (slice m) $ abins) + count1s m abins 
  where
    n = length as
    abins = map toBinary as
    kbin = toBinary k
    m = length kbin
    count _ _ [] _ = 0
    count _ _ _ [] = 0
    count i True (k:kbin) (b:bins)
      | k == 0 = 2^i * (sum b) + count (i-1) True kbin bins
      | n <= 2 * (sum b) = 2^i * (sum b) + count (i-1) False kbin bins
      | otherwise = 2^i * (n - sum b) + count (i-1) True kbin bins
    count i False kbin (b:bins) = 2^i * (max (n - sum b) (sum b)) + count (i-1) False kbin bins

count1s m abins = sum . map f $ abins
    where 
      n = length abins
      f bs 
        | n <= m = 0
        | otherwise = sum . zipWith (\b i -> b * 2^i) (take (n - m) bs) $ [(n-1), (n-2)..]

-- toBinary :: Integer -> [Int]
toBinary = reverse . go 
  where
    go i
      | i < 2  = [fromIntegral (i `mod` 2)]
      | otherwise = (fromIntegral (i `mod` 2)):(go $ i `div` 2)

slice n ls 
  | m < n = (take (n - m) (repeat 0)) ++ ls
  | otherwise = drop (m - n) ls
  where m = length ls