One
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n;
vector<int> v;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cin >> n;
int cur;
v.push_back(0);
for (int i = 0; i < n; i++){
cin >> cur;
v.push_back(cur);
}
sort(v.begin(), v.end());
long long sum = 0;
int b = 0, e = v.size()-1;
while (b < e){
sum += (v[e]-v[b])*(v[e]-v[b]);
b++;
sum += (v[e]-v[b])*(v[e]-v[b]);
e--;
}
cout << sum;
return 0;
}
Two
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <vector>
#include <functional>
using namespace std;
int n;
priority_queue<int, vector<int>, greater<int> > ql;
priority_queue<int, vector<int>, less<int> > qb;
vector<int> v;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
int cur;
for (int i = 0; i < n; i++){
cin >> cur;
ql.push(cur);
qb.push(cur);
}
v.push_back(0);
for (int i = 0; i < n/2; i++){
v.push_back(qb.top());
v.push_back(ql.top());
qb.pop(); ql.pop();
}
if (n%2) v.push_back(qb.top());
long long sum = 0;
for (int i = 1; i < v.size(); i++) sum += (v[i]-v[i-1])*(v[i]-v[i-1]);
cout << sum;
return 0;
}