import           Data.Bits ((.&.))
import           Data.List (foldl1', inits, tails, sort)

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 =
  foldl1' (.&.) .
  take k .
  reverse .
  sort .
  map sum .
  concatMap (tail . inits) .
  init . tails
