#include<stdio.h>
#include
using namespace std;
priority_queue<int,vector,greater> que;
int main(){
int n;
while(scanf("%d",&n) != EOF){
while(que.empty() == false) que.pop();
for(int i =0; i< n;i++){
int x ;
scanf("%d",&x);
que.push(x);
}
int arr =0;
while(que.size() > 1){
int a,b;
a = que.top();
que.pop();
b = que.top();
que.pop();
arr += a+b;
que.push(a+b);
}
printf("%d\n",arr);
}
return 0;
}