#include <iostream> #include <string> #include <cstring> #include <queue> #include <vector> using namespace std; priority_queue<int, vector<int>, greater<int>>q; int main() { int n; int sum_=0; cin >> n; if(n==1){cout << "0"; return 0;} for (int i = 0; i < n; i++) { int t; cin >> t; q.push(t); } while (!q.empty()) { int a = q.top(); q.pop(); if (!q.empty()) { int b = q.top(); q.pop(); sum_ += (a + b); if(!q.empty()) q.push(a + b); } else sum_ += a; } cout << sum_ << endl; }