#include<iostream>
#include<cstdio>
#include<queue>
   
using namespace std;

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        priority_queue<int ,vector<int>,greater<int>> myPriorityQueue;
        while(n--){
            int x;
            scanf("%d",&x);
            myPriorityQueue.push(x);            //把输入的权值输入到队列中
        }
        int answer=0;                                //answer==WPL
        while(myPriorityQueue.size() > 1){
            int a=myPriorityQueue.top();
            myPriorityQueue.pop();
            int b=myPriorityQueue.top();
            myPriorityQueue.pop();
            answer += a+b;
            myPriorityQueue.push(a+b);            //把算的的answer放入队列中在运算
        }
        printf("%d\n",answer);
    }
    return 0;
}