#include <bits/stdc++.h>
using namespace std;
int n,a[11451419],tong[114514],l,r;
long long ans, x;
long long b[11451419];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n;i++)
{
cin >> x;
tong[x]++;
}
int k=0;
for(int i=0;i<=100000;i++)
for(int j=1;j<=tong[i];j++)
a[k++]=i;
int i = 0;
while(1)
{
if(i>=n)
{
if(l+1==r)
break;
x = b[l++];
ans += x + b[l];
b[r++]=(x + b[l++]);
}
else
{
if(l!=r and b[l]<=a[i])
x = b[l++];
else
x = a[i++];
if(l!=r and (i>=n or b[l]<=a[i]))
{
ans += x + b[l];
b[r++]=(x + b[l++]);
}
else
{
ans += x + a[i];
b[r++]=(x + a[i++]);
}
}
}
cout << ans;
return 0;
}