题解:
简单贪心
要注意的地方是体力要用long long 型定义
代码:
#include <iostream>
#include <algorithm>
using namespace std;
int a[305];
bool myCompare(int aa, int bb) {
return aa > bb;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int h;
cin >> h;
a[i] = h;
}
sort(a, a + n, myCompare);
long long int sum = 0;
sum += a[0] * a[0];
int i = 0;
int j = n-1;
int flag = 0;
while (i <= j) {
flag++;
sum += (a[i] - a[j]) * (a[i] - a[j]);
if (flag % 2 == 1) {
i++;
}
else {
j--;
}
}
cout << sum << endl;
return 0;
}