Codeforces Round #594 (Div. 2) B. Grow The Tree
题意
给你几个棍子,问如何摆(垂直)使得两端距离最远
思路
可以当成一个直角三角形来理解
把给的棍子从小到大排序 把前半部分加起来作为直角三角形一条边 后半部分加起来当做一条边
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int a[(int)1e6+7];
int main(void)
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
sort(a, a + n);
ll s1 = 0, s2 = 0;
for (int i = 0; i < n; ++i)
{
if (i < n / 2)
{
s1 += a[i];
}
else
{
s2 += a[i];
}
}
printf("%lld\n", s1 * s1 + s2 * s2);
return 0;
}