import           Data.Bits ((.&.), bit)
import           Data.List (inits, tails, foldl')

main :: IO ()
main = do
  [n, k] <- take 2 . map read . words <$> getLine
  as <- take n . map read . words <$> getLine
  print $ f k as

-- C004_scrambled 以降 TLE
f :: Int -> [Int] -> Int
f k as =
  let
    aaa = beautifullnesses as
    bSize = bitSize $ maximum aaa
  in
    fst $ foldl' g (0, aaa) $ map bit [bSize, bSize - 1 .. 0]
  where
    g (acc, bbb) i =
      let
        bb = filter (\b -> b .&. i == i) bbb
      in
        if length bb >= k then (acc + i, bb) else (acc, bbb)

beautifullnesses :: [Int] -> [Int]
beautifullnesses =
  map sum .
  concatMap (tail . inits) .
  init . tails

bitSize :: Int -> Int
bitSize n = fst $ head $ dropWhile ((n >=) . snd) $ zip [0..] $ iterate (2 *) 1
