P1334 Rui wooden board, p1334 wooden board

Source: Internet
Author: User

P1334 Rui wooden board, p1334 wooden board
Description

Rui wants to personally repair the barrier around a farm. He measured the barrier and found that he needed N (1 ≤ N ≤ 20,000) Boards, each with an integer of Li (1 ≤ Li ≤ 50,000 ). As a result, he purchased a wooden board that was long enough and the length was the sum of the length of the required N wooden boards. He decided to cut the Board into N wooden boards. (Riui does not produce wood chips when cutting boards, and does not need to consider the length of loss during cutting.) riui uses a special method when cutting boards, this method consumes x units of energy when dividing a template with x length into two. Rui has endless energy, but now he advocates saving energy, so as an example, he decided to save energy as much as possible. Apparently, the total need to cut the N-1 times, the problem is, how should we cut each time? Calculate the minimum amount of energy consumed by programming.

Input/Output Format Input Format:

Line 1: integer N, indicating the number of required boards

Rows from 2nd to N + 1: Each behavior has an integer representing the length of a board.

Output Format:

An integer that represents the sum of the minimum amount of energy consumed

Input and Output sample Input example #1:
3858
Output sample #1:
34
Description

The first cut of a board with a length of 21 is 8 and 13, consuming 21 units of energy, for the second time, 13 boards are cut to 5 or 8 in length, which consumes 13 units of energy and consumes 34 units of energy. This is the solution with the minimum energy consumption.

 

Xiaogen heap

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #define lli long long int  7 using namespace std; 8 void read(lli & n) 9 {10     char c='+';lli x=0;lli flag=0;11     while(c<'0'||c>'9')12     {13         c=getchar();14         if(c=='-')flag=1;15     }16     17     while(c>='0'&&c<='9')18         x=x*10+c-48,c=getchar();19     if(flag==1)n=-x;20     else n=x;21 }22 priority_queue<lli,vector<lli>,greater<lli> >q;23 lli n,p;24 lli ans;25 int main()26 {27     read(n);28     for(int i=1;i<=n;i++)29     {30         read(p);31         q.push(p);32     }33     for(int i=1;i<=n-1;i++)34     {35         lli x=q.top();36         q.pop();37         lli y=q.top();38         q.pop();39         x=x+y;40         ans+=x;41         q.push(x);42     }43     cout<<ans;44     return 0;45 }

 

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.