50分5个RE求助!! 在线等!!
查看原帖
50分5个RE求助!! 在线等!!
524085
yanbinmu楼主2022/8/21 09:06
#include <bits/stdc++.h>
using namespace std;
int s[15005],tail=0;
void up(int x){
	if(s[x/2]>s[x]){
		swap(s[x/2],s[x]);
		up(x/2);
	}
	return;
}
void insert(int x){
	s[++tail]=x;
	if(tail>1)up(tail);
	return;
}
void down(int x){
	int t=2*x;
	if(s[t+1]<s[t] && t<tail){
		t++;
	}
	if(s[t]<s[x] && t<=tail){
		swap(s[t],s[x]);
		down(t);
	}
	return;
}
void del(){
	s[1]=s[tail--];
	down(1);
}
int get(){
	int t=s[1];
	del();
	return t;
}
int main(){
	int n;
	cin>>n;
	for(int i = 1;i<=n;i++){
		int t;
		cin>>t;
		insert(t);
	}
	long long ans=0;
  	while(tail>=2){
	    int t1=get();
	    int t2=get();
	    ans+=(t1+t2);
	    insert(t1+t2);
	}
	cout<<ans;
	return 0;
}
2022/8/21 09:06
加载中...