51Nod--1117 聪明的木匠(排序)

我们可以反过来想,如何将这几个线段组成一根 并且每次花费是组成的两段的和 

 

#include<bits/stdc++.h>
using namespace std;
#define maxn 50005
#define LL long long
LL  a[maxn],b[maxn],ans=0;
priority_queue<int,vector<int>,greater<int> >q;
int main(){
  LL  n;
  cin>>n;
  for(LL  j=1;j<=n;j++){
     scanf("%lld",&a[j]);
     q.push(a[j]);
  }
  LL ans=0;
  while(q.size()!=1){
     int l1=q.top();
     q.pop();
     int l2=q.top();
     q.pop();
     ans+=l1+l2;
     q.push(l1+l2);
  }
  cout<<ans<<endl;
  return 0;
}

 

全部评论

相关推荐

想去大厂的土拨鼠正在卷:生化环材还劝退?信了张雪峰的鬼话,入了计算机,西北风都喝不到。反而同学校生化环材offer点击即送
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务