Codeforces Round #263 (Div. 2) proC

来源:互联网 发布:斑马网络计划破解软件 编辑:程序博客网 时间:2024/05/23 01:58

题目:

C. Appleman and Toastman
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.


题意分析:

题意还是比较明白。
对原数组进行排序,然后一个递归模拟一下就行了。

代码:

#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <string>#include <iostream>using namespace std;long long a[300005];int main(){    int n;    long long ans;    while(scanf("%d",&n)!=EOF)    {        ans=0;        for(int i=1;i<=n;i++)        {            cin>>a[i];        }        sort(a+1,a+n+1);        for(int i=1;i<=n;i++)        {            a[i]+=a[i-1];        }        ans=a[n];        for(int i=0;i<=n-2;i++)        {            ans+=(a[n]-a[i]);        }        cout<<ans<<endl;    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 ps界面字体太小怎么办 手机qq自动横屏怎么办 联通光猫重置了怎么办 手机掉了至尊宝怎么办 至尊宝换手机了怎么办 至尊宝手机丢了怎么办 咪咕游戏扣费了怎么办 美版4s没有信号怎么办 移动流量用超了怎么办 联通合约机坏了怎么办 联通合约机丢了怎么办 集团v网取消不了怎么办 退订了集团v网怎么办 苹果id两个串号怎么办 无线固话的号码怎么办 手机电话键没了怎么办 翼支付密码忘了怎么办 滴滴驾龄不够3年怎么办 百度云下的太慢怎么办 百度云下的好慢怎么办 网盘下载速度慢怎么办 4g移动卡网速慢怎么办 天翼校园开wifi怎么办 校园卡不给销户怎么办 吃腻了难受恶心怎么办 深夜吃撑了难受怎么办 吃撑了肚子难受怎么办 喝水喝撑了难受怎么办 晚饭吃撑了难受怎么办 吃多了肚子难受怎么办 电信4g信号变3g怎么办 五险交了3月离职怎么办 怎么办工行携程灵通卡 易买得消费卡怎么办 贷款回执单丢了怎么办 医院挂号过号了怎么办 脑子里总是胡思乱想睡不着怎么办 被妹夫给睡了怎么办 被妹夫抱了下怎么办 最里面牙齿烂了怎么办 老公有外遇不承认怎么办