#!/usr/bin/env python3
n, xs, occupied, left, right = int(input()), list(map(int, input().split())), set(), {}, {}
for x in xs:
    if x not in occupied:
        occupied.add(x)
        left[x], right[x] = x - 1, x + 1
        continue

    leftmost, rightmost, fix_left, fix_right = left[x], right[x], [], []

    while leftmost in occupied:
        fix_left.append(leftmost)
        leftmost = left[leftmost]
    for j in fix_left: left[j] = leftmost

    while rightmost in occupied:
        fix_right.append(rightmost)
        rightmost = right[rightmost]
    for j in fix_right: right[j] = rightmost

    new_x = min(leftmost, rightmost, key=lambda nx: sum((a - b) ** 2 for a, b in zip(xs, sorted((nx, *occupied)))))
    occupied.add(new_x)
    left[new_x], right[new_x] = new_x - 1, new_x + 1

print(sum((a - b) ** 2 for a, b in zip(xs, sorted(occupied))))
