#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main() {
	int n;
	cin >> n;
	vector<int> s(n + 1);
	s[0] = 0;
	for (int i = 1; i <= n; i++)cin >> s[i];
	sort(s.begin(), s.end());
	int p1 = 1;
	int p2 = n;
    long long res = 0;
	int f = 0;
	while (p1 <= p2) {
		int a = s[p2] - f;
		int b = f - s[p1];
		if(a>=b){
			res += pow(a, 2);
			f = s[p2];
			p2--;
		}
		else {
			res += pow(b, 2);
			f = s[p1];
			p1++;
		}
	}
	cout << res << endl;
	return 0;
}

更多推荐