UVA 10954 Add All (sort)

Source: Internet
Author: User

UVA 10954 ADD All



yup!! The problem name reflects your task; Just add a set of numbers. But the feel yourselves condescended, to write a C + + program just to add a set of numbers. Such a problem would 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, and the was added. So, to add1 and ten, you need a cost of one. If you want to add1, 2 and 3. There is 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 has understood already your mission, to add a set of integers so that's the cost is minimal.

Input

Each test case would start with a positive number, N (2≤n≤5000) followed byN positive integers (all a Re less than 100000). Input is terminated by a case where the value ofN is zero. This case is should not being processed.

Output

For each case, print the minimum total cost of addition

Sample input Output for sample input

3

1 2 3

4                                                                                                           &NBSP

1 2 3 4

0

     

9

19





The main idea: to give the number of N, to add the number of n, each time the sum of the value of the calculation of the time, complete all the summation operation, the total amount of computation required is minimal.

Solution Ideas: Each time to find the smallest two number of additions, record this sum, and then put and put back the original array to replace the previous two number, repeat the operation until the remaining and, and then the previous record and the rest and all add, is the answer.


#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm>using namespace  Std;int num[5005], Sum[5005];int main () {int n;while (scanf ("%d", &n), N) {for (int i = 0; i < n; i++) {scanf ("%d", &num[i]);} int cnt = 0, Cnt2 = 0;while (cnt < n-1) {sort (num + cnt, num + N); num[cnt + 1] + = num[cnt];sum[cnt2++] = num[cnt + 1] ; cnt++;} int Sum = 0;for (int i = 0; i < Cnt2; i++) {sum + = Sum[i];} printf ("%d\n", Sum);} return 0;}



UVA 10954 Add All (sort)

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.