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. sentinel.conf 配置

    daemonize yes logfile "/home/data/redis/redis_sentinel.log" sentinel monitor mymaster 192. ...

  2. hlslcc

    https://cdn2.unrealengine.com/Resources/files/UE4_OpenGL4_GDC2014-514746542.pdf ue的跨平台编译器 hlsl cross ...

  3. Unrecognized SSL message, plaintext connection? 将https 换为http 即可

    请求链接:https://59********* 升级后的项目地址有https换为了http  ,出现这个错误,改为http请求即可

  4. 09 深科技相关表结构 (未完成)、git

    1.深科技相关 1. 深科技表结构(6表) 深科技4张+2张用户表 - 深科技 用户表 用户Token 文章来源 文章表 通用评论表 通用收藏表 # ######################## ...

  5. laravel 服务容器的用法

    建立一个服务 <?php namespace App\Services; class FooService { public function __construct(){ } public f ...

  6. 根据xml文件生成javaBean

    原 根据xml文件生成javaBean 2017年08月15日 18:32:26 吃完喝完嚼益达 阅读数 1727 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出 ...

  7. Educational Codeforces Round 33 (Rated for Div. 2) C题·(并查集变式)

    C. Rumor Vova promised himself that he would never play computer games... But recently Firestorm — a ...

  8. 漫谈计算机编码:从ASCII码到UTF-8

    第一阶段 盘古开天辟地——ASCII码 计算机大家都知道,本质是二进制运算和存储.在计算机中人类的几乎所有文字和字符都没法直接表示,所以美国人在发明计算机的时候为了让计算机可以用于保存和传输文字,就发 ...

  9. Ubuntu16.04从源码部署安装禅道过程记录

    1.首先把基础的lamp环境搭建好,这里利用apt安装即可 sudo apt install mysql-server sudo apt install apache2 sudo apt instal ...

  10. MySQL安装及基础命令

    介绍数据库安装基础命令 linux的下载和安装 mac的下载和安装 windows的下载和安装 介绍: 数据库在开发中占据的位置? 数据库能更简单的使用存储在文件中的数据能更好的解决并发问题,数据统一 ...