Ultraviolet A 10954-Add All

Source: Internet
Author: User

Original question:
Yup !! The problem name reflects your task; just add a set of numbers. but you may feel yourselves condescended, to write a C/C ++ program just to add a set of numbers. such a problem will simply question your erudition. so, let's add some flavor of ingenuity to it.
 
Addition operation requires cost now, and the cost is the summation of those two to be added. so, to add 1 and 10, you need a cost of 11.If you want to add 1, 2 and 3. there are several ways-
 
1 + 2 = 3, cost = 3
3 + 3 = 6, cost = 6
Total = 9
1 + 3 = 4, cost = 4
2 + 4 = 6, cost = 6
Total = 10
2 + 3 = 5, cost = 5
1 + 5 = 6, cost = 6
Total = 11
 
I hope you have understood already your mission, to add a set of integers so that the cost is minimal.
 
Input
Each test case will start with a positive number, N (2 ≤ N ≤5000) followed by N positive integers (all are less than 100000 ). input is terminated by a case where the value of N is zero. this case shoshould not be processed.
 
Output
For each case print the minimum total cost of addition in a single line.
 
Sample input:
3
1 2 3
4
1 2 3 4
0

Sample output:
9
19


Question:
There are some numbers. We need to sum up all these numbers to calculate their sum. When an addition is not executed, the result of this addition is a cost. Addition can be performed in any number order, so the order is different. Calculate the minimum total cost.

Analysis summary:
According to the greedy idea, in order to minimize the total cost, the final result must be the least if each time is spent.
The priority queue can be used, so that the two numbers obtained each time for addition must be the smallest two in the queue, and the cost is also the least.

Code:
[Cpp]
/*
* Ultraviolet A: 10954-Add All
* Result: Accept
* Time: 0.024 s
* Author: D_Double
*/
# Include <cstdio>
# Include <queue>
# Define maxn5005
Using namespace std;
 
Priority_queue <long, vector <long>, greater <long> que;
 
Int main (){
Int N;
Long m;
 
While (scanf ("% d", & N ){
While (! Que. empty () que. pop ();
For (int I = 0; I <N; ++ I ){
Scanf ("% lld", & m );
Que. push (m );
}
Long sum = 0, ans = 0, a, B, t;
While (! Que. empty ()){
A = que. top ();
Que. pop ();
If (que. empty () break;
B = que. top ();
Que. pop ();
Sum = a + B;
Ans + = sum;
Que. push (sum );
}
Printf ("% lld \ n", ans );
}
Return 0;
}


Author: shuangde800

Related Article

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.