2494: 【普及-】【P4995】跳跳!

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:1 解决:1

题目描述

你是一只小跳蛙,你特别擅长在各种地方跳来跳去。

这一天,你和朋友小 F 一起出去玩耍的时候,遇到了一堆高矮不同的石头,其中第 lns="http://www.w3.org/1998/Math/MathML"> 块的石头高度为 lns="http://www.w3.org/1998/Math/MathML">,地面的高度是 lns="http://www.w3.org/1998/Math/MathML">0=0。你估计着,从第 lns="http://www.w3.org/1998/Math/MathML"> 块石头跳到第 lns="http://www.w3.org/1998/Math/MathML"> 块石头上耗费的体力值为 lns="http://www.w3.org/1998/Math/MathML">()2,从地面跳到第 lns="http://www.w3.org/1998/Math/MathML"> 块石头耗费的体力值是 lns="http://www.w3.org/1998/Math/MathML">()2

为了给小 F 展现你超级跳的本领,你决定跳到每个石头上各一次,并最终停在任意一块石头上,并且小跳蛙想耗费尽可能多的体力值。

当然,你只是一只小跳蛙,你只会跳,不知道怎么跳才能让本领更充分地展现。

不过你有救啦!小 F 给你递来了一个写着 AK 的电脑,你可以使用计算机程序帮你解决这个问题,万能的计算机会告诉你怎么跳。

那就请你——会写代码的小跳蛙——写下这个程序,为你 NOIp AK 踏出坚实的一步吧!

输入

输入一行一个正整数 lns="http://www.w3.org/1998/Math/MathML">,表示石头个数。

输入第二行 lns="http://www.w3.org/1998/Math/MathML"> 个正整数,表示第 lns="http://www.w3.org/1998/Math/MathML"> 块石头的高度 lns="http://www.w3.org/1998/Math/MathML">

输出

输出一行一个正整数,表示你可以耗费的体力值的最大值。

样例输入 复制

2
2 1

样例输出 复制

5

提示

样例解释

两个样例按照输入给定的顺序依次跳上去就可以得到最优方案之一。

数据范围

对于 lns="http://www.w3.org/1998/Math/MathML">1,有 lns="http://www.w3.org/1998/Math/MathML">0<104,且保证 lns="http://www.w3.org/1998/Math/MathML"> 互不相同。

对于 lns="http://www.w3.org/1998/Math/MathML">10% 的数据,lns="http://www.w3.org/1998/Math/MathML">3

对于 lns="http://www.w3.org/1998/Math/MathML">20% 的数据,lns="http://www.w3.org/1998/Math/MathML">10

对于 lns="http://www.w3.org/1998/Math/MathML">50% 的数据,lns="http://www.w3.org/1998/Math/MathML">20

对于 lns="http://www.w3.org/1998/Math/MathML">80% 的数据,lns="http://www.w3.org/1998/Math/MathML">50

对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,lns="http://www.w3.org/1998/Math/MathML">300

#include<bits/stdc++.h>
using namespace std;

int main(){
    long long n,a[305], ans = 0,l,r,mark = 1, tmp = 0;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a,a+n);
    l = 0, r = n-1, mark = 1;
    while (l<=r) {
    	if (mark == 1) {
    		ans +=(a[r] - tmp)*(a[r] - tmp);
    		tmp = a[r];
    		r--;
		}else {
			ans +=(a[l] - tmp)*(a[l] - tmp);
			tmp = a[l];
			l++;  
		}
		mark = 1 - mark;
	}
	cout << ans;
	return 0;
}