/*
给你n个数,让你构建一个哈弗曼树,求哈弗曼编码的长度
解:就是求除了叶子节点以外的所有节点的权值和
*/
#include<iostream>
#include<queue>
using namespace std;
typedef long long inta;
struct node
{
inta weight;
bool operator<(const node & a) const
{
return weight>a.weight;
}
};
int main()
{
int size,n,l;
while(cin>>n)
{
priority_queue<node> pq;
long long ans=0;
for(int i=0;i<n;i++)
{
int w;
cin>>w;
node temp;
temp.weight=w;
pq.push(temp);
}
while(pq.size()>1)
{
node n1=pq.top();
pq.pop();
node n2=pq.top();
pq.pop();
node newnode;
newnode.weight=n1.weight+n2.weight;
pq.push(newnode);
ans+=newnode.weight;
}
cout<<ans<<endl;
}
}