#include #include using namespace std; int main(){ priority_queue, greater> pq; int n; cin >> n; int tmp; int sum = 0; for(int i = 0; i < n; i++){ cin >> tmp; sum += tmp; pq.push(tmp); } long long ans = 0; while(pq.size() > 1){ int x = pq.top(); pq.pop(); int y = pq.top(); pq.pop(); pq.push(x + y); ans += (x + y); } cout << ans << endl; return 0; }