#include<queue>
#include<functional>//如果在VS环境下,必须加上这一个头文件,因为greater模板在里边
#include<stdio.h>
using namespace std;
priority_queue< int , vector<int> ,greater<int> > Q;//建立一个大顶堆
int main()
{
int n,x;
scanf("%d",&n);
while(Q.empty()==false)
for(int i=0;i<n;i++)
{
scanf("%d",&x);
Q.push(x);
}
int ans=0;
while(Q.size()>1)
{
int a=Q.top();
Q.pop();
int b=Q.top();
Q.pop();
ans+=a+b;
Q.push(a+b);
}
printf("%d\n",ans);
system("pause");
return 0;
}