#!/bin/sh
read n ; read a
if [ `echo -e "${a// /\n}" | sort -u | wc -w` -eq 1 ]
then
	echo 0
	exit
fi
x=0 ; y=0 ; z=0
for i in $a
do
	x=$(( x + i ))
done
y=$(( x / n ))
[[ $(( x % n )) -gt $(( n / 2 )) ]] && y=$(( y + 1 ))
for i in $a
do
	z=$(( z + $(( $(( i - y )) ** 2 )) ))
done
echo $z
