Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.
InputLines 2.. N+1: Each line contains a single integer describing the length of a needed plank
3 8 5 8Sample Output
34Hint
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
#include<iostream>
using namespace std;
int main()
{
int n;
long long sum=0.0,*a;
cin>>n;
a=new long long int[n+1];
for(int i=1;i<=n;i++)
cin>>a[i];
while(n>1)//只剩一块木板时结束
{
//求出当前最短和次短的木板
int fir=1,sec=2;
if(a[fir]>a[sec])
swap(fir,sec);
for(int i=3;i<=n;i++)//注意下面的两次不是坐标交换而是赋值
if(a[i]<a[fir])
sec=fir,fir=i;
else if(a[i]<a[sec])
sec=i;
//它们之和需要加入sum,并进入父辈进行再排序
sum+=a[fir]+a[sec];
//由于之后要n--,要把第n项和它们之和这两项进入下一回合参与排序
if(fir==n)
swap(fir,sec);
a[fir]=a[fir]+a[sec];
a[sec]=a[n];
n--;
}
cout<<sum;
delete []a;
return 0;
}
#include<iostream>
using namespace std;
long long ans,heap[20001];
int n,heap_size;
void put(long long t)//置入一个数并建立小根堆
{
heap[++heap_size]=t;
int next,now=heap_size;
while(now>1)
{
next=now>>1;
if(heap[next]<=heap[now])
break;
swap(heap[next],heap[now]);
now=next;
}
}
long long get()//取出最小的那个数,即根
{
long long re=heap[1];
heap[1]=heap[heap_size--];
int now=1,next;
while(now<<1<=heap_size)
{
next=now<<1;
if(next<heap_size&&heap[next+1]<heap[next])
next++;
if(heap[now]<heap[next])
break;
swap(heap[now],heap[next]);
now=next;
}
return re;
}
int main()
{
long long temp;
scanf("%d",&n);
for(int i=1;i<=n;i++)//将数据建成一个小根堆,便于取出最小的那个
scanf("%lld",&temp),put(temp);
for(;n>1;n--)//取出前两小的数并合成一个再次进入树,从n个木板一直合成到1个木板
temp=get()+get(),put(temp),ans+=temp;
printf("%lld",ans);
return 0;
}
#include<iostream>
#include<queue>
using namespace std;
priority_queue<long long,vector<long long>,greater<long long> >h;
int main()
{
int n;
long long temp,x,y,ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&x),h.push(x);
for(;n>1;n--)
{
x=h.top(),h.pop();
y=h.top(),h.pop();
ans+=x+y;
h.push(x+y);
}
printf("%lld",ans);
return 0;
}