#include<iostream>
#include<queue>
#include<string>
#include<cmath>
using namespace std;



int main() {
    int N;
    scanf("%d", &N);
    priority_queue<int, vector<int>, greater<int>>q;
    while (N--) {
        int data;
        scanf("%d", &data);
        q.push(data);
    }
    int weights = 0;
    while (q.size() > 1) {
        int a = q.top();
        q.pop();
        int b = q.top();
        q.pop();
        int c = a + b;
        weights = weights + c;
        q.push(c);
    }
    cout << weights;

}