module Main where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import Data.Maybe (fromJust)

main = do
  n <- readLn :: IO Int
  as <- map (fst . fromJust . B8.readInt) . B8.words <$> BS.getLine
  print $ solve as n
  return ()

solve :: [Int] -> Int -> Int
solve as n = min (calcSum as (k `div` n)) (calcSum as (k `div` n + 1))
  where
    k = sum as

calcSum :: [Int] -> Int -> Int
calcSum xs n = sum $ map (\x -> (x - n)*(x-n)) xs
