#include <bits/stdc++.h>
using namespace std;
int main() {
    //迭代求带权路径和
    int n, leaf;
    while (cin >> n) {
        priority_queue<int> q;
        for (int i = 0; i < n; i++) {
            cin >> leaf;
            q.push(-leaf);//存入相反数
        }
        int res = 0;
        while (q.size() > 1) {
            int leaf1 = q.top();
            q.pop();
            int leaf2 = q.top();
            q.pop();
            res = res + leaf1 + leaf2;
            q.push(leaf1 + leaf2);
        }
        cout << -res << endl;
    }

    return 0;
}