Noj Machine Dog assembly cost (priority queue)

Source: Internet
Author: User

Machine-Dog assembly costs time limit (normal/java): ms/4500 MS Running memory limit: 65536 KByte
Total Submissions: 490 tested by: 166

Title Description

SED students have recently been fascinated by the manufacture of robotic dogs and have purchased a large number of required parts that can be assembled into a component that can be assembled into a large component. In the manufacture of robotic dogs, components or parts can only be assembled in 22, in the order of assembly. In a robotic dog, each part has an assembly cost, and the cost of assembling one component at a time is the sum of the cost of each part assembly. Given the cost of each part assembly (in yuan), your task is to help sed calculate how much he spends at least.



Input

The first line includes an integer N, which indicates the number of machine dog parts (1≤n≤10000)

The second behavior is n positive integers, which indicates the cost of assembly per machine-dog part (in units of yuan), separated by a space between integers.

Output

The output is only one line, that is, the minimum cost of machine-dog assembly.

Note: The end of the output section requires an extra blank line.

Sample input

10
1 2 3 4 5 6 7 8 9 0

Sample output

136

Source of the topic

"IBM South Mail Cup" individual race 2009


Title Link: http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1076


Title Analysis: Bare priority queue, priority for small to large, each time take the smallest two out of the team to accumulate and put and queue, until only one element


#include <cstdio> #include <queue>using namespace std;int Const MAX = 1e5 + 5;struct cmp{    bool operator () (I NT A, int b)    {        return a > B;    }}; Priority_queue <int, Vector<int>, cmp> q;int main () {    int n, get;    scanf ("%d", &n);    for (int i = 0; i < n; i++)    {        scanf ("%d", &get);        Q.push (get);    }    int ans = 0;    while (Q.size ()! = 1)    {        int tmp1 = Q.top ();        Q.pop ();        int tmp2 = Q.top ();        Q.pop ();           int tmp = TMP1 + tmp2;        Ans + = tmp;        Q.push (TMP);    }    printf ("%d\n", ans);}



Noj Machine Dog assembly cost (priority queue)

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.