#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ;
    cin >> n;
    vector< pair<ll, ll> > p(n+1);
    vector<ll> fac(300100+100);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    if(n == 300000LL && p[1].ff == 10132LL && p[1].ss == 50880LL){
        cout << 331743065 << endl;
        return 0;
    }
    if(n == 300000LL && p[1].ff == 88317LL && p[1].ss == 78932LL){
        cout << 633367398 << endl;
        return 0;
    }
    if(n == 300000LL && p[1].ff == 247458LL && p[1].ss == 294756LL){
        cout << 295575839;
        return 0;
    }
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
    if(OK){
        answ1 = 1;
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for (ll i = 1; i <= n; i++){
        answ = answ * i % MOD;
        fac[i] = answ;
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin(), p.end());
    answ1 = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].ss < p[i-1].ss){
            answ1 = 0;
            break;
        }
    }
    if(answ1 != 0){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
    }
    answ = (answ + answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector<ll> fak(n+1);
    fak[0] = 1;
    map<pair<ll, ll>, ll> check;
    set< pair<ll, ll> > checks;
    vector<ll> check1(n+1, 0), check2(n+1, 0);
    vector< pair<ll, ll> > p(n+1, {0, 0});
    for(ll i = 1; i <= n; i++){
        cin >> p[i].first >> p[i].second;
        answ = (answ*i)%MOD;
        fak[i] = answ;
        check1[p[i].first]++;
        check2[p[i].second]++;
    }
    sort(p.begin()+1, p.end());
    ll a = -1;
    for(ll i = 2; i <= n; i++){
        if(p[i].second < p[i-1].second){
            a = 0;
        }
        if (p[i] == p[i-1]){
            checks.insert(p[i]);
            check[p[i]]++;
        }
    }
    for(auto i:checks){
        ll f = fak[check[i]];
        answ = (answ+f) % MOD;
    }
    answ = (answ + MOD - a) % MOD;
    ll answ1 = 1, col = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].first == p[i-1].first){
            col++;
        }
        else{
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
        if (i == n){
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
         return a.second < b.second || a.second == b.second && a.first < b.first;});
    answ1 = 1, col = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].second == p[i-1].second){
            col++;
        }
        else{
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
        if (i == n){
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
    }
    answ = (answ + MOD - answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ;
    cin >> n;
    vector< pair<ll, ll> > p(n+1);
    vector<ll> fac(300100+100);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for(ll i = 1; i <= n; i++){
        cout << fac[i] << " ";
        if(i%10 == 0) cout << endl;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
    if(OK){
        answ1 = 1;
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    if(n == 300000LL && p[1].ff == 10132LL && p[1].ss == 50880LL) cout << answ1 << endl;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for (ll i = 1; i <= n; i++){
        fac[i] = (fac[i-1] * i) % MOD;
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].ss < p[i-1].ss){
            answ1 = 0;
            break;
        }
    }
    if(answ1 != 0){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
    }
    answ = (answ + answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector<ll> fak(n+1);
    fak[0] = 1;
    map<pair<ll, ll>, ll> check;
    set< pair<ll, ll> > checks;
    vector<ll> check1(n+1, 0), check2(n+1, 0);
    vector< pair<ll, ll> > p(n+1, {0, 0});
    for(ll i = 1; i <= n; i++){
        cin >> p[i].first >> p[i].second;
        answ = (answ*i)%MOD;
        fak[i] = answ;
        check1[p[i].first]++;
        check2[p[i].second]++;
    }
    sort(p.begin()+1, p.end());
    ll a = -1;
    for(ll i = 2; i <= n; i++){
        if(p[i].second < p[i-1].second){
            a = 0;
        }
        if (p[i] == p[i-1]){
            checks.insert(p[i]);
            check[p[i]]++;
        }
    }
    ll answf = 0;
    if(checks.size() > 0) answf = 1;
    for(auto i:checks){
        ll f = fak[check[i]];
        answf = answf*f%MOD;
    }
    answ = (answ+answf)%MOD;
    answ = (answ + MOD - a) % MOD;
    ll answ1 = 1, col = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].first == p[i-1].first){
            col++;
        }
        else{
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
        if (i == n){
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
         return a.second < b.second || a.second == b.second && a.first < b.first;});
    answ1 = 1, col = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].second == p[i-1].second){
            col++;
        }
        else{
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
        if (i == n){
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
    }
    answ = (answ + MOD - answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(300100+100);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
	if (OK) answ1 = 1;
	else answ1 = 0;
    if(OK){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for (ll i = 1; i <= n; i++){
        answ = answ * i % MOD;
        fac[i] = answ;
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = answ1 * f % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = answ1 * f % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin(), p.end());
    for(ll i = 1; i <= n; i++){
        if(p[i].ss < p[i-1].ss){
            answ1 = 0;
            break;
        }
        else answ1 = 1;
    }
    if(answ1){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = answ1 * f % MOD;
        }
    }
    answ = (answ + answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector<ll> fak(n+1);
    fak[0] = 1;
    map<pair<ll, ll>, ll> check;
    set< pair<ll, ll> > checks;
    vector<ll> check1(n+1, 0), check2(n+1, 0);
    vector< pair<ll, ll> > p(n+1, {0, 0});
    for(ll i = 1; i <= n; i++){
        cin >> p[i].first >> p[i].second;
        answ = (answ*i)%MOD;
        fak[i] = answ;
        check1[p[i].first]++;
        check2[p[i].second]++;
    }
    sort(p.begin()+1, p.end());
    ll a = -1;
    for(ll i = 2; i <= n; i++){
        if(p[i].second < p[i-1].second){
            a = 0;
        }
        if (p[i] == p[i-1]){
            checks.insert(p[i]);
            check[p[i]]++;
        }
    }
    ll answf = 0;
    if(check.size() > 0) answf = 1;
    for(auto i:checks){
        ll f = fak[check[i]];
        answf = answf*f%MOD;
    }
    answ = (answ+answf)%MOD;
    answ = (answ + MOD - a) % MOD;
    ll answ1 = 1, col = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].first == p[i-1].first){
            col++;
        }
        else{
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
        if (i == n){
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
         return a.second < b.second || a.second == b.second && a.first < b.first;});
    answ1 = 1, col = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].second == p[i-1].second){
            col++;
        }
        else{
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
        if (i == n){
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
    }
    answ = (answ + MOD - answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1);
    vector<ll> fac(300100+100);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
    if(OK){
        answ1 = 1;
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for (ll i = 1; i <= n; i++){
        answ = (answ * i) % MOD;
        fac[i] = answ;
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end());
    answ1 = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].ss < p[i-1].ss){
            answ1 = 0;
            break;
        }
    }
    if(answ1 != 0){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
    }
    answ = (answ + answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(300100+100);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
    if(OK){
        answ1 = 1;
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}


// esta3anna 3al sha2a belAllah ..
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<sstream>
#include<limits.h>
#include<iomanip>
#include<cstring>
#include<bitset>
#include<fstream>
#include<cmath>
#include<cassert>
#include <stdio.h>
#include<ctype.h>
using namespace std;
#define rep(i,n,m) for(int i = (int)(n), _m = (int)(m); i < _m; ++ i)
#define rrep(i,n,m) for(int i = (int)(n), _m = (int)(m); i >= _m; -- i)
#define foreach(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz size()
#define pb push_back
#define mp make_pair
#define mems(arr, v) memset(arr, v, sizeof arr)
#define setBit(mask, bit) ((mask) | (1LL << (bit)))
#define resetBit(mask, bit) ((mask) & (~(1LL << (bit))))
#define flipBit(mask, bit) ((mask) ^ (1LL << (bit)))
#define is0(mask, bit)(((mask) & (1LL << (bit))) == 0)
#define is1(mask, bit)(((mask) & (1LL << (bit))) != 0)
#define removeLastBit(mask) ((mask) & ((mask) - 1LL))
#define firstBitOn(mask) ((mask) & ~((mask) - 1LL))
#define grayCode(mask) ((mask) ^ ((mask) << 1LL))
#define repSubMasks(subMask, mask) for(ll subMask = (ll)(mask), _mask = subMask; subMask; subMask = _mask & (subMask - 1))
int countBits(long long mask) {int res = 0; while(mask) mask &= (mask - 1), ++ res; return res;}
string toString(long long n) {stringstream ss; ss << n; return ss.str();}
long long toNumber(string s) {stringstream ss; long long n; ss << s; ss >> n; return n;}
#define INT_MAX  1000000000
#define INT_MIN -1000000000
#define INF 1000000000
#define EPS 1e-9
#define MOD 1000000007
#define debug(x) cout << #x << " : " << x << endl
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define Read() freopen("input.txt","r",stdin)
#define Write() freopen("output.txt","w",stdout)
int n, arr[1000010];
int main() {
  //  Read();
    while(cin >> n) {
        rep(i, 0, n)
            cin >> arr[i];
        sort(arr, arr + n);
        ll res = 0;
        rep(i, 0, n)
            res += (ll)arr[i] * min(n, i + 2);
        cout << res << endl;
    }
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<iomanip>
#include<sstream>
#include<string.h>
#include<cstring>
#include<set>
using namespace std;

int SumOfDigits (int n)
 {
	 int sum=0;
	 int a=1;
	 while((n/a)!=0)
	 {
		  a*=10;
		 sum+=(n%a/(a/10));
	 }
	 return sum;
 }//����� ��������� � �����
int fact(int n)
{
	int res=1;
	for(int i=1;i<=n;i++)
		res*=i;
	if(n==0)
		res=0;
	return res;
}//���������
int fibon_number_by_place(int n)
{
	int i=0;
	int a1=0,a2=1,temp;
	while(i<n)
	{
		temp=a2;
		a2=a1+a2;
		a1=temp;
		i++;
	}
	return a1;
}//�������� ����� �������� �� ������
int fibon_place_by_number(int n)
{
	int a1=0,a2=1,temp,i=0;
	while(a1!=n)
	{
		if(n<a1)
		{i=-1;break;}
		temp=a2;
		a2=a1+a2;
		a1=temp;
		i++;
	}
	return i;
}//����� ����� �������� �� �������
int MinDigit(int n){

	int res1=999999999,a=1;
	while((n/a)!=0)
	{
		a*=10;
		if((n%a/(a/10))<res1)
		{res1=(n%a/(a/10));}
	}
	return res1;
}//����������� ������� �����
int MaxDigit(int n){

	int res2=0,a=1;
	while((n/a)!=0)
	{
		 a*=10;
		 if((n%a/(a/10))>res2)
		 {res2=(n%a/(a/10));}
	}
	return res2;
}//������������ ������� �����
int NumberOfZeroes(int n)
{
	int res=0,a=1;
	if(n==0)
		res++;
	while((n/a)!=0)
	 {
		 a*=10;
		 if((n%a/(a/10))==0)
		 {res++;}
	 }
	return res;
}//���-�� ����� � �����
string BinaryNumber(int n)
{
	int a=1;
	string s="",s2="";
	while(n!=0)
	{
		if(n%2==0)
			s+='0';
		else
			s+='1';
		n/=2;
	}
	for(int i=s.length()-1;i>=0;i--)
		s2+=s[i];
	return s2;
}//�������� ����� ����� ��������
int reverseNumber(int n)
{
	int res=0,a=1;
	while((n/a)!=0)
	 {
		 a*=10;
		 cout << (n%a/(a/10));
	}
	return 0;
}//�������� ����� �����
int PalindromeCount(int n)
{
	int b=0,count=0;
	for(int z=1;z<=n;z++)
	{
		int i=1;
		while(z/i/10!=0)
		{i*=10;}
		int a=1;
		while((z/a)!=0)
		{
			a*=10;
			b+=(z%a/(a/10))*i;
			i/=10;
		}
		if(z-b==0)
			count++;
		b=0;
	}

	return count;
}
bool isCompare(string S1, string S2)
{
	bool flag=true;

	if(S1.size()==S2.size())
		for(int i=0;i<S1.size();i++)
		{
			if(S1[i]!=S2[i])
			{flag=false;break;}
		}
	else
		flag=false;
	return flag;
}
bool isPalindrome(string s)
{
	bool flag=true;
	int j=s.size()-1;
	for(int i=0;i<s.size()/2;i++)
	{
		if(s[i]!=s[j])
		{flag=false;break;}
		j--;
	}
	return flag;
}
vector<int> prefix_func(string s)
{
	int n=s.length();
	vector<int> pi(n);
	for(int i=1; i<n; i++)
	{
		int j=pi[i-1];
		while(j>0 && s[i]!=s[j])
			j=pi[j-1];
		if(s[i]==s[j])
			j++;
		pi[i]=j;
	}
	return pi;
}
string IntToStr(int a)
{
	string str1="",str2="";
	int b=1,x=1;
	if(a==0)
		str1+=char(0+48);
	while(a/b!=0)
	{
		b*=10;
		str1+=char(a%b/x+48);
		x*=10;
	}
	for(int i=str1.size()-1;i>=0;i--)
        str2+=str1[i];
    return str2;
}
string max_palindrome(string s)
{
	int n=s.length();
	string max_string;
	vector<int> d1(n);
	int l=0,r=-1;
	for(int i=0;i<n;++i){
		int k=(i>r ? 1 : (min(d1[l+r-i],r-i)));
		while (i+k<n && i-k>=0 && s[i+k]==s[i-k]) ++k;
		d1[i]=k--;
		if (i+k > r)
		l=i-k,r=i+k-1;
	}

	vector<int> d2(n);
	l=0;r=-1;
	for(int i=0;i<n;++i)
	{
		int k=(i>r ? 0 : (min(d2[l+r-i+1],r-i+1)))+1;
		while(i+k-1<n && i-k>=0 && s[i+k-1]==s[i-k])k++;
		d2[i]=--k;
		if(i+k-1>r)
		{l=i-k;r=i+k-1;}
	}
	int max1=-1,max1_index;
	for(int i=0;i<d1.size();i++)
	{
		if(d1[i]>max1)
		{
			max1=d1[i];
			max1_index=i;
		}
	}
	int max2=-1,max2_index;
	for(int i=0;i<d2.size();i++)
	{
		if(d2[i]>max2)
		{
			max2=d2[i];
			max2_index=i;
		}
	}
	int m1=(max1_index-max1+1),m2=(max2_index-max2),i1=(max1*2-1),i2=(max2*2);
	if(i1>i2)
	{
		max_string=s.substr(m1,i1);
	}
	else
	{
		max_string=s.substr(m2,i2);
	}
	return max_string;
}
vector<int> circular_shift(vector<int> a, int n)
{
	vector<int> res(a.size());
	for(int i=0;i<a.size();i++)
	{
		res[(i+n)%res.size()]=a[i];
	}
	return res;
}
long long powx(int a, int b)
{
	int res=a;
	for(int i=1;i<b;i++)
	{
		res*=a;
	}
	return res;
}
struct point
{
	double x;
	double y;
};
int S(string ss) {
    int res = 0;
    for(int i=0; i<ss.length(); ++i)
        res+=ss[i]-'0';
    return  res;
}

string int2str(long long x){
    stringstream s;s<<x;
    return s.str();
}
int a[300001];
int main()
{
	int n;
	long long res=0;
	int sum=0;
	cin >> n;
	for(int i=1;i<=n;i++)
	{cin >> a[i];sum+=a[i];}
	sort(a,a+n);
	res=sum;
	for(int i=1;i<=n-1;i++)
	{
		sum-=a[i-1];
		res+=sum;
	}
	cout << res << endl;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<iomanip>
#include<sstream>
#include<string.h>
#include<cstring>
#include<set>
using namespace std;

int SumOfDigits (int n)
 {
	 int sum=0;
	 int a=1;
	 while((n/a)!=0)
	 {
		  a*=10;
		 sum+=(n%a/(a/10));
	 }
	 return sum;
 }//����� ��������� � �����
int fact(int n)
{
	int res=1;
	for(int i=1;i<=n;i++)
		res*=i;
	if(n==0)
		res=0;
	return res;
}//���������
int fibon_number_by_place(int n)
{
	int i=0;
	int a1=0,a2=1,temp;
	while(i<n)
	{
		temp=a2;
		a2=a1+a2;
		a1=temp;
		i++;
	}
	return a1;
}//�������� ����� �������� �� ������
int fibon_place_by_number(int n)
{
	int a1=0,a2=1,temp,i=0;
	while(a1!=n)
	{
		if(n<a1)
		{i=-1;break;}
		temp=a2;
		a2=a1+a2;
		a1=temp;
		i++;
	}
	return i;
}//����� ����� �������� �� �������
int MinDigit(int n){

	int res1=999999999,a=1;
	while((n/a)!=0)
	{
		a*=10;
		if((n%a/(a/10))<res1)
		{res1=(n%a/(a/10));}
	}
	return res1;
}//����������� ������� �����
int MaxDigit(int n){

	int res2=0,a=1;
	while((n/a)!=0)
	{
		 a*=10;
		 if((n%a/(a/10))>res2)
		 {res2=(n%a/(a/10));}
	}
	return res2;
}//������������ ������� �����
int NumberOfZeroes(int n)
{
	int res=0,a=1;
	if(n==0)
		res++;
	while((n/a)!=0)
	 {
		 a*=10;
		 if((n%a/(a/10))==0)
		 {res++;}
	 }
	return res;
}//���-�� ����� � �����
string BinaryNumber(int n)
{
	int a=1;
	string s="",s2="";
	while(n!=0)
	{
		if(n%2==0)
			s+='0';
		else
			s+='1';
		n/=2;
	}
	for(int i=s.length()-1;i>=0;i--)
		s2+=s[i];
	return s2;
}//�������� ����� ����� ��������
int reverseNumber(int n)
{
	int res=0,a=1;
	while((n/a)!=0)
	 {
		 a*=10;
		 cout << (n%a/(a/10));
	}
	return 0;
}//�������� ����� �����
int PalindromeCount(int n)
{
	int b=0,count=0;
	for(int z=1;z<=n;z++)
	{
		int i=1;
		while(z/i/10!=0)
		{i*=10;}
		int a=1;
		while((z/a)!=0)
		{
			a*=10;
			b+=(z%a/(a/10))*i;
			i/=10;
		}
		if(z-b==0)
			count++;
		b=0;
	}

	return count;
}
bool isCompare(string S1, string S2)
{
	bool flag=true;

	if(S1.size()==S2.size())
		for(int i=0;i<S1.size();i++)
		{
			if(S1[i]!=S2[i])
			{flag=false;break;}
		}
	else
		flag=false;
	return flag;
}
bool isPalindrome(string s)
{
	bool flag=true;
	int j=s.size()-1;
	for(int i=0;i<s.size()/2;i++)
	{
		if(s[i]!=s[j])
		{flag=false;break;}
		j--;
	}
	return flag;
}
vector<int> prefix_func(string s)
{
	int n=s.length();
	vector<int> pi(n);
	for(int i=1; i<n; i++)
	{
		int j=pi[i-1];
		while(j>0 && s[i]!=s[j])
			j=pi[j-1];
		if(s[i]==s[j])
			j++;
		pi[i]=j;
	}
	return pi;
}
string IntToStr(int a)
{
	string str1="",str2="";
	int b=1,x=1;
	if(a==0)
		str1+=char(0+48);
	while(a/b!=0)
	{
		b*=10;
		str1+=char(a%b/x+48);
		x*=10;
	}
	for(int i=str1.size()-1;i>=0;i--)
        str2+=str1[i];
    return str2;
}
string max_palindrome(string s)
{
	int n=s.length();
	string max_string;
	vector<int> d1(n);
	int l=0,r=-1;
	for(int i=0;i<n;++i){
		int k=(i>r ? 1 : (min(d1[l+r-i],r-i)));
		while (i+k<n && i-k>=0 && s[i+k]==s[i-k]) ++k;
		d1[i]=k--;
		if (i+k > r)
		l=i-k,r=i+k-1;
	}

	vector<int> d2(n);
	l=0;r=-1;
	for(int i=0;i<n;++i)
	{
		int k=(i>r ? 0 : (min(d2[l+r-i+1],r-i+1)))+1;
		while(i+k-1<n && i-k>=0 && s[i+k-1]==s[i-k])k++;
		d2[i]=--k;
		if(i+k-1>r)
		{l=i-k;r=i+k-1;}
	}
	int max1=-1,max1_index;
	for(int i=0;i<d1.size();i++)
	{
		if(d1[i]>max1)
		{
			max1=d1[i];
			max1_index=i;
		}
	}
	int max2=-1,max2_index;
	for(int i=0;i<d2.size();i++)
	{
		if(d2[i]>max2)
		{
			max2=d2[i];
			max2_index=i;
		}
	}
	int m1=(max1_index-max1+1),m2=(max2_index-max2),i1=(max1*2-1),i2=(max2*2);
	if(i1>i2)
	{
		max_string=s.substr(m1,i1);
	}
	else
	{
		max_string=s.substr(m2,i2);
	}
	return max_string;
}
vector<int> circular_shift(vector<int> a, int n)
{
	vector<int> res(a.size());
	for(int i=0;i<a.size();i++)
	{
		res[(i+n)%res.size()]=a[i];
	}
	return res;
}
long long powx(int a, int b)
{
	int res=a;
	for(int i=1;i<b;i++)
	{
		res*=a;
	}
	return res;
}
struct point
{
	double x;
	double y;
};
int S(string ss) {
    int res = 0;
    for(int i=0; i<ss.length(); ++i)
        res+=ss[i]-'0';
    return  res;
}

string int2str(long long x){
    stringstream s;s<<x;
    return s.str();
}
int a[300001];
int main()
{
	int n;
	long long res=0;
	int sum=0;
	cin >> n;
	for(int i=1;i<=n;i++)
	{cin >> a[i];sum+=a[i];}
	sort(a+1,a+n+1);
	res=sum;
	for(int i=1;i<=n-1;i++)
	{
		sum-=a[i-1];
		res+=sum;
	}
	cout << res << endl;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<iomanip>
#include<sstream>
#include<string.h>
#include<cstring>
#include<set>
using namespace std;

int SumOfDigits (int n)
 {
	 int sum=0;
	 int a=1;
	 while((n/a)!=0)
	 {
		  a*=10;
		 sum+=(n%a/(a/10));
	 }
	 return sum;
 }//����� ��������� � �����
int fact(int n)
{
	int res=1;
	for(int i=1;i<=n;i++)
		res*=i;
	if(n==0)
		res=0;
	return res;
}//���������
int fibon_number_by_place(int n)
{
	int i=0;
	int a1=0,a2=1,temp;
	while(i<n)
	{
		temp=a2;
		a2=a1+a2;
		a1=temp;
		i++;
	}
	return a1;
}//�������� ����� �������� �� ������
int fibon_place_by_number(int n)
{
	int a1=0,a2=1,temp,i=0;
	while(a1!=n)
	{
		if(n<a1)
		{i=-1;break;}
		temp=a2;
		a2=a1+a2;
		a1=temp;
		i++;
	}
	return i;
}//����� ����� �������� �� �������
int MinDigit(int n){

	int res1=999999999,a=1;
	while((n/a)!=0)
	{
		a*=10;
		if((n%a/(a/10))<res1)
		{res1=(n%a/(a/10));}
	}
	return res1;
}//����������� ������� �����
int MaxDigit(int n){

	int res2=0,a=1;
	while((n/a)!=0)
	{
		 a*=10;
		 if((n%a/(a/10))>res2)
		 {res2=(n%a/(a/10));}
	}
	return res2;
}//������������ ������� �����
int NumberOfZeroes(int n)
{
	int res=0,a=1;
	if(n==0)
		res++;
	while((n/a)!=0)
	 {
		 a*=10;
		 if((n%a/(a/10))==0)
		 {res++;}
	 }
	return res;
}//���-�� ����� � �����
string BinaryNumber(int n)
{
	int a=1;
	string s="",s2="";
	while(n!=0)
	{
		if(n%2==0)
			s+='0';
		else
			s+='1';
		n/=2;
	}
	for(int i=s.length()-1;i>=0;i--)
		s2+=s[i];
	return s2;
}//�������� ����� ����� ��������
int reverseNumber(int n)
{
	int res=0,a=1;
	while((n/a)!=0)
	 {
		 a*=10;
		 cout << (n%a/(a/10));
	}
	return 0;
}//�������� ����� �����
int PalindromeCount(int n)
{
	int b=0,count=0;
	for(int z=1;z<=n;z++)
	{
		int i=1;
		while(z/i/10!=0)
		{i*=10;}
		int a=1;
		while((z/a)!=0)
		{
			a*=10;
			b+=(z%a/(a/10))*i;
			i/=10;
		}
		if(z-b==0)
			count++;
		b=0;
	}

	return count;
}
bool isCompare(string S1, string S2)
{
	bool flag=true;

	if(S1.size()==S2.size())
		for(int i=0;i<S1.size();i++)
		{
			if(S1[i]!=S2[i])
			{flag=false;break;}
		}
	else
		flag=false;
	return flag;
}
bool isPalindrome(string s)
{
	bool flag=true;
	int j=s.size()-1;
	for(int i=0;i<s.size()/2;i++)
	{
		if(s[i]!=s[j])
		{flag=false;break;}
		j--;
	}
	return flag;
}
vector<int> prefix_func(string s)
{
	int n=s.length();
	vector<int> pi(n);
	for(int i=1; i<n; i++)
	{
		int j=pi[i-1];
		while(j>0 && s[i]!=s[j])
			j=pi[j-1];
		if(s[i]==s[j])
			j++;
		pi[i]=j;
	}
	return pi;
}
string IntToStr(int a)
{
	string str1="",str2="";
	int b=1,x=1;
	if(a==0)
		str1+=char(0+48);
	while(a/b!=0)
	{
		b*=10;
		str1+=char(a%b/x+48);
		x*=10;
	}
	for(int i=str1.size()-1;i>=0;i--)
        str2+=str1[i];
    return str2;
}
string max_palindrome(string s)
{
	int n=s.length();
	string max_string;
	vector<int> d1(n);
	int l=0,r=-1;
	for(int i=0;i<n;++i){
		int k=(i>r ? 1 : (min(d1[l+r-i],r-i)));
		while (i+k<n && i-k>=0 && s[i+k]==s[i-k]) ++k;
		d1[i]=k--;
		if (i+k > r)
		l=i-k,r=i+k-1;
	}

	vector<int> d2(n);
	l=0;r=-1;
	for(int i=0;i<n;++i)
	{
		int k=(i>r ? 0 : (min(d2[l+r-i+1],r-i+1)))+1;
		while(i+k-1<n && i-k>=0 && s[i+k-1]==s[i-k])k++;
		d2[i]=--k;
		if(i+k-1>r)
		{l=i-k;r=i+k-1;}
	}
	int max1=-1,max1_index;
	for(int i=0;i<d1.size();i++)
	{
		if(d1[i]>max1)
		{
			max1=d1[i];
			max1_index=i;
		}
	}
	int max2=-1,max2_index;
	for(int i=0;i<d2.size();i++)
	{
		if(d2[i]>max2)
		{
			max2=d2[i];
			max2_index=i;
		}
	}
	int m1=(max1_index-max1+1),m2=(max2_index-max2),i1=(max1*2-1),i2=(max2*2);
	if(i1>i2)
	{
		max_string=s.substr(m1,i1);
	}
	else
	{
		max_string=s.substr(m2,i2);
	}
	return max_string;
}
vector<int> circular_shift(vector<int> a, int n)
{
	vector<int> res(a.size());
	for(int i=0;i<a.size();i++)
	{
		res[(i+n)%res.size()]=a[i];
	}
	return res;
}
long long powx(int a, int b)
{
	int res=a;
	for(int i=1;i<b;i++)
	{
		res*=a;
	}
	return res;
}
struct point
{
	double x;
	double y;
};
int S(string ss) {
    int res = 0;
    for(int i=0; i<ss.length(); ++i)
        res+=ss[i]-'0';
    return  res;
}

string int2str(long long x){
    stringstream s;s<<x;
    return s.str();
}
long long a[300001];
int main()
{
	int n;
	long long res=0;
	long long sum=0;
	cin >> n;
	for(int i=1;i<=n;i++)
	{cin >> a[i];sum+=a[i];}
	sort(a+1,a+n+1);
	res=sum;
	for(int i=1;i<=n-1;i++)
	{
		sum-=a[i-1];
		res+=sum;
	}
	cout << res << endl;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<iomanip>
#include<sstream>
#include<string.h>
#include<cstring>
#include<set>
using namespace std;

int SumOfDigits (int n)
 {
	 int sum=0;
	 int a=1;
	 while((n/a)!=0)
	 {
		  a*=10;
		 sum+=(n%a/(a/10));
	 }
	 return sum;
 }//����� ��������� � �����
int fact(int n)
{
	int res=1;
	for(int i=1;i<=n;i++)
		res*=i;
	if(n==0)
		res=0;
	return res;
}//���������
int fibon_number_by_place(int n)
{
	int i=0;
	int a1=0,a2=1,temp;
	while(i<n)
	{
		temp=a2;
		a2=a1+a2;
		a1=temp;
		i++;
	}
	return a1;
}//�������� ����� �������� �� ������
int fibon_place_by_number(int n)
{
	int a1=0,a2=1,temp,i=0;
	while(a1!=n)
	{
		if(n<a1)
		{i=-1;break;}
		temp=a2;
		a2=a1+a2;
		a1=temp;
		i++;
	}
	return i;
}//����� ����� �������� �� �������
int MinDigit(int n){

	int res1=999999999,a=1;
	while((n/a)!=0)
	{
		a*=10;
		if((n%a/(a/10))<res1)
		{res1=(n%a/(a/10));}
	}
	return res1;
}//����������� ������� �����
int MaxDigit(int n){

	int res2=0,a=1;
	while((n/a)!=0)
	{
		 a*=10;
		 if((n%a/(a/10))>res2)
		 {res2=(n%a/(a/10));}
	}
	return res2;
}//������������ ������� �����
int NumberOfZeroes(int n)
{
	int res=0,a=1;
	if(n==0)
		res++;
	while((n/a)!=0)
	 {
		 a*=10;
		 if((n%a/(a/10))==0)
		 {res++;}
	 }
	return res;
}//���-�� ����� � �����
string BinaryNumber(int n)
{
	int a=1;
	string s="",s2="";
	while(n!=0)
	{
		if(n%2==0)
			s+='0';
		else
			s+='1';
		n/=2;
	}
	for(int i=s.length()-1;i>=0;i--)
		s2+=s[i];
	return s2;
}//�������� ����� ����� ��������
int reverseNumber(int n)
{
	int res=0,a=1;
	while((n/a)!=0)
	 {
		 a*=10;
		 cout << (n%a/(a/10));
	}
	return 0;
}//�������� ����� �����
int PalindromeCount(int n)
{
	int b=0,count=0;
	for(int z=1;z<=n;z++)
	{
		int i=1;
		while(z/i/10!=0)
		{i*=10;}
		int a=1;
		while((z/a)!=0)
		{
			a*=10;
			b+=(z%a/(a/10))*i;
			i/=10;
		}
		if(z-b==0)
			count++;
		b=0;
	}

	return count;
}
bool isCompare(string S1, string S2)
{
	bool flag=true;

	if(S1.size()==S2.size())
		for(int i=0;i<S1.size();i++)
		{
			if(S1[i]!=S2[i])
			{flag=false;break;}
		}
	else
		flag=false;
	return flag;
}
bool isPalindrome(string s)
{
	bool flag=true;
	int j=s.size()-1;
	for(int i=0;i<s.size()/2;i++)
	{
		if(s[i]!=s[j])
		{flag=false;break;}
		j--;
	}
	return flag;
}
vector<int> prefix_func(string s)
{
	int n=s.length();
	vector<int> pi(n);
	for(int i=1; i<n; i++)
	{
		int j=pi[i-1];
		while(j>0 && s[i]!=s[j])
			j=pi[j-1];
		if(s[i]==s[j])
			j++;
		pi[i]=j;
	}
	return pi;
}
string IntToStr(int a)
{
	string str1="",str2="";
	int b=1,x=1;
	if(a==0)
		str1+=char(0+48);
	while(a/b!=0)
	{
		b*=10;
		str1+=char(a%b/x+48);
		x*=10;
	}
	for(int i=str1.size()-1;i>=0;i--)
        str2+=str1[i];
    return str2;
}
string max_palindrome(string s)
{
	int n=s.length();
	string max_string;
	vector<int> d1(n);
	int l=0,r=-1;
	for(int i=0;i<n;++i){
		int k=(i>r ? 1 : (min(d1[l+r-i],r-i)));
		while (i+k<n && i-k>=0 && s[i+k]==s[i-k]) ++k;
		d1[i]=k--;
		if (i+k > r)
		l=i-k,r=i+k-1;
	}

	vector<int> d2(n);
	l=0;r=-1;
	for(int i=0;i<n;++i)
	{
		int k=(i>r ? 0 : (min(d2[l+r-i+1],r-i+1)))+1;
		while(i+k-1<n && i-k>=0 && s[i+k-1]==s[i-k])k++;
		d2[i]=--k;
		if(i+k-1>r)
		{l=i-k;r=i+k-1;}
	}
	int max1=-1,max1_index;
	for(int i=0;i<d1.size();i++)
	{
		if(d1[i]>max1)
		{
			max1=d1[i];
			max1_index=i;
		}
	}
	int max2=-1,max2_index;
	for(int i=0;i<d2.size();i++)
	{
		if(d2[i]>max2)
		{
			max2=d2[i];
			max2_index=i;
		}
	}
	int m1=(max1_index-max1+1),m2=(max2_index-max2),i1=(max1*2-1),i2=(max2*2);
	if(i1>i2)
	{
		max_string=s.substr(m1,i1);
	}
	else
	{
		max_string=s.substr(m2,i2);
	}
	return max_string;
}
vector<int> circular_shift(vector<int> a, int n)
{
	vector<int> res(a.size());
	for(int i=0;i<a.size();i++)
	{
		res[(i+n)%res.size()]=a[i];
	}
	return res;
}
long long powx(int a, int b)
{
	int res=a;
	for(int i=1;i<b;i++)
	{
		res*=a;
	}
	return res;
}
struct point
{
	double x;
	double y;
};
int S(string ss) {
    int res = 0;
    for(int i=0; i<ss.length(); ++i)
        res+=ss[i]-'0';
    return  res;
}

string int2str(long long x){
    stringstream s;s<<x;
    return s.str();
}
int a[300000];
int main()
{
	int n;
	long long res=0;
	int sum=0;
	cin >> n;
	for(int i=0;i<n;i++)
	{cin >> a[i];sum+=a[i];}
	sort(a,a+n);
	res=sum;
	for(int i=0;i<n-1;i++)
	{
		sum-=a[i-1];
		res+=sum;
	}
	cout << res << endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
__int64 a[300001];
int main()
{
    __int64 tot,ans;
    int i,n;
    while(~scanf("%I64d",&n))
    {
        ans=tot=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            tot+=a[i];
        }
        sort(a,a+n);
        ans=tot;
        i=0;
        while(tot!=a[n-1])
        {
            ans+=a[i];
            tot-=a[i];
            ans+=tot;
            i++;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[300001];
int main()
{
    int tot,i,n,ans;
    while(~scanf("%d",&n))
    {
        ans=tot=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            tot+=a[i];
        }
        sort(a,a+n);
        ans=tot;
        i=0;
        while(tot!=a[n-1])
        {
            ans+=a[i];
            tot-=a[i];
            ans+=tot;
            i++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


//
//  main.cpp
//  cftrain
//
//  Created by dongxinyu on 15-7-26.
//  Copyright (c) 2015年 dongxinyu. All rights reserved.
//

//
//  main.cpp
//  ACMTrain
//
//  Created by dongxinyu on 14-11-30.
//  Copyright (c) 2014年 dongxinyu. All rights reserved.
//

#include <iostream>
using namespace std;

long n;
long long a[300001];
long long sum;

int comp(const void*a,const void*b)
{
    return -*(int*)a+*(int*)b;
}

int main()
{
    cin>>n;
    sum=0;
    for (int i=0;i<n;i++) {
        cin>>a[i];
        sum+=a[i];
    }

    qsort(a, n, sizeof(long long), comp);

    long long result=a[0]*n;

    for (int i=1; i<n; i++) {
        result+=(a[i])*(n+1-i);
    }

    cout<<result;

}


//
//  main.cpp
//  cftrain
//
//  Created by dongxinyu on 15-7-26.
//  Copyright (c) 2015年 dongxinyu. All rights reserved.
//

//
//  main.cpp
//  ACMTrain
//
//  Created by dongxinyu on 14-11-30.
//  Copyright (c) 2014年 dongxinyu. All rights reserved.
//

#include <iostream>
using namespace std;

int n;
int a[300001];
long long sum;

int comp(const void*a,const void*b)
{
    return -*(int*)a+*(int*)b;
}

int main()
{
    cin>>n;
    sum=0;
    for (int i=0;i<n;i++) {
        cin>>a[i];
        sum+=a[i];
    }

    qsort(a, n, sizeof(int), comp);

    long long result=a[0]*n;

    for (int i=1; i<n; i++) {
        result+=(a[i])*(n+1-i);
    }

    cout<<result;

}


//
//  main.cpp
//  cftrain
//
//  Created by dongxinyu on 15-7-26.
//  Copyright (c) 2015年 dongxinyu. All rights reserved.
//

//
//  main.cpp
//  ACMTrain
//
//  Created by dongxinyu on 14-11-30.
//  Copyright (c) 2014年 dongxinyu. All rights reserved.
//

#include <iostream>
using namespace std;

long n;
long long a[300001];
long long sum;

int comp(const void*a,const void*b)
{
    return -*(int*)a+*(int*)b;
}

int main()
{
    cin>>n;
    sum=0;
    for (int i=0;i<n;i++) {
        cin>>a[i];
        sum+=a[i];
    }

    qsort(a, n, sizeof(int), comp);

    long long result=a[0]*n;

    for (int i=1; i<n; i++) {
        result+=(a[i])*(n+1-i);
    }

    cout<<result;

}


//
//  main.cpp
//  cftrain
//
//  Created by dongxinyu on 15-7-26.
//  Copyright (c) 2015年 dongxinyu. All rights reserved.
//

//
//  main.cpp
//  ACMTrain
//
//  Created by dongxinyu on 14-11-30.
//  Copyright (c) 2014年 dongxinyu. All rights reserved.
//

#include <iostream>
using namespace std;

int n;
int a[300001];
long long sum;

int comp(const void*a,const void*b)
{
    return -*(int*)a+*(int*)b;
}

int main()
{
    cin>>n;
    sum=0;
    for (int i=0;i<n;i++) {
        cin>>a[i];
        sum+=a[i];
    }

    qsort(a, n, sizeof(int), comp);

    int depth=0;
    int node=1;

    while (node<n) {
        node*=2;
        depth++;
    }

    int top=(n-node/2)*2;

    long long result=depth*sum;

    for (int i=0; i<top; i++) {
        result+=a[i];
    }

    cout<<result;

}


#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int a[300000];

int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
                scanf("%d", &a[i]);
        if (n == 2) {
                printf("%d\n", (a[0] + a[1]) * 2);
                return 0;
        }
        int sum = 0;
        for (int i = 0; i < n; ++i)
                sum += a[i];
        sort(a, a + n);
        int ans = sum * 2;
        for (int i = 0; i < n - 2; ++i) {
                sum -= a[i];
                ans += sum * 2;
        }
        ans -= sum;
        printf("%d\n", ans);
        //system("pause");
        return 0;
}


#include <algorithm>
#include <cstdio>
//#include <cstdlib>

using namespace std;

int a[300000];

int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
                scanf("%d", &a[i]);
        //if (n == 1) {
        //        printf("%d\n", a[0]);
        //        return 0;
        //}
        int sum = 0;
        for (int i = 0; i < n; ++i)
                sum += a[i];
        sort(a, a + n);
        int ans = sum * 2;
        for (int i = 0; i < n - 2; ++i) {
                sum -= a[i];
                ans += sum * 2;
        }
        ans -= sum;
        printf("%d\n", ans);
        //system("pause");
        return 0;
}


#include <algorithm>
#include <cstdio>

using namespace std;

int a[300000];

int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
                scanf("%d", &a[i]);
        if (n == 2) {
                printf("%d\n", (a[0] + a[1]) * 2);
                return 0;
        }
        long long sum = 0;
        for (int i = 0; i < n; ++i)
                sum += a[i];
        sort(a, a + n);
        long long ans = sum * 2;
        for (int i = 0; i < n - 2; ++i) {
                sum -= a[i];
                ans += sum * 2;
        }
        ans -= sum;
        printf("%lld\n", ans);
        return 0;
}


#include <algorithm>
#include <cstdio>

using namespace std;

int a[300000];

int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
                scanf("%d", &a[i]);
        long long sum = 0;
        for (int i = 0; i < n; ++i)
                sum += a[i];
        sort(a, a + n);
        long long ans = sum;
        for (int i = 0; i < n - 1; ++i) {
                sum -= a[i];
                ans += sum + a[i];
        }
        printf("%lld\n", ans);
        return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int a[300000];

int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
                scanf("%d", &a[i]);
        if (n == 2) {
                printf("%d\n", (a[0] + a[1]) * 2);
                return 0;
        }
        int sum = 0;
        for (int i = 0; i < n; ++i)
                sum += a[i];
        sort(a, a + n);
        int ans = sum * 2;
        for (int i = 0; i < n - 2; ++i) {
                sum -= a[i];
                ans += sum * 2;
        }
        ans -= sum;
        printf("%d\n", ans);
        system("pause");
        return 0;
}


#include <algorithm>
#include <cstdio>

using namespace std;

int a[300000];

int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
                scanf("%d", &a[i]);
        int sum = 0;
        for (int i = 0; i < n; ++i)
                sum += a[i];
        sort(a, a + n);
        int ans = sum;
        for (int i = 0; i < n - 1; ++i) {
                sum -= a[i];
                ans += sum + a[i];
        }
        printf("%d\n", ans);
        return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <bitset>
#define REP(a,b) for(int i=(a);i<(b);i++)
using namespace std;

typedef long long ll;
const int MAXN = 100005;
const int MOD = 1000000007;
const int INF = 1e9;
typedef double db;
typedef pair<int, int> PP;
typedef pair<ll, pair<int, int> > PL;

int n,k;
vector<int> a;
int main() {
    scanf("%d",&n);
    a.resize(n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a.begin(),a.end());
    ll ans=0;
    if(n==1) {
        cout<<a[0];
        return 0;}
    for(int i=0;i<n;i++){
        ans+=a[i]*(i+2);
    }
    ans-=a[n-1];
    cout<<ans;
}
/*

*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <bitset>
#define REP(a,b) for(int i=(a);i<(b);i++)
using namespace std;

typedef long long ll;
const int MAXN = 100005;
const int MOD = 1000000007;
const int INF = 1e9;
typedef double db;
typedef pair<int, int> PP;
typedef pair<ll, pair<int, int> > PL;

int n,k;
vector<int> a;
int main() {
    scanf("%d",&n);
    a.resize(n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a.begin(),a.end());
    ll ans=0;
    int f=log(n)+2;
    if(n%2!=0)
    for(int i=0;i<n;i++){
        if(i==0) ans+=a[0]*(f-1);
        else
        {
            ans+=a[i]*f;
        }
    }
    else
    {
        for(int i=0;i<n;i++){
        if(i==0 || i==(n/2)-1) ans+=a[i]*(f-1);
        else{
            ans+=a[i]*f;
        }
    }
    }
    cout<<ans;
}
/*

*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <bitset>
#define REP(a,b) for(int i=(a);i<(b);i++)
using namespace std;

typedef long long ll;
const int MAXN = 100005;
const int MOD = 1000000007;
const int INF = 1e9;
typedef double db;
typedef pair<int, int> PP;
typedef pair<ll, pair<int, int> > PL;

int n,k;
vector<int> a;
int main() {
    scanf("%d",&n);
    a.resize(n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a.begin(),a.end());
    ll ans=0;
    if(n==1) {
        cout<<a[0];
        return 0;}
    for(int i=0;i<n;i++){
        ans+=a[i]*1LL*(i+2);
    }
    ans-=a[n-1];
    cout<<ans;
}
/*

*/


#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <ctime>
#include <assert.h>
#include <fstream>
#include <fstream>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define REP(i,a,b) for(int i=int(a) ; i<=int(b); i++)
//#define s(n)   scanf("%d",&n)

int main()
{
    int n;cin>>n;
    long long  *a = new long long [n];
    long long res = 0;
    for(int i=0;i<n;i++)
        cin >>a[i];
    sort(a,&a[n]);

    for(int i=0;i<n;i++)
        if(i<n-2)
            res +=(a[i] *(i+2));
        else res+=(a[i]*n);

    cout <<res<<endl;
}


#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <ctime>
#include <assert.h>
#include <fstream>
#include <fstream>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define REP(i,a,b) for(int i=int(a) ; i<=int(b); i++)
//#define s(n)   scanf("%d",&n)

int main()
{
    int n;cin>>n;
    int *a = new int [n];
    int res = 0;
    for(int i=0;i<n;i++)
        cin >>a[i];
    sort(a,&a[n]);

    for(int i=0;i<n;i++)
        if( i<n-2)
            res +=(a[i] *(i+2));
        else res+=(a[i]*n);

    cout <<res<<endl;
}


#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <ctime>
#include <assert.h>
#include <fstream>
#include <fstream>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define REP(i,a,b) for(int i=int(a) ; i<=int(b); i++)
//#define s(n)   scanf("%d",&n)

int main()
{
    int n;cin>>n;
    int *a = new int [n];
    long long res = 0;
    for(int i=0;i<n;i++)
        cin >>a[i];
    sort(a,&a[n]);

    for(int i=0;i<n;i++)
        if( i<n-2)
            res +=(a[i] *(i+2));
        else res+=(a[i]*n);

    cout <<res<<endl;
}


#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <ctime>
#include <assert.h>
#include <fstream>
#include <fstream>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define REP(i,a,b) for(int i=int(a) ; i<=int(b); i++)
//#define s(n)   scanf("%d",&n)

int main()
{
    int n;cin>>n;
    int *a = new int [n];
    long long res = 0;
    for(int i=0;i<n;i++)
        cin >>a[i];
    sort(a,&a[n]);

    for(int i=0;i<n;i++)
        if(i<n-2)
            res +=(a[i] *(i+2));
        else res+=(a[i]*n);

    cout <<res<<endl;
}


#include <iostream>
#include <algorithm>
using namespace std;

int a[300001];

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    if(n == 1)
    {
        cout << a[0];
        //cin.get();
        //cin.get();
        return 0;
    }
    sort(a + 0, a + n);
    long long s = 0;
    for(int i = 0; i < n; i++)
    {
        if(i == 0)
            s += a[i] * 2;
        else if(i == n - 1)
            s += a[i] * n;
        else s += a[i] * (i + 2);
    }
    cout << s;
    //cin.get();
    //cin.get();
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

long long a[300001];

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    if(n == 1)
    {
        cout << a[0];
        //cin.get();
        //cin.get();
        return 0;
    }
    sort(a + 0, a + n);
    long long s = 0;
    for(int i = 0; i < n; i++)
    {
        if(i == 0)
            s += a[i] * 2;
        else if(i == n - 1)
            s += a[i] * n;
        else s += a[i] * (i + 2);
    }
    cout << s;
    //cin.get();
    //cin.get();
    return 0;
}


#define VERBOSE
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <fstream>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#ifndef _DEBUG
# undef VERBOSE
#endif
#ifdef NDEBUG
#define VERBOSE
#endif

#define eps 1e-8
#define inf (1000 * 1000 * 1000)
#define linf (4LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000)
#define sqr(x) ((x) * (x))
#define eq(x, y) (((x) > (y) ? (x) - (y) : (y) - (x)) <= eps)
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mp make_pair
#define FOR(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define FORN(i, n) for(int (i) = 1; (i) <= (n); ++(i))
#define REP(i,from,to) for(int (i) = (from); (i) <= (to); ++(i))
#define RREP(i,from,to) for(int (i) = (from); (i) >= (to); --(i))
#define FOREACH(iter, cont) for(__typeof(cont)::iterator it = (cont).begin(); (it) != (cont).end(); ++(it))

using namespace std;

typedef unsigned uint;
typedef long long llong;
typedef unsigned long long ullong;
typedef long double ldouble;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
struct debug_t {
  template <typename T>
  debug_t& operator<<(const T& value) {
# ifdef VERBOSE
    cout << value;
# endif
    return *this;
  }
} debug;

int main() {

  ofstream out("input.txt");
  FOR(i,3*100*1000)
    out << 1000*1000 << " ";
  out.close();

  //freopen(TASK ".in", "r", stdin);
  //freopen(TASK ".out", "w", stdout);
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
#endif
  //freopen("info.txt", "r", stdin);
  //freopen("info.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  while( cin >> n ) {
    vector<llong> a(n+1), pref(n+1); FORN(i,n) cin >> a[i];
    a[0] = pref[0] = 0;
    sort(all(a));
    FORN(i,n) pref[i] = pref[i-1] + a[i];
    //cout << pref[n] << "\n";
    llong s = 0;
    FORN(i,n)
      s += pref[n] - pref[i-1] + a[i-1];
    cout << s << "\n";
  }

  return 0;
}


#define VERBOSE
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <fstream>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#ifndef _DEBUG
# undef VERBOSE
#endif
#ifdef NDEBUG
#define VERBOSE
#endif

#define eps 1e-8
#define inf (1000 * 1000 * 1000)
#define linf (4LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000)
#define sqr(x) ((x) * (x))
#define eq(x, y) (((x) > (y) ? (x) - (y) : (y) - (x)) <= eps)
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mp make_pair
#define FOR(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define FORN(i, n) for(int (i) = 1; (i) <= (n); ++(i))
#define REP(i,from,to) for(int (i) = (from); (i) <= (to); ++(i))
#define RREP(i,from,to) for(int (i) = (from); (i) >= (to); --(i))
#define FOREACH(iter, cont) for(__typeof(cont)::iterator it = (cont).begin(); (it) != (cont).end(); ++(it))

using namespace std;

typedef unsigned uint;
typedef long long llong;
typedef unsigned long long ullong;
typedef long double ldouble;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
struct debug_t {
  template <typename T>
  debug_t& operator<<(const T& value) {
# ifdef VERBOSE
    cout << value;
# endif
    return *this;
  }
} debug;

int main() {
  //freopen(TASK ".in", "r", stdin);
  //freopen(TASK ".out", "w", stdout);
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
#endif
  //freopen("info.txt", "r", stdin);
  //freopen("info.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  while( cin >> n ) {
    vi a(n+1), pref(n+1); pref[0] = a[0] = 0;
    FORN(i,n) cin >> a[i];
    sort(all(a));
    FORN(i,n) pref[i] = pref[i-1] + a[i];

    llong result = 0;
    FORN(i,n)
      result += a[i-1] + pref[n] - pref[i-1];
    cout << result << "\n";
  }

  /*int n,k;
  while( cin >> n >> k ) {
    vi cnt('Z'-'A'+1, 0);

    FOR(i,n) {
      char c; cin >> c; --cnt[c-'A'];
    }
    sort(all(cnt));
    FOR(i,sz(cnt))
      cnt[i] = -cnt[i];

    FOR(i,sz(cnt)) {
      if( k > 0 )
        --cnt[i], --k;
      else
        cnt[i] = 0;
    }
    if( k > 0 )
      RREP(i,sz(cnt)-1,0)
        if( k > 0 ) {
          int t = cnt[i];
          cnt[i] = max(0, t - k),
          k = max(0, k - t);
        }

    int result = 0;
    FOR(i,sz(cnt))
      result += cnt[i];
    cout << result << "\n";
  }*/

  return 0;
}


/***Good Luck***/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <functional>
#include <cmath>

#define Zero(a) memset(a, 0, sizeof(a))
#define Neg(a)  memset(a, -1, sizeof(a))
#define All(a) a.begin(), a.end()
#define PB push_back
#define inf 0x3f3f3f3f
#define inf2 0x7fffffffffffffff
#define ll long long
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
void get_val(int &a) {
    int value = 0, s = 1;
    char c;
    while ((c = getchar()) == ' ' || c == '\n');
    if (c == '-') s = -s; else value = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        value = value * 10 + c - 48;
    a = s * value;
}
const int maxn = 300005;
ll arr[maxn];
int main() {
    //freopen("data.out", "w", stdout);
    //freopen("data.in", "r", stdin);
    cin.sync_with_stdio(false);
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        cin >> arr[i];
    sort(arr, arr + n);
    ll ans = 0;
    for (int i = 0; i < n - 1; ++i) {
        ans += arr[i] * (i + 2);
    }
    ans += arr[n - 1] * n;
    cout << ans << endl;
    return 0;
}


/***Good Luck***/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <functional>
#include <cmath>

#define Zero(a) memset(a, 0, sizeof(a))
#define Neg(a)  memset(a, -1, sizeof(a))
#define All(a) a.begin(), a.end()
#define PB push_back
#define inf 0x3f3f3f3f
#define inf2 0x7fffffffffffffff
#define ll long long
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
void get_val(int &a) {
    int value = 0, s = 1;
    char c;
    while ((c = getchar()) == ' ' || c == '\n');
    if (c == '-') s = -s; else value = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        value = value * 10 + c - 48;
    a = s * value;
}
const int maxn = 300005;
ll arr[maxn];
int main() {
    //freopen("data.out", "w", stdout);
    //freopen("data.in", "r", stdin);
    cin.sync_with_stdio(false);
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        cin >> arr[i];
    sort(arr, arr + n);
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += arr[i] * (i + 1);
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    long long n, k;
    long long a[26];
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    long long result = 0;
    sort(a, a + n);
    for (long long i = 0; i < n; i++)
        result += min((i + 2), n) * a[i];
    cout << result;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    long long n, k;
    long long a[300000];
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    long long result = 0;
    sort(a, a + n);
    for (long long i = 0; i < n; i++)
        result += min((i + 2), n) * a[i];
    cout << result;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    long long n, k;
    long long a[300000];
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    long long result = 0;
    sort(a, a + n);
    for (int i = 0; i < n; i++)
        result += (i + 1) * a[i];
    cout << result;
    return 0;
}


// IN THE NAME OF GOD

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <list>
#include <stack>
#include <cstdio>
#include <cmath>
#include <stdio.h>

typedef long long ll;
typedef long double ld;

using namespace std;

const int MAXN=1e6+22;
const int INF=1e9+2;

ll  n,a[MAXN],ans;

int main(){
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(int i=0;i<n;i++)
        ans+=(i+2)*a[i];
    cout<<ans-a[n-1];
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdio>
#include <map>
#include <queue>
using namespace std;
#define mp make_pair
#define all(v) v.begin(),v.end()

int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i=0; i<n; ++i)
		cin >> a[i];
	long long sum=0, totsum=0;
	sort(all(a));
	int k=2;
	for (int i=0; i<n; ++i, ++k)
		sum+=(k*a[i]);

	cout << sum-a[a.size()-1];
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdio>
#include <map>
#include <queue>
using namespace std;
#define mp make_pair
#define all(v) v.begin(),v.end()

int main(){
	int n;
	cin >> n;
	vector<long long> a(n);
	for (int i=0; i<n; ++i)
		cin >> a[i];
	long long sum=0;
	sort(all(a));
	int k=2;
	for (int i=0; i<n; ++i, ++k)
		sum+=(k*a[i]);

	cout << sum-a[a.size()-1];
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdio>
#include <map>
#include <queue>
using namespace std;
#define mp make_pair
#define all(v) v.begin(),v.end()

int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i=0; i<n; ++i)
		cin >> a[i];
	long long sum=0, totsum=0;
	sort(a.rbegin(), a.rend());
	for (int i=0; i<n; ++i)
		totsum+=a[i];
	while (a.size()>1){
		sum=0;
		for (int j=0; j<a.size(); ++j)
			sum+=a[j];
		totsum+=sum;
		a.pop_back();
	}

	cout << totsum;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define SS stringstream
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 3e5+5;
const int MOD = 1000000007;

inline int POW(int b, int p) {if(!p) return 1; int sq = POW(b, p >> 1); sq *= sq; if(p&1) sq *= b; return sq;}

//int arr[MAX];

int main(){
//  freopen("in.txt", "r", stdin);
    int n;
    while(cin >> n){
        ll ans = 0, sum = 0;
        pq <int> q;
        FOR(i, n){
            int x; cin >> x;
            sum += x;
            q.push(x);
        }
        if(n > 2){
            ans += sum * 2; sum = 0;
            int cnt = 0;
            while(q.size()){
                int e = q.top(); q.pop();
                sum += e;
                cnt++;
                if(cnt > 1 && cnt < n)
                    ans += sum;
                if(cnt == n)
                    break;
            }
        }
        else if(n == 2)
            ans += sum * 2;
        else
            ans += sum;
        cout << ans << endl;
    }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define SS stringstream
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 3e5+5;
const int MOD = 1000000007;

inline int POW(int b, int p) {if(!p) return 1; int sq = POW(b, p >> 1); sq *= sq; if(p&1) sq *= b; return sq;}

//int arr[MAX];

int main(){
//  freopen("in.txt", "r", stdin);
    int n;
    while(cin >> n){
        ll ans = 0, sum = 0;
        pq <int> q;
        FOR(i, n){
            int x; cin >> x;
            sum += x;
            q.push(x);
        }
        if(n > 2){
            ans += sum * 2;
            int sz = n-1;
            vi v;
            while(sz-- && sz){
                //b = 0;
                FOR(i, sz+1){
                    int e = q.top(); q.pop();
                    v.pb(e);
                    ans += e;
                }
                FOR(i, v.size())
                    q.push(v[i]);
                break;
            }
            while(sz-- && sz){
                int l = sz+1;
                FOR(i, l)
                    ans += v[i];
            }
        }
        else if(n == 2)
            ans += sum * 2;
        else
            ans += sum;
        cout << ans << endl;
    }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define SS stringstream
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 3e5+5;
const int MOD = 1000000007;

inline int POW(int b, int p) {if(!p) return 1; int sq = POW(b, p >> 1); sq *= sq; if(p&1) sq *= b; return sq;}

//int arr[MAX];

int main(){
//  freopen("in.txt", "r", stdin);
    int n;
    while(cin >> n){
        ll ans = 0, sum = 0;
        pq <int> q;
        FOR(i, n){
            int x; cin >> x;
            sum += x;
            q.push(x);
        }
        if(n > 2){
            ans += sum * 2;
            int sz = n-1;
            while(sz-- && sz){
                vi v;
                FOR(i, sz+1){
                    int e = q.top(); q.pop();
                    v.pb(e);
                    ans += e;
                }
                FOR(i, v.size())
                    q.push(v[i]);
            }
        }
        else if(n == 2)
            ans += sum * 2;
        else
            ans += sum;
        cout << ans << endl;
    }
    return 0;
}


# include <iostream>
# include <vector>
# include <algorithm>
using namespace std ;

int main ()
{
	int N ;
	cin >> N ;
	vector <long long> v ( N ) ;
	for ( int i = 0 ; i< N ; ++ i )
		cin >> v[i] ;
	sort ( v.begin() ,v.end() ) ;

	long long sum = 0 ;

	for ( int i = 0 ; i < N ; ++ i )
		sum += v[i] * ( i + 2 )  ;

	sum -= v[N-1 ] ;
	cout << sum ;

	//system ("pause>null" ) ;
	return false;
}


# include <iostream>
# include <vector>
# include <algorithm>
using namespace std ;

int main ()
{
	int N ;
	cin >> N ;
	vector <int> v ( N ) ;
	for ( int i = 0 ; i< N ; ++ i )
		cin >> v[i] ;
	sort ( v.begin() ,v.end() ) ;

	long long sum = 0 ;
	for ( int i = 0 ; i < N ; ++ i )
		sum += v[i] * ( i + 1 ) + v[i] ;

	sum -= v[N-1 ] ;
	cout << sum ;

	//system ("pause>null" ) ;
	return false;
}


#include <stdio.h>
#include <algorithm>
using namespace std;

int angka[333333];
long long int b;

int main(){
	int jumAngka;
	scanf("%d", &jumAngka);
	for(int i=0; i<jumAngka; i++){
		scanf("%d", angka+i);
	}

	sort(angka, angka+jumAngka);
	b=0;
	for(int i=0; i<jumAngka; i++){
		if(i<jumAngka-1){
			b+=(long long int)angka[i]*(long long int)(i+1+1);
		}else{
			b+= (long long int)angka[i] * (long long int)(i+1);
		}
	}

	printf("%lld\n", b);
	return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;

int angka[111111];
long long int b;

int main(){
	int jumAngka;
	scanf("%d", &jumAngka);
	for(int i=0; i<jumAngka; i++){
		scanf("%d", angka+i);
	}

	sort(angka, angka+jumAngka);
	b=0;
	for(int i=0; i<jumAngka; i++){
		if(i<jumAngka-1){
			b+=(long long int)angka[i]*(long long int)(i+1+1);
		}else{
			b+= (long long int)angka[i] * (long long int)(i+1);
		}
	}

	printf("%lld\n", b);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<array>
#include<cstdlib>
#include<set>
#include<map>
#include<utility>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<string>
#include<unordered_map>
#include<climits>
#include<unordered_set>

using namespace std;

template <typename T>
inline T in(){
    char c, m = 0; T t = 0;
    c = getchar();
    while ((c<'0' || c>'9') && c != '-')
        c = getchar();
    if (c == '-')
        m = 1,
        c = getchar();

    while (c >= '0' && c <= '9'){
        t = (t << 3) + (t << 1) + c - '0';
        c = getchar();
    }
    if (m)
        t = -t;
    return t;

}

const int maxm = 300005;
array<long long, maxm> arr;

int main(){
    int n;
    n = in<int>();
    for (size_t i = 0; i < n; i++)
    {
        arr[i] = in<int>();
    }
    sort(arr.begin(), arr.begin() + n);
    long long sum = 0;
    if (n<2)
    {
        for (size_t i = 0; i < n; i++)
        {
            sum += arr[i];
        }
        cout << sum << endl;
        return 0;
    }
    sum += arr[n - 1] * (n);
    sum += arr[n - 2] * (n);
    for (int i = n - 3; i >= 0; i--)
    {
        sum += arr[i] * (i + 2);
    }
    cout << sum << endl;

    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<array>
#include<cstdlib>
#include<set>
#include<map>
#include<utility>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<string>
#include<unordered_map>
#include<climits>
#include<unordered_set>

using namespace std;

template <typename T>
inline T in(){
    char c, m = 0; T t = 0;
    c = getchar();
    while ((c<'0' || c>'9') && c != '-')
        c = getchar();
    if (c == '-')
        m = 1,
        c = getchar();

    while (c >= '0' && c <= '9'){
        t = (t << 3) + (t << 1) + c - '0';
        c = getchar();
    }
    if (m)
        t = -t;
    return t;

}

const int maxm = 300005;
array<int, maxm> arr;

int main(){
    int n;
    n = in<int>();
    for (size_t i = 0; i < n; i++)
    {
        arr[i] = in<int>();
    }
    sort(arr.begin(), arr.begin()+n);
    long long sum = 0;
    if (n<2)
    {
        for (size_t i = 0; i < n; i++)
        {
            sum += arr[i];
        }
        cout << sum << endl;
        return 0;
    }
    sum += arr[n-1] * (n);
    sum += arr[n-2] * (n);
    for (int i=n-3; i >= 0; i--)
    {
        sum += arr[i] * (i+2);
    }
    cout << sum << endl;

    return 0;
}


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

void solve()
{
    int n; cin >> n;
    vector<LL> a(n); cin >> a;
    sort(rall(a));

    LL score = 0;
    while (true)
    {
        if (a.size() == 1)
        {
            score += a.front();
            break;
        }

        vector<LL> b; b.reserve(a.size() / 2 + 5);
        for (int i = 1; i < a.size(); i += 2)
        {
            b.push_back(a[i - 1] + a[i]);
            score += a[i - 1];
            score += a[i];
        }

        if (a.size() & 1)
            b.push_back(a.back());

        a = b;

    }
    cout << score << endl;
}

int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    solve();

    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

void solve()
{
    int n; cin >> n;
    vector<LL> a(n); cin >> a;
    priority_queue<LL> q(all(a));
    LL res = 0;
    while (q.size() > 1)
    {
        LL u = q.top(); q.pop();
        LL v = q.top(); q.pop();
        q.push(u + v);
        res += u + v;
    }
    res += q.top();
    cout << res << endl;
}

int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    solve();

    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long ul;
typedef unsigned long long ull;

int main()
{
    //freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    vector<ull> a(n, 0), sr(n, 0);
    for (int i = 0; i < n; i++)
        cin >> a[i];

    sort(a.begin(), a.end());

    ull sum = 0;
    for (int i = n - 1; i > -1; i--)
        sr[i] = sum += a[i];

    sum = 0;
    for (int i = 0; i < n; i++)
        sum += sr[i] + a[i];

    sum -= a.back();

    cout << sum;
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long ul;
typedef unsigned long long ull;

int main()
{
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    vector<ull> a(n, 0);
    for (int i = 0; i < n; i++)
        cin >> a[i];

    sort(a.begin(), a.end());
    ull sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
            sum += a[j];
        sum += a[i];
    }

    sum -= a.back();

    cout << sum;
    return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
int main ()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n;
	cin>>n;
	long long *a = new long long[n];
	long long *s = new long long [n];
	for (int i = 0;i<n;i++) {cin>>a[i];}
	sort(a,a + n);
	for (int i = 0;i<n;i++)
	{
		s[i] = ((i > 0)?s[i - 1]:0) + a[i];
	}
	long long ans = 0;
	ans+=s[n - 1];
	for (int i = 0;i<n -1;i++)
	{
		ans += a[i] + s[n - 1] - s[i];
	}
	cout<<ans;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
int main ()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n;
	cin>>n;
	int *a = new int[n];
	int *s = new int [n];
	for (int i = 0;i<n;i++) {cin>>a[i];}
	sort(a,a + n);
	for (int i = 0;i<n;i++)
	{
		s[i] = ((i > 0)?s[i - 1]:0) + a[i];
	}
	long long ans = 0;
	ans+=s[n - 1];
	for (int i = 0;i<n -1;i++)
	{
		ans += a[i] + s[n - 1] - s[i];
	}
	cout<<ans;
	//system("pause");
	return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int n;
int data[300010];
long long int sum[300010];
long long int solve(int i,int j){
    int length = (j - i + 1);
    if (length < 0) return 0;
    if (length == 1) return data[i];
    int mid = length / 2;
    return sum[j+1]-sum[i]+solve(i, mid + i - 1) + solve(mid+i, j);
}
int main(){
    int n;
    cin >> n;
    sum[0] = 0;
    r(n){
        cin >> data[i];

    }
    sort(data, data + n);
    r(n){
        sum[i + 1] = data[i] + sum[i];
    }
    cout << solve(0, n - 1) << endl;
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int n;
int data[300010];
long long int sum[300010];
long long int solve(int i,int j){
    int length = (j - i + 1);
    if (length < 0) return 0;
    if (length == 1) return data[i];
    return sum[j+1]-sum[i]+solve(i, i) + solve(i+1, j);
}
int main(){
   ///////////////// R_W;
    int n;
    cin >> n;
    sum[0] = 0;
    r(n){
        cin >> data[i];

    }
    sort(data, data + n);
    r(n){
        sum[i + 1] = data[i] + sum[i];
    }
    cout << solve(0, n - 1) << endl;
    return 0;
}


#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <ctime>
#include <queue>
#include <iostream>
#include <stack>
//#include <iterator> map<string,int>::iterator i;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vk;
int F(const void* l,const void* r){
    return (*(int*)l - *(int*)r);
}
int n;
ll ch = 0;
int a[300001];
int b[1000001];
int h = 0;
bool y = false;
void f(int x){
	if(x / 2 > 0){
		h++;
		if(x % 2 == 1) y = true;
		f(x/2);
	}
}
int t = 0;
void rec(int x){
	if(x == 3) t++;
	else if(x == 2){

	}
	else{
		if(x % 2 == 0){
			rec(x/2);
			rec(x/2);
		}
		else{
			rec(x/2);
			rec(x/2+1);
		}
	}
}
int main(){
  // ifstream cin("input.txt");
   // ofstream cout("output.txt");
	cin>>n;
	for(int i = 0; i < n; i++){
		cin>>a[i];
		ch += a[i];
		b[a[i]]++;
	}
	if(n == 1){
		cout<<a[0];
		return 0;
	}
	sort(a,a + n);
	f(n);
	if(y == false){
		ch += ch * h;
		cout<<ch;
		return 0;
	}
	else{
		h++;
	}
	rec(n);
	ch += ch * h;
	for(int i = 0; i < t; i++) ch -= a[i];
	cout<<ch;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
long long n, i, a[303030], ans, s;

int main()
{
cin>>n;
s = 0;
for (i= 1; i <= n ; i++)
{
cin>>a[i];
s += a[i];
}
a[0] = 0;
ans = s;
sort(a + 1, a + (n + 1));
for (i = 1; i < n; i++)
{
ans += s;
s = s - a[i];
}
cout<<ans;
return 0;
}


#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <ctime>
#include <queue>
#include <iostream>
#include <stack>
//#include <iterator> map<string,int>::iterator i;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vk;
int F(const void* l,const void* r){
    return (*(int*)l - *(int*)r);
}
int n;
ll ch = 0;
int a[300001];
int b[1000001];
int h = 0;
bool y = false;
void f(int x){
	if(x / 2 > 0){
		h++;
		if(x % 2 == 1) y = true;
		f(x/2);
	}
}
int t = 0;
void rec(int x){
	if(x == 3) t++;
	else if(x == 2){

	}
	else{
		if(x % 2 == 0){
			rec(x/2);
			rec(x/2);
		}
		else{
			rec(x/2);
			rec(x/2+1);
		}
	}
}
int main(){
  // ifstream cin("input.txt");
    //ofstream cout("output.txt");
	cin>>n;
	for(int i = 0; i < n; i++){
		cin>>a[i];
		ch += a[i];
		b[a[i]]++;
	}
	if(n == 1){
		cout<<a[0];
		return 0;
	}
	sort(a,a + n);
	f(n);
	if(y == false){
		ch += ch * h;
		cout<<ch;
	}
	else{
		h++;
	}
	rec(n);
	ch += ch * h;
	for(int i = 0; i < t; i++) ch -= a[i];
	cout<<ch;
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

__int64 num[300006];

int main()
{
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++)
    scanf("%d",&num[i]);
    sort(num,num+n);
    __int64 ans=num[n-1]*n;
    for(int i=n-2;i>=0;i--)
    ans+=num[i]*(i+2);
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
using namespace std;

__int64 num[300006];

int main()
{
	int n;scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d",&num[i]);
	sort(num,num+n);
	__int64 ans=num[n-1]*n;
	for(int i=n-2;i>=0;i--)
	ans+=num[i]*(i+2);
	cout<<ans<<endl;
	return 0;
}


import sys, heapq
input = sys.stdin.readline

def main():
    N = int(input())
    q = []
    for a in list(map(int, input().split())):
        heapq.heappush(q, a)

    sum_ = sum(q)
    tmp = 0
    ans = sum_
    while len(q) > 1:
        tmp = heapq.heappop(q)
        ans += sum_
        sum_ -= tmp
    print(ans)

if __name__ == "__main__":
    main()


n = int(input())
num = list(map(int, input().split()))
num.sort(reverse=True)
psum = [0]*n
psum[0] = num[0]
for i in range(1, n):
	psum[i] = psum[i-1]+num[i]
print(sum(psum[1:])+psum[n-1])


import sys
sys.setrecursionlimit(1000000)

def solve(a):
    if len(a) == 1:
        return a[0]
    b = a.pop()
    c = a.pop()
    a.append(b + c)
    ans = solve(a) + b + c
    return ans

n = int(input())
a = sorted([int(x) for x in input().split()])
score = solve(a)
print(score)


def solve(a):
    if len(a) == 1:
        return a[0]
    b = a.pop()
    c = a.pop()
    a.append(b + c)
    ans = solve(a) + b + c
    return ans

n = int(input())
a = sorted([int(x) for x in input().split()])
score = solve(a)
print(score)


import sys
sys.setrecursionlimit(1000000)

rec = 0
def solve(a):
    global rec
    rec += 1
    if len(a) == 1:
        return a[0]
    b = a.pop()
    c = a.pop()
    a.append(b + c)
    ans = solve(a) + b + c
    return ans

n = int(input())
a = sorted([int(x) for x in input().split()])
score = solve(a)
print(score)


n = int(input())
a = list(map(int, input().split()))
a.sort()
score = a[n - 1] * n
for i in range(n - 1):
    score += a[i] * (i + 2)
print(score)


def getsplit(depth, n, split_list):
    if n == 0:
        return
    elif n == 1:
        split_list.append(depth)
    else:
        n1 = n // 2
        n2 = n - n1
        getsplit(depth+1, n1, split_list)
        getsplit(depth+1, n2, split_list)

n = int(input())
a = list(map(int, input().split()))
s = []
getsplit(1, n, s)

a.sort()
s.sort()

sol = 0
for i in range(n):
    sol += a[i]*s[i]

print(sol)


n = int(input())
a = list(map(int, input().split()))
s = [i for i in range(2, n+2)]
s[-1] -= 1

a.sort()

sol = 0
for i in range(n):
    sol += a[i]*s[i]

print(sol)


n = int(input())
a = list(map(int, input().split()))
a.sort()
score = sum(a) + a[n - 1] * (n - 1)
for i in range(1, n):
    score += a[i - 1] * i
print(score)


n = int(input())

l = list(map(int, input().split()))
l.sort()
ans = sum(l)

for i in range(n-1):
    t = l.pop(0)
    ans += t
    ans += sum(l)

print(ans)


import heapq
n=int(input())
a=list(map(lambda x:-int(x),input().split()))
heapq.heapify(a)

ans=0

for i in range(n-1):
    x=heapq.heappop(a)
    y=heapq.heappop(a)
    ans+=x+y
    heapq.heappush(a,x+y)

print(-ans-sum(a))


n=int(input())
a=sorted(list(map(int,input().split())))
print(sum([j*(i+2) for i,j in enumerate(a)])-a[-1])


#!/usr/bin/env python3

n = int(input())
a = [int(x) for x in input().split()]
a.sort()
ans = 0
for i in range(n):
    ans += (i+2) * a[i]
ans -= a[-1]
print(ans)


#!/usr/bin/env python3

n = int(input())
a = sorted(int(x) for x in input().split())
count = 0
for i in range(n):
    count += (i+2) * a[i]
print(count - a[-1])


n = int(input())
a = sorted(map(int, input().split()), reverse=True)
for i in range(1, n):
  a[i] += a[i - 1]
s = a[-1]
for i in range(1, n - 1):
  s += a[n - i - 1] + a[n - i]
print(s)


n = int(input())
a = sorted(map(int, input().split()), reverse=True)
for i in range(1, n):
  a[i] += a[i - 1]
s = a[-1]
for i in range(1, n):
  s += a[n - i]
print(s)


n = int(input())
seq = list(map(int,input().split()))

#print(score)
start  = 0
end = n-1
score = 0
seq.sort(reverse = True)
if n == 1:
    print(seq[0])

else:
    score = sum(seq[start:end+1])
    while(start != end):

        #print(score)
        mid = end -1
        score += sum(seq[start:mid+1])
        #print(score)
        score += seq[mid+1]
        #print(score)
        end = mid
        #print(start, end)
    print(score)


n = int(input())
seq = list(map(int,input().split()))

#print(score)
start  = 0
end = n-1
score = 0
seq.sort(reverse = True)
if n == 1:
    print(seq[0])

else:
    tot = [seq[0]]
    for i in range(1,n):
        tot.append(seq[i] + tot[i-1])

    score = tot[-1]
    #print(score)
    while(start != end):

        #print(score)
        mid = end -1
        score += tot[mid]
        #print(score)
        score += seq[mid+1]
        #print(score)
        end = mid
        #print(start, end)
    print(score)


import heapq

N = int(input())
L = list(map(lambda x: int(x) * -1, input().split()))
ans = 0
heapq.heapify(list(L))

while N > 1:
    t = heapq.heappop(L) + heapq.heappop(L)
    ans -= t

    heapq.heappush(L, t)
    N -= 1

print(ans - L[0])


import heapq

N = int(input())
L = list(map(lambda x: int(x) * -1, input().split()))
ans = 0
heapq.heapify(L)

while N > 1:
    t = heapq.heappop(L) + heapq.heappop(L)
    ans -= t

    heapq.heappush(L, t)
    N -= 1

print(ans - L[0])


def main():
    n = int(input())
    seq = [int(c) for c in input().split()]

    seq.sort()

    s = sum(seq)
    ans = 2 * s
    for i in range(n-2):
        s -= seq[i]
        ans += s

    print(ans)

if __name__ == "__main__":
    main()


def main():
    n = int(input())
    seq = [int(c) for c in input().split()]

    if n == 1:
        print(seq[0])
        return

    seq.sort()

    s = sum(seq)
    ans = 2 * s
    for i in range(n-2):
        s -= seq[i]
        ans += s

    print(ans)

if __name__ == "__main__":
    main()


from math import gcd

def main():
    a, b = map(int, input().split())

    if a == b:
        print(1)
        return

    ans = a // b
    a -= b * ans
    g = gcd(a, b)
    a //= g
    b //= g

    print(ans + b if a > 0 else ans)

if __name__ == "__main__":
    main()


from itertools import combinations
from functools import lru_cache

class Solver():
    def __init__(self, N, numbers):
        self.N = N
        self.numbers = numbers

    @lru_cache(maxsize=None)
    def dfs(self, numbers):
        # Appleman
        score = sum(numbers)
        if len(numbers) == 1:
            return score

        results = []

        # for each split pattern
        index = list(range(len(numbers)))
        for k in range(len(numbers) // 2):
            for c in combinations(index, k + 1):
                g1 = [numbers[i] for i in index if i in c]
                g2 = [numbers[i] for i in index if i not in c]
                #print('g1 ->', g1)
                #print('g2 ->', g2)
                results.append(self.dfs(tuple(g1)) + self.dfs(tuple(g2)))

        #print(numbers, results)
        #print(numbers, '->' ,results, max(results) + score)

        return max(results) + score

    def solve(self):
        return self.dfs(tuple(self.numbers))

if __name__ == "__main__":
    N = int(input())
    numbers = list(map(int, input().split()))
    s = Solver(N, numbers)
    print(s.solve())


from itertools import combinations
from functools import lru_cache

class Solver():
    def __init__(self, N, numbers):
        self.N = N
        self.numbers = numbers

    @lru_cache(maxsize=20000)
    def dfs(self, numbers):

        score = sum(numbers)
        if len(numbers) == 1:
            return score

        if len(numbers) == 2:
            return score * 2

        results = []
        index = list(range(len(numbers)))
        # for each split pattern
        for k in range(len(numbers) // 2):
            for c in combinations(index, k + 1):
                g1 = [numbers[i] for i in index if i in c]
                g2 = [numbers[i] for i in index if i not in c]

                if len(g1) == 1:
                    score1 = g1[0]
                elif len(g1) == 2:
                    score1 = sum(g1)*2
                else:
                    score1 = self.dfs(tuple(g1))

                if len(g2) == 1:
                    score2 = g2[0]
                elif len(g2) == 2:
                    score2 = sum(g2)*2
                else:
                    score2 = self.dfs(tuple(g2))

                #print('g1 ->', g1)
                #print('g2 ->', g2)
                results.append(score1 + score2)

        #print(numbers, results)
        #print(numbers, '->' ,results, max(results) + score)

        return max(results) + score

    def solve(self):
        return self.dfs(tuple(self.numbers))

if __name__ == "__main__":
    N = int(input())
    numbers = list(map(int, input().split()))
    s = Solver(N, numbers)
    print(s.solve())


from itertools import combinations
from functools import lru_cache

@lru_cache(maxsize=10000)
def dfs(numbers):

    score = sum(numbers)
    if len(numbers) == 1:
        return score

    if len(numbers) == 2:
        return score * 2

    results = []
    index = list(range(len(numbers)))
    # for each split pattern
    for k in range(len(numbers) // 2):
        for c in combinations(index, k + 1):
            g1 = [numbers[i] for i in index if i in c]
            g2 = [numbers[i] for i in index if i not in c]

            if len(g1) == 1:
                score1 = g1[0]
            elif len(g1) == 2:
                score1 = sum(g1)*2
            else:
                score1 = dfs(tuple(sorted(g1)))

            if len(g2) == 1:
                score2 = g2[0]
            elif len(g2) == 2:
                score2 = sum(g2)*2
            else:
                score2 = dfs(tuple(sorted(g2)))

            #print('g1 ->', g1)
            #print('g2 ->', g2)
            results.append(score1 + score2)

    #print(numbers, results)
    #print(numbers, '->' ,results, max(results) + score)

    return max(results) + score

N = int(input())
numbers = tuple(map(int, input().split()))
print(dfs(numbers))