Codeforces Round #263 (Div. 2) proC
题目:
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.
题意分析:
代码:
#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; } }
相关文章
- Codeforces Round #277(Div. 2) (A Calculating Function, B OR in Matrix, C Palindrome Transformation)
- 【Codeforces Round #629 (Div. 3) D】Carousel
- 【Codeforces Round #635 (Div. 2) E】Kaavi and Magic Spell
- 【Codeforces Round #668 (Div. 2) C】Balanced Bitstring
- 【Educational Codeforces Round 88 (Rated for Div. 2) D】Yet Another Yet Another Task
- 【Educational Codeforces Round 88 (Rated for Div. 2) C】 Mixing Water
- 【Codeforces Round #645 (Div. 2) D】The Best Vacation
- 【Codeforces Round #628 (Div. 2) D】Ehab the Xorcist
- 【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap
- 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) A】Palindrome Dance
- 【Codeforces Round #499 (Div. 2) E】Border
- 【Educational Codeforces Round 41 (Rated for Div. 2) D】Pair Of Lines
- 【Codeforces Round #456 (Div. 2) A】Tricky Alchemy
- 【Codeforces Round #451 (Div. 2) C】Phone Numbers
- 【Codeforces Round #452 (Div. 2) D】Shovel Sale
- 【Codeforces Round #445 (Div. 2) C】 Petya and Catacombs
- 【Codeforces Round #299 (Div. 2) D】Tavas and Malekas
- 【44.64%】【codeforces 743C】Vladik and fractions
- 【Codeforces Round #434 (Div. 2) B】Which floor?
- 【codeforces 434 div 1 A】Did you mean...
- 【Codeforces Round #426 (Div. 2) B】The Festive Evening
- Codeforces Round #246 (Div. 2):B. Football Kit
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard
- Codeforces Round #256 (Div. 2) D. Multiplication Table 二分法
- Codeforces Round #311 (Div. 2)A Ilya and Diplomas
- Codeforces #250 (Div. 2) C.The Child and Toy
- Codeforces Round #469 (Div. 2)
- Codeforces Round #462 (Div. 2)
- Codeforces Round #412 Div. 2 第一场翻水水