import Data.Array.Unboxed

main = print =<< sol <$> get <*> get

get = fmap read . words <$> getLine

sol [n,m] as = fst . foldr upd (0,0) . filter ((>0) . snd) $ assocs hs
  where
  ar = listArray (1,n) as :: UArray Int Int
  amax = maximum as
  amin = minimum as
  hs = accumArray (+) 0 (2*amin,2*amax)
    [(ar!i + ar!j,1) | i <- [1..n], j <- [1..n]] :: UArray Int Int
  upd (i,e) (s,c) 
    | c < m     = (s+i*d,c+d)
    | otherwise = (s,c)
    where d = min e (m-c)