def main():

    N = int(eval(input()))

    A = list(map(int, input().split()))



    avg = sum(A) // N



    ans = 10 ** 10

    

    for i in [avg, avg+1]:

        tmp = 0

        for ai in A:

            x = (i - ai) ** 2

            tmp += x

        ans = min(ans, tmp)



    print(ans)







if __name__ == "__main__":

    main()
def main():

    N = int(eval(input()))

    A = list(map(int, input().split()))



    # avg = sum(A) // N

    avg = sum(A) / N

    avg = round(avg)



    # ans = 10 ** 10

    

    # for i in [avg, avg+1]:

    #     tmp = 0

    #     for ai in A:

    #         x = (i - ai) ** 2

    #         tmp += x

    #     ans = min(ans, tmp)

    ans = 0

    for ai in A:

        x = (avg - ai) ** 2

        ans += x



    print(ans)







if __name__ == "__main__":

    main()
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    avg = sum(A) // N
    ans = 10**10
    for i in [avg, avg + 1]:
        tmp = 0
        for ai in A:
            x = (i - ai) ** 2
            tmp += x
        ans = min(ans, tmp)
    print(ans)


if __name__ == "__main__":
    main()

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    # avg = sum(A) // N
    avg = sum(A) / N
    avg = round(avg)
    # ans = 10 ** 10
    # for i in [avg, avg+1]:
    #     tmp = 0
    #     for ai in A:
    #         x = (i - ai) ** 2
    #         tmp += x
    #     ans = min(ans, tmp)
    ans = 0
    for ai in A:
        x = (avg - ai) ** 2
        ans += x
    print(ans)


if __name__ == "__main__":
    main()

-    avg = sum(A) // N -    ans = 10**10 -    for i in [avg, avg + 1]: -        tmp = 0 -        for ai in A: -            x = (i - ai) ** 2 -            tmp += x -        ans = min(ans, tmp) +    # avg = sum(A) // N +    avg = sum(A) / N +    avg = round(avg) +    # ans = 10 ** 10 +    # for i in [avg, avg+1]: +    #     tmp = 0 +    #     for ai in A: +    #         x = (i - ai) ** 2 +    #         tmp += x +    #     ans = min(ans, tmp) +    ans = 0 +    for ai in A: +        x = (avg - ai) ** 2 +        ans += x