#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
class cmp
{
public:
bool operator()(const ll a,const ll b)const
{
return a>b;
}
};
int main()
{
int n;
while(~scanf("%d",&n))
{
ll sum=0,t;
priority_queue<ll,vector<ll>,cmp> v;
for(int i=0;i<n;i++)
{
scanf("%lld",&t);
v.push(t);
}
while(v.size()>1)
{
ll a=v.top();
v.pop();
ll b=v.top();
v.pop();
sum+=a+b;
v.push(a+b);
}
printf("%lld\n",sum);
while(!v.empty())
{
v.pop();
}
}
}
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
class cmp
{
public:
bool operator()(const ll a,const ll b)const
{
return a>b;
}
};
int main()
{
int n;
while(~scanf("%d",&n))
{
ll sum=0,t;
priority_queue<ll,vector<ll>,cmp> v;
for(int i=0;i<n;i++)
{
scanf("%lld",&t);
v.push(t);
}
while(v.size()>1)
{
ll a=v.top();
v.pop();
ll b=v.top();
v.pop();
sum+=a+b;
v.push(a+b);
}
printf("%lld\n",sum);
while(!v.empty())
{
v.pop();
}
}
}