#include <iostream>
#include <algorithm>
#include <vector>
#include<queue>
using namespace std;
typedef long long ll;
class cmp
{
public:
bool operator()(ll a, ll b);
};
bool cmp::operator()(ll a , ll b)
{
return a > b;
}
int main()
{
int N;
while(cin >> N)
{
priority_queue<ll , vector<ll>, cmp> pQueue;
for(int i = 1; i <= N ;i++)
{
ll temp;
cin >> temp;
pQueue.push(temp);
}
ll minCost = 0;
while(pQueue.size() >1 )
{
ll tmpA = pQueue.top();
pQueue.pop();
ll tmpB = pQueue.top();
pQueue.pop();
pQueue.push(tmpA + tmpB);
minCost += tmpA + tmpB;
}
cout << minCost << endl;
while(!pQueue.empty())
pQueue.pop();
}
return 0;
STL 优先队列水过