#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
priority_queue<int> heap;
while(n--)
{
int x;
cin>>x;
heap.push(-x);
}
int ans=0;
while(heap.size()>1)
{
int a=heap.top();
heap.pop();
int b=heap.top();
heap.pop();
ans-=a+b;
heap.push(a+b);
}
cout<<ans<<endl;
return 0;
}