题解 | 搬水果

搬水果

https://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a

#include<iostream>
#include<queue>

using namespace std;
int main() {
	priority_queue<int,vector<int>,greater<int>>heap;//小根堆
	int n;
	while(cin>>n){
		if(n==0)break;
		for(int i=1;i<=n;i++){
			int x;
			cin>>x;
			heap.push(x);
		}
		int res = 0;
		while(!heap.empty()){
			int x=heap.top();
			heap.pop();
			int y=0;
			if(!heap.empty()){
				y=heap.top();
				heap.pop();
			}
			else break;
			heap.push(x+y);
			res+=x+y;
		}
		cout<<res<<endl;
	}
	return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 10:39
一个证都没&nbsp;我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 17:10
点赞 评论 收藏
分享
Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 11:29
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务