调皮孩子捅鸟窝还不愿意花钱
弄一个优先队列先接两个短的然后入列再接两个短的到指定长度就好
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include<cstdio>
#include<queue>
using namespace std;
struct node{
long long l;
bool friend operator < (node a,node b){
return a.l>b.l;
}//优先队列按照木棍的长度排序小的优先
}a;
int main(){
int n;
node b;
while(~scanf("%d",&n)){
priority_queue<node> cnt;
for(int i=0;i<n;i++){
scanf("%lld",&a.l);
cnt.push(a);
}
if (n==1) {
printf("%lld\n",a.l);
continue;
}
long long sum=0;
while(!cnt.empty()){
a=cnt.top();
cnt.pop();
if(!cnt.empty()){
b=cnt.top();
cnt.pop();
a.l=a.l+b.l;
sum+=a.l;
cnt.push(a);
}
}
printf("%lld\n",sum);
}
return 0;
}