您的位置:首页 > 移动开发

CodeForces 462C Appleman and Toastman 贪心

2015-08-05 21:05 393 查看
原题: http://codeforces.com/problemset/problem/462/C

题目:

Appleman and Toastman

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:

Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.

Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.

After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?

Input

The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.

Output

Print a single integer — the largest possible score.

Sample test(s)

input

3

3 1 5

output

26

input

1

10

output

10

Note

Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.

思路:

一堆数,可以无限分,每次分成两部分,分到某部分只有1个的时候,就踢出去,把过程中所有分出来的部分的和加起来,求最大。

比如:3 1 5

第一次是1+3+5这一堆。sum=9。

第二次可以分成1,3+5这两堆,sum=9+1+8=18。

因为1那一堆只剩一个元素了,所以踢掉,我们再分另一堆。

分成3,5两堆,sum=18+3+5=26。

这里我们第二次是还有两种分法的,如果我们先分出3来,sum=9+3+6=18,再把1和5分开,sum=18+1+5=24,。

而先分出5来,sum=9+5+4=18,再把1和3分开,sum=18+1+3=22。

从上面我们可以看出,每次把最小的单独分出来,会得到最大值。

而每次分的时候让一堆只有一个,另一堆尽量最多,可以分更多次,得到的结果也会更大,不信大家和每次平均分比较一下。

既然已经证明了我们每次只把最小的分出来的解法是最优的,那么我们如何计算呢?暴力?10^5个数,再计算每堆的和,时间复杂度是O(n^2),肯定会超时,所以我们可以找规律。

比如1 3 5,显然26=1*2+3*3+5*3,这个不是很好看出规律。

我们再来一组数据,1,2,3,4。

那么我们得到的结果是10 +1+9 +2+7 +3 +4。细心我们又可以发现9=2+3+4,7=3+4,所以这36是等于1*2+2*3+3*4+4*4。

总结规律可得,对于排序后的数组:

sum=a1*2 +a2*3 +a3*4 +……+ a(n-1)*n+an*n’;

所以我们只要按照这样的循环就可以了。

注意:

因为an的值可以取到10^6,n的值可以取到10^5,所以得到的乘积会溢出int,应使用长整型。

代码:

#include <iostream>
#include"stdlib.h"
#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
typedef long long int lint;
const lint N =1000005;

lint a
;
int main()
{
memset(a,0,sizeof(a));
lint n;
scanf("%I64d",&n);
lint ans=0;
for(lint i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
}
sort(a,a+n);
for(lint i=0;i<n;i++)
{
ans=ans+a[i]*(i+2);
}
ans=ans-a[n-1];
printf("%I64d\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: