1327 merge fruit

Source: Internet
Author: User
Description

In an orchard, many fruits have been laid down and divided into different heaps based on different types of fruits. I decided to combine all the fruits into a pile.
For each merge, you can combine two fruit sets. The physical strength consumed is equal to the sum of the weights of the two fruit sets. It can be seen that after all the fruits have been merged for n-1 times, there is only one pile left. The total amount of physical strength consumed when Merging Results is equal to the sum of physical strength consumed by each merge.
Because we need to make great effort to move these fruits home, we need to save as much effort as possible when merging them. Assuming that each fruit has a weight of 1 and the number of known fruit types and the number of each fruit, your task is to design a combined sequence scheme to minimize the amount of effort required, and output the minimum physical labor consumption value.
For example, there are three fruit types in sequence: 1, 2, and 9. You can merge 1 and 2 heaps first. The number of new heaps is 3, and the physical strength is 3. Next, merge the new heap with the original third heap and obtain the new heap. The number is 12, which consumes 12 resources. Therefore, the total amount of physical strength is 3 + 12 = 15. It can prove that 15 is the minimum physical labor cost.

Input

The input contains two rows. The first row is an integer N (1 <=n <= 10000), indicating the number of fruit types. The second line contains N integers separated by spaces. the I-th integer AI (1 <= AI <= 20000) is the number of fruit I.

Multiple groups of data

Output

The output includes a row. This row contains only one integer, that is, the minimum physical consumption value. The input data must be less than 2 ^ 31.

Sample Input
3 
1 2 9
Sample output
15

 

Greedy problem, sort first

#include <iostream>#include <queue>using namespace std;typedef struct node{    int data;        friend bool operator <(node a , node b)    {                return a.data > b.data;            }    }Point;int main(){        priority_queue<node> Q;        int n , sum;    Point v , a , b , x;        while (scanf ("%d" , &n) != EOF)    {sum = 0;        while (n --){scanf ("%d" , &v.data);Q.push(v);}while (!Q.empty()){            a = Q.top();            Q.pop();            b = Q.top();            Q.pop();            x.data = a.data + b.data;            sum += x.data;            if (!Q.empty())Q.push(x);}printf ("%d\n" , sum);while (!Q.empty()){Q.pop();}    }    return 0;}

 

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.