import Data.List

main = do
    getLine
    line<-getLine
    let a =[read x::Int | x <- words line]
    let lwbd = minL a
    let upbd = maxL a
    print $ minL [totalCost a z | z<-[lwbd..upbd]] 

totalCost :: [Int]->Int -> Int
totalCost [] p = 0
totalCost (a:ax) p = (a-p)*(a-p) + totalCost ax p 

minL :: [Int] -> Int
minL (x:[]) = x
minL (x:xs) = min x $ minL xs

maxL :: [Int] -> Int
maxL (x:[]) = x
maxL (x:xs) = max x $ maxL xs