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?
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.
Print a single integer — the largest possible score.
3 3 1 5
26
1 10
10
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.
解题思路:
题目大意就是每次可以把一个数组分成两部分,每一部分的和加起来,一直分····一直分····求最后能得到的
最大的和。
刚开始暴力模拟······但写完后发现它其实是有规律的,于是写了个规律。以第一个为例,排完序为5、3、1,
它的式子是 9 + 1 + 8 + 3 + 5 = 26·········最开始和是9,然后最后一个数是1······接下来是8(看成9 - 1),然后是
倒数第二数3······这样看出规律了吧·····至于为什么这样取·······我只能说很明显这样是最大的嘛··········这里用k记
录前两个数的差,最后需要判断下k是否为0(因为可能已经从后扫到头退出循环了,可是还有剩下的,比如这里的5)。
最后·····sum用__int64存·····它的数据里sum会加到超过int。
完整代码:
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;
/** Constant List .. **/ //{
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
int a[10000001];
bool cmp(int a , int b)
{
return a > b;
}
int main()
{
#ifdef DoubleQ
freopen("in.txt","r",stdin);
#endif
int n;
while(~scanf("%d",&n))
{
memset(a , 0 , sizeof(0));
__int64 sum = 0 ;
for(int i = 0; i < n ; i ++)
{
scanf("%d",&a[i]);
sum += a[i];
}
sort(a , a + n , cmp );
__int64 k = sum;
int j = n - 1;
sum = 0;
while(j != 0)
{
sum += k + a[j];
k -= a[j];
j--;
}
if(k != 0)
sum += k;
printf("%I64d\n",sum);
}
}