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. 微信 使用wScratchPad 组件时 出错

    说是由于android版本bug 可访问 https://github.com/websanova/wScratchPad/issues/15 解决方法: $("#wScratchPad c ...

  2. Vue-项目重要配置

    Vue配置axios ''' 1)安装插件(一定要在项目目录下): >: cnpm install axios 2)在main.js中配置: import axios from 'axios' ...

  3. node.js http模块和fs模块上机实验·

    httpserver const httpserver = require('http'); var server = httpserver.createServer(function (req,re ...

  4. python大数据初探--pandas,numpy代码示例

    import pandas as pd import numpy as np dates = pd.date_range(',periods=6) dates import pandas as pd ...

  5. spring-boot-configuration-processor 是干啥用的

    spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了 引入pom依赖 <de ...

  6. httpClient请求响应延迟

    客户端可以先向服务器端发送一个请求,如果服务器端返回的是状态码100,那么客户端就可以继续把请求体的数据发送给服务器端.这样在某些情况下可以减少网络开销. 再看看HttpClient里面对100-Co ...

  7. HDU 6063 - RXD and math | 2017 Multi-University Training Contest 3

    比赛时候面向过题队伍数目 打表- - 看了题解发现确实是这么回事,分析能力太差.. /* HDU 6063 - RXD and math [ 数学,规律 ] | 2017 Multi-Universi ...

  8. 使用yum命令出错:SyntaxError: invalid syntax 由于用户取消而退出

    详见: https://blog.csdn.net/qq_24880013/article/details/90731617 必须修改的两个yum配置文件: 因为yum使用python2,因此替换为p ...

  9. C++ 头文件的理解

    变量.函数在使用前必须被声明.至于函数里干了什么,编译时不关注,链接(link)时,才会去搜寻所有编译后的文件,寻找函数具体干了什么. *.h头文件干的事情就像“复制-粘贴”,哪里引用,就把*.h内容 ...

  10. 在一个非默认的位置包含头文件stdafx.h

    如果stdafx.h和你当前的工程不在一个文件夹下,当你在代码中第一行写下#include "stdafx.h"时,VC编译器会根据编译规则(相关的规则请查看MSDN)来区别std ...