#include<queue> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #define M 20010 using namespace std; int n; int l[M]; long long ans,sum; priority_queue< int,vector<int>,greater<int> > Q; void solve(){ for(int i=1;i<=n;i++){ Q.push(l[i]); sum+=l[i]; } long long now=sum; for(int i=1;i<=n;i++){ int tmp=Q.top(); Q.pop(); if(Q.empty()) break; tmp+=Q.top(); Q.pop(); ans+=tmp; Q.push(tmp); } } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&l[i]); solve(); printf("%lld\n",ans); }