Codevs 1063 Merging fruits//Priority queue

Source: Internet
Author: User

1063 Merging Fruits

2004 NOIP National League popularity Group

time limit: 1 sspace limit: 128000 KBtitle level: DiamondTitle Description Description

In an orchard, Toto has beaten down all the fruits and divided them into different heaps according to the different kinds of fruit. A lot decided to synthesize all the fruits.


Each time a merger, a lot can combine the two fruits together, the energy consumed equals two of the weight of the sum of the fruit. It can be seen that all the fruit after the N-1 merger, there is only a pile. The total amount of energy consumed in the merging of fruits equals the physical strength of each merger.


Because it's going to take a lot of effort to bring the fruit home, you need to save as much as you can when you combine the fruit. Assuming that each fruit has a weight of 1, and that the number of fruits and the number of each fruit is known, your task is to design a combination of sequential schemes that will consume the least amount of energy and output this minimum physical cost.


For example, there are 3 kinds of fruit, the number is 1,2,9. The 1 and 2 stacks can be merged first, and the new heap number is 3, which consumes 3 of the energy. Next, the new heap is merged with the original third heap, and a new heap is obtained, with a number of 12, which consumes 12 of the energy. So the total cost of energy =3+12=15. It can be shown that 15 is the minimum physical cost.

Enter a description Input Description

The input consists of two lines, the first line being an integer n (1<=n<=10000), which represents the number of species of fruit. The second line contains n integers, separated by spaces, and the first integer AI (1<=ai<=20000) is the number of fruit of the first I.

Output description Output Description

The output includes one row, which contains only an integer, which is the minimum physical cost. The input data guarantees that this value is less than 231.

Sample input Sample Input

3
1 2 9

Sample output Sample Output

15

Data range and Tips Data Size & Hint

For 30% of data, n<=1000 is guaranteed:
For 50% of data, n<=5000; is guaranteed
For all data, the n<=10000 is guaranteed.

1#include <cstdio>2#include <iostream>3#include <queue>4 using namespacestd;5Priority_queue <int> que;//define que as a priority queue of type int6 intMain ()7 {8     intN;9scanf"%d",&n);Ten      for(intI=0, x;i<n;++i) One     { Ascanf"%d",&x); -Que.push (-X);//Turn it into a small Gan -         //Q.push () to the queue, pressing a new element at the tail of the team the         //for Priority_queue, insert a new element in the appropriate priority-based location -     } -     intans=0; -      for(intI=1, tmp;i<n;++i) +     { -Tmp=que.top ();//returns the value of the element with the highest precedence, +                       //but the element is not deleted (priority queue only) Aans-=que.top (); atQue.pop ();//deletes the first element of the team, but does not return its value -tmp+=que.top (); -ans-=que.top (); - Que.pop (); - Que.push (TMP); -     } incout<<ans<<Endl; -     return 0; to}

Codevs 1063 Merging fruits//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.