#include<bits/stdc++.h>
using namespace std;
int tree[10001],heh[10000];
int N;
void aju(int root,int n){
int N = n;
int rootpot = root;
int leftchild = rootpot * 2;
while (leftchild <= N)
{
if(leftchild + 1 <= N && tree[leftchild + 1] > tree[leftchild])
{
leftchild++;
}
if(tree[leftchild] > tree[rootpot])
{
swap(tree[leftchild],tree[rootpot]);
rootpot = leftchild;
leftchild = rootpot * 2;
}
else
{
return;
}
}
}
int main()
{
cin>>N;
for(int i = 1; i <= N; i++)
{
cin>>tree[i];
}
for(int i = N >> 1; i >= 1; i--)
{
aju(i,N);
}
int v=N;
for(int i = 1; i <= N - 1; i++)
{
swap(tree[1],tree[N - i + 1]);
aju(1,--v);
}
int sum=0;
for(int i=1;i<=N;i++)
{
heh[i]=tree[i]+tree[i+1];
tree[i+1]=heh[i];
tree[i]=0;
}
for(int i=1;i<=N-1;i++)
{
sum+=heh[i];
}
cout<<sum;
return 0;
}