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

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

typedef size_t usize;
typedef ssize_t isize;
typedef int32_t i32;
typedef u_int32_t u32;
typedef int64_t i64;
typedef u_int64_t u64;

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

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

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

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

    make_heap(lengths.begin(), lengths.end(), gt);

    u64 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;
}