Codeforces Round #536 (Div. 2)C. Lunar New Year and Number Division【贪心】

 思路:根据题意,我们肯定将其分成n/2 个集合,这样平方和才会小,我们将其排序,然后不断将一个小的和一个大的组成一个集合,这样最后将其平方相加就行了,注意范围。下面是证明,感兴趣可以看一下

Codeforces Round #536 (Div. 2)C. Lunar New Year and Number Division【贪心】

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 3e5 + 10;
int a[maxn], b[maxn];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
        scanf("%d", &a[i]);
    sort(a, a + n);
    int l = 0, r = n - 1, cnt = 0;
    while(l < r)
    {
        b[cnt++] = a[l] + a[r];
        ++l, --r;
    }
    ll sum = 0;
    for(int i = 0; i < cnt; ++i)
        sum += b[i] * b[i];
    cout << sum << endl;
    return 0;
}