全TLE求调
查看原帖
全TLE求调
1286101
felixzou楼主2025/7/24 15:58
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N=1e7+10;
const ll inf=1e18;
ll n,sum,a[N],cnt[N];
queue<ll> q1,q2;

void read(ll &x){
	ll f=1;
	x=0;
	char s=getchar();
	while(s<'0'||s>'9'){
		if(s=='-'){
			f=-1;
			s=getchar();
		}
	}
	while(s>='0'&&s<='9'){
		x=x*10+s-'0';
		s=getchar();
	}
	x*=f;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	read(n);
	for(ll i=1;i<=n;i++){
		read(a[i]);
		cnt[a[i]]++;
	}
	for(ll i=1;i<=100005;i++){
		for(ll j=1;j<=cnt[i];j++){
			q1.push(i);
		}
	}
	for(ll i=1;i<n;i++){
		ll x=0,y=0;
		if(q2.empty() || (!q1.empty() && q1.front()<q2.front())){
			x=q1.front();
			q1.pop();
		}else if(!q2.empty()){
			x=q2.front();
			q2.pop();
		}
		if(q2.empty() || (!q1.empty() && q1.front()<q2.front())){
			y=q1.front();
			q1.pop();
		}else if(!q2.empty()){
			y=q2.front();
			q2.pop();
		}
		sum+=x+y;
		q2.push(x+y);
	}
	cout<<sum<<"\n";
    return 0;
}
2025/7/24 15:58
加载中...