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 ?
n
(1≤?≤100000) — the number of sticks Alexey got as a present.

The second line contains ?
n
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
inputCopy
3
1 2 3
outputCopy
26
inputCopy
4
1 1 2 2
outputCopy
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

解题思路:为了达到题目要求的离远点最远,长要尽可能地长,宽要尽可能地短,将棍棒长度进行排序,对半分,长度为后一半棍棒长度和,宽度为前一半棍棒的长度和。棍棒顺序为一横一竖,不允许多个横着或多个竖着。

AC代码:

#include <iostream>
#include <algorithm>
using namespace std;
long long a[];
int main()
{
long long n,lenx=,leny=;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
sort(a,a+n+);
for(int i=;i<=n;i++)
{
if(i<=n/)
lenx+=a[i];
else
leny+=a[i];
} cout<<lenx*lenx+leny*leny<<endl;
return ;
}

B. Grow The Tree Codeforces Round #594 (Div. 2)的更多相关文章

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

    B. Grow The Tree Gardener Alexey teaches competitive programming to high school students. To congrat ...

  2. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

  3. Codeforces Round #594 (Div. 2)

    传送门 C. Ivan the Fool and the Probability Theory 题意: 给出一个\(n*m\)的方格,现在要给方格中的元素黑白染色,要求任一颜色最多有一个颜色相同的格子 ...

  4. Codeforces Round #594 (Div. 1) D. Catowice City 图论

    D. Catowice City In the Catowice city next weekend the cat contest will be held. However, the jury m ...

  5. Codeforces Round #594 (Div. 1) C. Queue in the Train 模拟

    C. Queue in the Train There are

  6. Codeforces Round #594 (Div. 1) D2. The World Is Just a Programming Task (Hard Version) 括号序列 思维

    D2. The World Is Just a Programming Task (Hard Version) This is a harder version of the problem. In ...

  7. Codeforces Round #594 (Div. 2) A. Integer Points 水题

    A. Integer Points DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS to ...

  8. Codeforces Round #594 (Div. 1) A. Ivan the Fool and the Probability Theory 动态规划

    A. Ivan the Fool and the Probability Theory Recently Ivan the Fool decided to become smarter and stu ...

  9. Codeforces Round #594 (Div. 1)

    Preface 这场CF真是细节多的爆炸,B,C,F都是大细节题,每道题都写了好久的说 CSP前的打的最后一场比赛了吧,瞬间凉意满满 希望CSP可以狗住冬令营啊(再狗不住真没了) A. Ivan th ...

随机推荐

  1. Vmware虚拟机 centos7设置固定IP地址

    参考文章:https://www.cnblogs.com/lfhappy/p/10798400.html      https://blog.csdn.net/u014466635/article/d ...

  2. Codeforces Round #456 (Div. 2) 912E E. Prime Gift

    题 OvO http://codeforces.com/contest/912/problem/E 解 首先把这个数字拆成个子集,各自生成所有大小1e18及以下的积 对于最坏情况,即如下数据 16 2 ...

  3. vs调试代码 提示 未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项。试图加载格式不正确的程序。

    未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项.试图加载格式不正确的程序. 1.调试-选项-项目和解决方案-web项目-勾选对网站和项目使用iis express 64 ...

  4. 洛谷P2110 欢总喊楼记

    洛谷题目链接 乱搞qwq 我们其实可以找规律,对于每个数$x$,我们先求出从$1$~$x$中有多少符合条件的,记为$sum[x]$,那么类似于前缀和,答案自然就是$sum[r]-sum[l-1]$了 ...

  5. HZWER

    我们的征途是星辰大海 2016年3月13日8,8077 尊敬的各位老师.亲爱的同学们: 大家好,我是高三(1)班的黄哲威.今天很荣幸能和大家分享一些有关竞赛的心得体会. 去年7月15日,第32届全国信 ...

  6. 「BZOJ 4565」「HAOI 2016」字符合并「区间状压DP」

    题意 给一个长度为\(n(\leq 300)\)的\(01\)串,每次可以把\(k(\leq 8)\)个相邻字符合并,得到新字符和一定分数,最大化最后的得分 题解 考虑设计dp:\(dp[S][i][ ...

  7. Python地理信息数据可视化

    地图 基础铺垫 定义 地图(map):是指按一定的比例运用符号.颜色.文字标记等描绘显示地球表面的自然地理.行政区域.社会经济状况的图形. 地图绘制步骤 绘制需要展示的地图,获取地图对象,获取每个区域 ...

  8. 网络层中的 IP 协议

    IP协议 IP(IPv4.IPv6)相当于 OSI 参考模型中的第3层——网络层.网络层的主要作用是“实现终端节点之间的通信”.这种终端节点之间的通信也叫“点对点通信”. 网络的下一层——数据链路层的 ...

  9. BOM:浏览器对象

    1.window是javascript的顶级对象        window有很多内置方法 如 alert open 自定义函数 2.window是bom的顶级对象,通常情况下,可以省略 window ...

  10. HDFS CheckPoint && SavePoint

    HDFS CheckPoint && SavePoint 标签(空格分隔): Hadoop HDFS CheckPoint HDFS 将文件系统的元数据信息存放在 fsimage 和一 ...