solve :: [Int] -> Int
solve a = foldl (\acc i -> acc + (i - t)^2) 0 a
  where
    ceilDiv t s = (t + s - 1) `div` s
    t = ceilDiv (sum a) (length a)

main :: IO ()
main = do
    _ <- getLine
    a <- map read . words <$> getLine :: IO [Int]
    print $ solve a