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

pad :: Int -> [Int] -> [Int]
pad n ls = ls ++ (take (n - m) (repeat 0))
  where m = length ls

solve' k as = traceShow maxn $ fromJust . maximum $ (map g [0..(maxn-1)]) ++ [zeros]
    where
      kbin = toRevBinary k
      m = length kbin  
      n = length as
      abinary = map toRevBinary as
      maxn = max (maximum $ map length abinary) m
      -- counts = map sum . transpose . map (pad m . toRevBinary) $ as :: [Int]
      counts = map sum . transpose . map (pad maxn) $ abinary :: [Int]
      maxs = [max c (n-c) | c <- counts]
      ks = map (\ki -> if ki == 0 then ki else n - ki) kbin
      zeros = Just . sum $ zipWith (*) counts bis

      bis = map (2^) [0..]
      g :: Int -> Maybe Int
      g i
        | (pad maxn kbin) !! i == 0 = Nothing
        | otherwise = return . sum 
          $ zipWith (*) ((take i ks) ++ [n - counts!!i] ++ (drop (i+1) maxs)) bis

toRevBinary = go 
  where
    go i
      | i < 2  = [fromIntegral (i `mod` 2)]
      | otherwise = (fromIntegral (i `mod` 2)):(go $ i `div` 2)
