subtask#1#2#3#4 RE
subtask#5 TLE
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxx=100001;
LL n,b[maxx];
LL sum=0;
queue<LL>q1,q2;
int main()
{
scanf("%lld\n",&n);
for(int i=1;i<=n;i++)
{
LL a;
scanf("%lld",&a);
b[a]++;
}
for(int i=1;i<=maxx;i++)
while(b[i]) b[i]--,q1.push(i);
for(int i=1;i<n;i++)
{
LL x,y;
if(q2.empty()||(!q1.empty()&&q1.front()<q2.front()))
x=q1.front(),q1.pop();
else x=q2.front(),q2.pop();
if(q2.empty()||(!q1.empty()&&q1.front()<q2.front()))
y=q1.front(),q1.pop();
else y=q2.front(),q2.pop();
sum+=x+y;
q2.push(x+y);
}
printf("%lld",sum);
return 0;
}