#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int n;
int main()
{
priority_queue<int,vector<int>,greater<int> >heap; //小根堆借助优先队列实现
cin>>n;
int ans=0;
while (n--)
{
int x;
cin>>x;
heap.push(x);
}
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;
}