#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;
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(ll i=1;i<=n;i++){
cin>>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;
}
#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;
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(ll i=1;i<=n;i++){
scanf("%lld",&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;
}
把cin改成scanf为啥直接少了60分啊?