#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

#define let auto
#define fn auto
#define usize size_t
using namespace std;

fn fastio() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

fn main() -> int {
    fastio();

    
    usize n;
    cin >> n;
    let lengths = vector<usize>(n);
    for (let &l: lengths) {
        cin >> l;
    }

    let gt = std::greater<usize>();

    make_heap(lengths.begin(), lengths.end(), gt);
    
    usize cost = 0;
    while (lengths.size() > 1) {
        pop_heap(lengths.begin(), lengths.end(), gt);
        let a = lengths.back();
        lengths.pop_back();

        pop_heap(lengths.begin(), lengths.end(), gt);
        let b = lengths.back();
        lengths.pop_back();
        
        cost += a + b;
        lengths.push_back(a + b);
        push_heap(lengths.begin(), lengths.end(), gt);
    }

    cout << cost << endl;
}