#include<bits/stdc++.h>

using namespace std;

int main(){

int n;
cin>>n;
priority_queue<int,vector<int>,greater<int>> minh;

int a[n];
for(int i=0;i<n;i++){
  cin>>a[i];
  minh.push(a[i]);
}
int ans=0;
while(minh.size()>=2){
  int t1=minh.top();
  minh.pop();
  int t2=minh.top();
  minh.pop();
  ans=ans+(t1+t2);
  minh.push(ans);
}

cout<<ans<<endl;

return 0;
}


Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: