#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int main()
{
int n;
cin>>n;
priority_queue<int,vector<int>,greater<int>> heap;
while(n--)
{
int x;
scanf("%d",&x);
heap.push(x);
}
int res=0;
while(heap.size()>1)
{
int x=heap.top();
heap.pop();
int y=heap.top();
heap.pop();
int sum=x+y;
res+=sum;
heap.push(sum);
}
printf("%d\n",res);
}