题解 | 搬水果

搬水果

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;
}

全部评论

相关推荐

05-08 14:40
广东白云学院
各位大佬方狠狠批评吧
凌offer:投私企,政治面貌不是党员或者预备党员就不要写了,什么族感觉也没啥要写的必要
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务