Codeforces Round #594 (Div. 2) B. Grow The Tree 水题

B. Grow The Tree

Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher's Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.

The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point (0,0). While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to 𝑂𝑋 or 𝑂𝑌 axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.

Alexey wants to make a polyline in such a way that its end is as far as possible from (0,0). Please help him to grow the tree this way.

Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.

Input

The first line contains an integer 𝑛 (1≤𝑛≤100000) — the number of sticks Alexey got as a present.

The second line contains 𝑛 integers 𝑎1,…,𝑎𝑛 (1≤𝑎𝑖≤10000) — the lengths of the sticks.

Output

Print one integer — the square of the largest possible distance from (0,0) to the tree end.

Examples

input
3
1 2 3
output
26
input
4
1 1 2 2
output
20

Note

The following pictures show optimal trees for example tests. The squared distance in the first example equals 5⋅5+1⋅1=26, and in the second example 4⋅4+2⋅2=20.

题意

给你n个木棍,然后每个木棍的长度为a[i],现在你需要沿着(0,0)这个位置,横着一下,竖着一下摆放;问你最大的距离的平方会是多少

题解

问题转换为 已知a+b=C,问你a2+b2最大是多少。
我们令b = (C-a),那么a2+b2 = 2a2+2ac+c2,这个是一个开口向上,以1/2C为对称轴的二次函数,显然a的取值靠近0,或者靠近C的时候取最大值。也就是要让a和b之间的差值尽量大才行。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long a[maxn],sum,sum1;
int n;
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n/2;i++){
		sum1+=a[i];
	}
	cout<<sum1*sum1+(sum-sum1)*(sum-sum1)<<endl;
}
posted @ 2019-11-12 16:08  qscqesze  阅读(332)  评论(0编辑  收藏  举报