CodeForces 461A Appleman and Toastman
2016-01-14 10:34
281 查看
[b]CodeForces 461A Appleman and Toastman
[/b]
Time Limit:2000MS Memory
Limit:262144KB 64bit
IO Format:%I64d & %I64u
Description
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 Input
Input
Output
Input
Output
Hint
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.
[/b]
Time Limit:2000MS Memory
Limit:262144KB 64bit
IO Format:%I64d & %I64u
Description
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 Input
Input
3 3 1 5
Output
26
Input
1 10
Output
10
Hint
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<stdio.h> #include<algorithm> using namespace std; int main(){ long long sum,n,i,j,s[444444]; while(scanf("%lld",&n)!=EOF){ sum=0; for(i=0;i<n;i++){ scanf("%lld",&s[i]); sum+=s[i]; } sort(s,s+n); if(n>1){ for(i=0,j=1;i<n;i++){ sum+=s[i]*j; if(i<n-2) j++; } } printf("%lld\n",sum); } }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Gym100342E Minima
- Codeforces Gym100342J Triatrip
- Codeforces Gym100286B Blind Walk (dfs)