program main
	implicit none
    integer::n,t,i
    integer(8)::ans1,ans2
    integer,allocatable::a(:)
    real::ave
    read(*,*)n
    allocate(a(n))
    read(*,*)a
    ans1=0
    ans2=0
    ave=sum(a)/real(n,8)
    t=int(ave)
    do i=1,n
    	ans1=ans1+(a(i)-t)**2
    end do
    do i=1,n
    	ans2=ans2+(a(i)-(t+1))**2
    end do
    write(*,*) min(ans1,ans2)
    
    
    
    deallocate(a)
    stop
end program main
