module Main where

import Data.List (nub, sort)

getNums :: IO [Integer]
getNums = map (read :: String -> Integer) . words <$> getLine

zipWithIndex :: [a] -> [(a, Int)]
zipWithIndex xs = zip xs [1,2..]

makeUniquePairs :: (Eq a) => [a] -> [(a, a)]
makeUniquePairs xs = nub [(a, b) | a <- xs, b <- xs]

dropSecond :: [((x, y), (x, y))] -> [(x, x)]
dropSecond = map (\((v, i), (v', i')) -> (v, v'))

main :: IO ()
main = do
    [n, m] <- map (read :: String -> Int) . words <$> getLine
    as <- getNums
--    let guests = take m $ reverse . sort $ zipWithIndex as
    let guests = take ((*2) . round $ sqrt (fromIntegral m)) $ reverse . sort $ zipWithIndex as
    let pairs = makeUniquePairs guests
    let pairs' = dropSecond pairs
    let shakeValues = reverse . sort $ map (\(a, b) -> a + b) pairs'

    let result = foldr (+) 0 (take m shakeValues)

    putStrLn $ show result