import Control.Applicative
import Control.Monad
import Data.List

f :: Int -> Int -> Int
f a b = (a - b) ^ 2

main :: IO ()
main = do
    n <- readLn :: IO Int
    as <- sort . fmap read . words <$> getLine :: IO [Int]
    let k = length as
    let a1 = sum as `div` k
    let a2 = (sum as + (k - 1)) `div` k
    let ans = min (sum $ fmap (f a1) as) (sum $ fmap (f a2) as)
    print ans