Imports System.Collections.Generic
Imports System.Console

Module Program
    Sub Main()
        Dim N As Integer = NextInt()
        Dim a(N - 1) As Integer
        For i As Integer = 0 To N - 1
            a(i) = NextInt()
        Next

        Dim ans As Integer = Integer.MaxValue
        For k As Integer = Min(a) To Max(a)
            Dim cnt As Integer = 0
            For Each ai As Integer In a
                cnt += CInt((ai - k) ^ 2)
            Next
            ans = Min(New Integer() {ans, cnt})
        Next

        WriteLine(ans)
    End Sub

    Function Max(a() As Integer) As Integer
        Dim result As Integer
        For i As Integer = 0 To UBound(a)
            If i = 0 Or a(i) > result Then result = a(i)
        Next
        Return result
    End Function

    Function Min(a() As Integer) As Integer
        Dim result As Integer
        For i As Integer = 0 To UBound(a)
            If i = 0 Or a(i) < result Then result = a(i)
        Next
        Return result
    End Function

    Function NextInt() As Integer
        Return CInt(NextString())
    End Function

    Function NextString() As String
        Dim result As New List(Of Char)
        Do
            Dim tmp As Integer = Read()
            If tmp < 0 Then Exit Do
            Dim nextChar As Char = Chr(tmp)

            If Not Char.IsWhiteSpace(nextChar) Then
                result.Add(nextChar)
            ElseIf nextChar <> vbCr Then
                Exit Do
            End If
        Loop
        Return New String(result.ToArray())
    End Function
End Module
