import Data.Foldable (and, or, product, sum)
import Data.Function

import qualified Data.List as L

main :: IO ()
main = do
    n  <- read <$> getLine
    as <- getSeq
    print $ solve n as

getSeq  = map read . words <$> getLine   :: Read a => IO [a]

solve :: Int -> [Int] -> Int
solve n as =
  let
    mean = ceiling $ ((/) `on` fromIntegral) (sum as) n
    mode = L.head . L.maximumBy (compare `on` length) $ L.group as
    replaceTo :: Int -> Int
    replaceTo y = sum $ L.map (`cost` y) as
  in
    min (replaceTo mean) (replaceTo mode)

cost :: Int -> Int -> Int
cost x y = (x - y) ^ 2
