现在的位置: 首页 > 综合 > 正文

Codeforces Round #263 (Div. 2)-C. Appleman and Toastman

2018年05月02日 ⁄ 综合 ⁄ 共 2258字 ⁄ 字号 评论关闭

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 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.

题意:

给出一群由 n 个数组成的集合你,依次循环执行两种操作:

    (1)每次Toastman得到一个集合,他计算所有数的和,并且将它加入到score里。之后他将这个集合传给Appleman。

   (2)Appleman得到的集合如果只有一个数,就把它弃之,否则将这个集合分成 两个不相交且不空的集合,传回给Toastman.

     这些操作不断执行直到集合个数变为0,也就是通过使集合都变成只有一个元素而一个个扔掉。问如何划分使得score最大。

AC代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#define LL long long
const int Max=300100;
LL s[Max];
using namespace std;
int main()
{
    LL i,j,n,m,sum,num;
    while(cin>>n)
    {
        sum=0;
        for(i=0;i<n;i++)
        {
            cin>>s[i];
            sum+=s[i];
        }
        sort(s,s+n);
        for(i=0;i<n-1;i++)
        {
            sum+=s[i]*(i+1);
        }
        sum+=s[n-1]*(n-1);
        cout<<sum<<endl;
    }
    return 0;
}
/*
10
8 10 2 5 6 2 4 7 2 1
*/

抱歉!评论已关闭.