program ABC043C
    implicit none
    integer(8)::N,cost,result,i,j
    integer(8),allocatable,dimension(:)::A

    read(5,*)N
    allocate(A(N))
    read(5,*)(A(i),i=1,N)

    do i=minval(A),maxval(A)
        cost=0
        do j=1,N
            cost=cost+(i-A(j))**2
        end do

        if(i==minval(A))then
            result=cost
        else if(cost<result)then
            result=cost
        end if
    end do

    print'(i0)',result
end program ABC043C