51 nod 1117 wise carpenter

Topic source: Hebei University Algorithm Art Association
Base Time Limit: 1 second Space Limit: 131072 KB Score: 20Difficulty  : Level 3 Algorithm Questions
An old carpenter needs to cut a long wooden stick into N pieces. The length of each segment is L1, L2, ..., LN (1 <= L1, L2, ..., LN <= 1000, and all are integers) length units. We consider cutting only at integer points without loss of wood.
The carpenter found that the physical strength spent on each cut is proportional to the length of the stick, so it may take 1 unit of physical strength to cut a stick with a length of 1. For example: if N=3, L1 = 3, L2 = 4, L3 = 5, the original length of the stick is 12, and the carpenter can cut it in various ways, such as: first cut 12 into 3+9. It costs 12 stamina , then cut 9 into 4+5, spend 9 stamina, and spend a total of 21 stamina; you can also cut 12 into 4+8, spend 12 stamina, then cut 8 into 3+5, spend 8 stamina, and spend a total of 20 physical strength. Obviously, the latter is more energy efficient than the former.
So, how much physical effort does a carpenter have to spend at least to complete the cutting task?
Input
Line 1: 1 integer N (2 <= N <= 50000)
Lines 2 - N + 1: 1 integer Li per line (1 <= Li <= 1000).
Output
Output with minimal physical exertion.
Input example
3
3
4
5
Output example
19
#include <iostream>
#include <queue>
using namespace std;
int a[50005];
intmain()
{
	int i,n;
	cin>>n;
	priority_queue<int,vector<int>,greater<int> >q;     //type container functional
	for(i=0; i<n; ++i)
	{
		cin>>a[i];
		q.push(a[i]);
	}
	long long sum=0;
	while(q.size()>1)
	{
		int a1=q.top();
		q.pop();
		int a2=q.top();
		q.pop();
		sum+=a1+a2;
		q.push(a1+a2);
	}
	cout<<sum<<endl;
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324722442&siteId=291194637