A13049 | Grow The Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $OX$ or $OY$ 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.
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 $OX$ or $OY$ 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.
输入格式
The first line contains an integer $n$ ( $1 \le n \le 100\,000$ ) — the number of sticks Alexey got as a present.
The second line contains $n$ integers $a_1, \ldots, a_n$ ( $1 \le a_i \le 10\,000$ ) — the lengths of the sticks.
The second line contains $n$ integers $a_1, \ldots, a_n$ ( $1 \le a_i \le 10\,000$ ) — the lengths of the sticks.
输出格式
Print one integer — the square of the largest possible distance from $(0, 0)$ to the tree end.
输入输出样例
输入 #1
3 1 2 3
输出 #1
26
输入 #2
4 1 1 2 2
输出 #2
20
The following pictures show optimal trees for example tests. The squared distance in the first example equals $5 \cdot 5 + 1 \cdot 1 = 26$ , and in the second example $4 \cdot 4 + 2 \cdot 2 = 20$ .
 
 
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted