A. Appleman and Toastman

来源:互联网 发布:网络启动安装系统 编辑:程序博客网 时间:2024/05/16 06:39

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 a1a2, ..., 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
33 1 5
output
26
input
110
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.


解题说明:此题是一道贪心题,为了保证最后的得分最大,需要避免在一开始扔掉值较大的数,所以对数组进行从小到大排序。通过例子能够看到第i个位置的值都被计算了i+2词,最后的最大值单独计算即可。


#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstdlib>#include<cstring>using namespace std;long long n,a[400000],ans;int main(){int i;    cin>>n;    for(i=0;i<n;i++)    {cin>>a[i];}sort(a,a+n);    for(i=0;i<n-1;i++)    {ans+=a[i]*(i+2);}ans+=a[n-1]*n;    cout<<ans<<endl;return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 奶牛产奶脂肪低怎么办 新生儿混合喂养不喝水怎么办 小孩子咳嗽怎么办有什么偏方 混合喂养宝宝不喝奶粉怎么办 混合喂养宝宝不吃奶粉怎么办 混合喂养宝宝不吃奶瓶怎么办 混合喂养的宝宝不吃奶粉怎么办 三个月的宝宝突然不吃奶粉怎么办 四个月宝宝拉绿屎推拿怎么办 三个月大的宝宝消化不良怎么办 婴儿大便常规正常的腹泻怎么办 宝宝十个月母乳不够吃怎么办 吃药上吐腹泻在3怎么办 房子买了新生儿户口怎么办 吃海鲜拉稀拉水怎么办 吃海鲜腹泻拉水怎么办 两个月宝宝不爱睡觉怎么办 2个月宝宝干呕怎么办 9个月宝宝拉稀的怎么办 宝宝拉肚子快一个月了怎么办 满月宝宝发烧38度怎么办 大人腹泻10天了怎么办 十个月宝贝拉水怎么办 十个月宝宝脱水哭闹怎么办 宝宝肠胃不好老是拉肚子怎么办 七个月的宝宝老是拉肚子怎么办 6个月宝宝拉肚子怎么办 没满月宝宝吐奶怎么办 八个月宝宝有点拉肚子怎么办 4个月宝宝拉水怎么办 2岁宝宝消化不好拉肚子怎么办 两岁宝宝老拉肚子怎么办 2岁宝宝拉肚子老不好怎么办 2岁宝宝一直拉肚子不好怎么办 5天新生儿拉稀水怎么办 4天新生儿拉稀水怎么办 新生儿40天拉稀水怎么办 一周多的宝宝拉肚子怎么办 出生半个月的宝宝拉肚子怎么办 刚出生几天的宝宝拉肚子怎么办 刚出生的宝宝拉肚子怎么办