/**************************************************************** 
 * Description: LQ1009 不用优先队列
 * Author: Alex Li
 * Date: 2024-08-18 20:53:58
 * LastEditTime: 2024-08-18 20:54:05
****************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>  // 引入算法库以使用 sort 函数

using namespace std;

int main() {
    int n;
    cin >> n;  // 输入元素的个数
    
    vector<int> weights(n);  // 创建一个向量存储所有元素的权重
    
    for (int i = 0; i < n; ++i) {
        cin >> weights[i];  // 输入每个元素的权重
    }
    
    int totalCost = 0;  // 总的构建代价
    
    // 进行哈夫曼树的构建过程
    while (weights.size() > 1) {
        // 对当前的权重数组进行排序，从小到大
        sort(weights.begin(), weights.end());
        
        // 取出最小的两个数（权重最小的两个节点）
        int first = weights[0];
        int second = weights[1];
        
        int cost = first + second;  // 计算这两个节点合并的代价
        totalCost += cost;  // 累加到总代价中
        
        // 移除已使用的两个元素
        weights.erase(weights.begin());  // 删除第一个元素
        weights.erase(weights.begin());  // 删除第二个元素（此时它已经是新的第一个元素）
        
        // 将合并后的新节点权重加入数组
        weights.push_back(cost);
    }
    
    cout << totalCost << endl;  // 输出最终的总代价
    
    return 0;
}
