Codeforces 263C. Appleman and Toastman

Source: Internet
Author: User

C. Appleman and ToastmanTime limit per test 2 secondsmemory limit per test megabytesinput Standard Input Output Standard Output

Appleman and Toastman play a game. Initially Appleman gives one group of nnumbers to the Toastman, then they start-to-complete the following TAS Ks:

    • 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 the this group is out. Each time Appleman gets a group consisting of more than one number, he splits the group into the Non-empty groups (he can Do it on any-gives each of the 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, ..., a c15>n (1≤ ai ≤106)-the Initial group that's 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 the groups: [3, 5] and [1]. Both of them should is given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he'll 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-to: [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'll throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he'll throws it out). Finally Toastman has added 9 + 1 + 8 + 5 + 3 = the score. This is the optimal sequence of actions.

Problem-solving: greedy, then messed up, incredibly right.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <climits>7#include <vector>8#include <queue>9#include <cstdlib>Ten#include <string> One#include <Set> A#include <stack> - #defineLL Long Long - #definePII pair<int,int> the #defineINF 0x3f3f3f3f - using namespacestd; - Const intMAXN =300100; - LL D[maxn],sum,ans; + intN; - intMain () { +      while(~SCANF ("%d",&N)) { A          for(inti = SUM =0; I < n; i++){ atCin>>D[i]; -Sum + =D[i]; -         } -Sort (d,d+n); -Ans =sum; -          for(inti =0; i+1< n; i++){ inAns + =sum; -Sum-=D[i]; to  +         } -cout<<ans<<Endl; the     } *     return 0; $}
View Code

Codeforces 263C. Appleman and Toastman

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.