#include<stdio.h>
int heap[100000],size=0;
void push(int x)
{
int i=size++;
while(i>0)
{
int j=(i-1)/2;
if(heap[j]<=x) break;
heap[i]=heap[j];
i=j;
}
heap[i]=x;
}
int pop()
{
int res=heap[0],temp=heap[--size];
int i=0;
while(2*i+1<size)
{
int j=2*i+1;
while(j+1<size && heap[j+1]<heap[j]) j++;
if(heap[j]>=temp) break;
heap[i]=heap[j];
i=j;
}
heap[i]=temp;
return res;
}
int main()
{
int n,a[100000],i,x,y;
long long sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
push(a[i]);
}
while(size>1)
{
x=pop();
y=pop();
sum+=x+y;
push(x+y);
}
printf("%ld\n",sum);
return 0;
}