#include<bits/stdc++.h>
using namespace std;

int x[100000+50],y[100000+50];

vector <int> rtol[300000+50];

vector <int> ltor[300000+50],nltor[300000+50];

vector <int> row[100000+50],col[100000+50];

int ans[10];

int main(){
    ios::sync_with_stdio(false);

    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x[i]>>y[i];
        row[x[i]].push_back(y[i]);
        col[y[i]].push_back(x[i]);
        rtol[x[i]+y[i]].push_back(y[i]);
        if(x[i]-y[i]>=0){
            ltor[x[i]-y[i]].push_back(y[i]);
        }
        else{
            nltor[y[i]-x[i]].push_back(y[i]);
        }
    }

    for(int i=0;i<m;i++){
        int bc=0;

        int rowsize=row[x[i]].size();
        if(rowsize==1){

        }
        else{
            int index=lower_bound(row[x[i]].begin(),row[x[i]].end(),y[i])-row[x[i]].begin();
            if(index==0){
                bc++;
            }
            else if(index==rowsize-1){
                bc++;
            }
            else{
                bc+=2;
            }
        }

        int colsize=col[y[i]].size();
        if(colsize==1){

        }
        else{
            int index=lower_bound(col[y[i]].begin(),col[y[i]].end(),x[i])-col[y[i]].begin();
            if(index==0||index==colsize-1){
                bc++;
            }
            else{
                bc+=2;
            }
        }

        int diagsize=rtol[x[i]+y[i]].size();

        if(diagsize==1){

        }
        else{
            int index=lower_bound(rtol[x[i]+y[i]].begin(),rtol[x[i]+y[i]].end(),y[i])-rtol[x[i]+y[i]].begin();
            if(index==0||index==diagsize-1){
                bc++;
            }
            else{
                bc+=2;
            }
        }

        if(x[i]-y[i]>=0){
            diagsize=ltor[x[i]-y[i]].size();
            if(diagsize==1){

            }
            else{
                int index=lower_bound(ltor[x[i]-y[i]].begin(),ltor[x[i]-y[i]].end(),y[i])-ltor[x[i]-y[i]].begin();
                if(index==0||index==diagsize-1){
                    bc++;
                }
                else{
                    bc+=2;
                }
            }
        }
        else{
            diagsize=nltor[-x[i]+y[i]].size();
            if(diagsize==1){

            }
            else{
                int index=lower_bound(nltor[-x[i]+y[i]].begin(),nltor[-x[i]+y[i]].end(),y[i])-nltor[-x[i]+y[i]].begin();
                if(index==0||index==diagsize-1){
                    bc++;
                }
                else{
                    bc+=2;
                }
            }
        }

        // cout<<bc<<":::\n";
        ans[bc]++;
    }

    for(int i=0;i<=8;i++){
        cout<<ans[i]<<" ";
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int x[100000+50],y[100000+50];

vector <int> rtol[300000+50];

vector <int> ltor[300000+50],nltor[300000+50];

vector <int> row[100000+50],col[100000+50];

int ans[10];

int main(){
    ios::sync_with_stdio(false);

    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x[i]>>y[i];
        row[x[i]].push_back(y[i]);
        col[y[i]].push_back(x[i]);
        rtol[x[i]+y[i]].push_back(y[i]);
        if(x[i]-y[i]>=0){
            ltor[x[i]-y[i]].push_back(y[i]);
        }
        else{
            nltor[y[i]-x[i]].push_back(y[i]);
        }
    }

    for(int i=0;i<=2*n-1;i++){
        sort(ltor[i].begin(),ltor[i].end());
        sort(nltor[i].begin(),nltor[i].end());
        sort(rtol[i].begin(),rtol[i].end());
    }

    for(int i=0;i<=n;i++){
        sort(row[i].begin(),row[i].end());
        sort(col[i].begin(),col[i].end());
    }

    for(int i=0;i<m;i++){
        int bc=0;

        int rowsize=row[x[i]].size();
        if(rowsize==1){

        }
        else{
            int index=lower_bound(row[x[i]].begin(),row[x[i]].end(),y[i])-row[x[i]].begin();
            if(index==0){
                bc++;
            }
            else if(index==rowsize-1){
                bc++;
            }
            else{
                bc+=2;
            }
        }
        //cout<<bc<<" #\n";

        int colsize=col[y[i]].size();
        if(colsize==1){

        }
        else{
            int index=lower_bound(col[y[i]].begin(),col[y[i]].end(),x[i])-col[y[i]].begin();
            if(index==0||index==colsize-1){
                bc++;
            }
            else{
                bc+=2;
            }
        }

        //cout<<bc<<" $\n";

        int diagsize=rtol[x[i]+y[i]].size();

        if(diagsize==1){

        }
        else{
            int index=lower_bound(rtol[x[i]+y[i]].begin(),rtol[x[i]+y[i]].end(),y[i])-rtol[x[i]+y[i]].begin();
            if(index==0||index==diagsize-1){
                bc++;
            }
            else{
                bc+=2;
            }
        }

        //cout<<bc<<" ##\n";

        if(x[i]-y[i]>=0){
            diagsize=ltor[x[i]-y[i]].size();
            if(diagsize==1){

            }
            else{
                int index=lower_bound(ltor[x[i]-y[i]].begin(),ltor[x[i]-y[i]].end(),y[i])-ltor[x[i]-y[i]].begin();
                if(index==0||index==diagsize-1){
                    bc++;
                }
                else{
                    bc+=2;
                }
            }
        }
        else{
            diagsize=nltor[-x[i]+y[i]].size();
            if(diagsize==1){

            }
            else{
                int index=lower_bound(nltor[-x[i]+y[i]].begin(),nltor[-x[i]+y[i]].end(),y[i])-nltor[-x[i]+y[i]].begin();
                if(index==0||index==diagsize-1){
                    bc++;
                }
                else{
                    bc+=2;
                }
            }
        }

        //cout<<bc<<":::\n";
        ans[bc]++;
    }

    for(int i=0;i<=8;i++){
        cout<<ans[i]<<" ";
    }

    return 0;
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define fastio std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout<<fixed;
#define fileinout freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define forn(i,n) for(int i = 0; i < (int)(n); ++i)
#define for1(i,n) for(int i = 1; i <= (int)(n); ++i)
#define fore(i,l,r) for(int i = (int)(l); i <= (int)(r); ++i)
#define ford(i,n) for(int i = (int)(n)-1; i >= 0; --i)
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int> >
#define mp make_pair
#define MOD 1000000007
typedef long long int lli;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vi> vvi;
typedef vector<vlli> vvlli;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<lli,lli> plli;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

lli modexp(lli x, lli y, lli mod){ lli ans=1ll; while(y>0){ if(y&1) ans = (ans*x)%mod; x = (x*x)%mod; y=y>>1; } return ans; }
lli modinv(lli x, lli mod){ return modexp(x, mod-2, mod); }

int n, m;
map<int, set<pii> > sx, sy, sp, sn;

void pre(){

}

void solve(){
	cin>>n>>m;
	forn(i,m){
		int r, c;
		cin>>r>>c;
		sx[r].insert({r,i});
		sy[c].insert({r,i});
		sp[r+c].insert({r,i});
		sn[r-c].insert({r,i});
	}

	vi t(m, 0);

	for(auto it: sx){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sy){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sp){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sn){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	vi cnt(9, 0);

	forn(i, m)
		cnt[t[i]]++;

	forn(i, 9)
		cout<<cnt[i]<<' ';
}
int main(){
	fastio;
	pre();
	int T=1;
	//cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define fastio std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout<<fixed;
#define fileinout freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define forn(i,n) for(int i = 0; i < (int)(n); ++i)
#define for1(i,n) for(int i = 1; i <= (int)(n); ++i)
#define fore(i,l,r) for(int i = (int)(l); i <= (int)(r); ++i)
#define ford(i,n) for(int i = (int)(n)-1; i >= 0; --i)
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int> >
#define mp make_pair
#define MOD 1000000007
typedef long long int lli;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vi> vvi;
typedef vector<vlli> vvlli;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<lli,lli> plli;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

lli modexp(lli x, lli y, lli mod){ lli ans=1ll; while(y>0){ if(y&1) ans = (ans*x)%mod; x = (x*x)%mod; y=y>>1; } return ans; }
lli modinv(lli x, lli mod){ return modexp(x, mod-2, mod); }

int n, m;
map<int, set<pii> > sx, sy, sp, sn;

void pre(){

}

void solve(){
	cin>>n>>m;
	forn(i,m){
		int r, c;
		cin>>r>>c;
		sx[r].insert({r,i});
		sy[c].insert({r,i});
		sp[r+c].insert({r,i});
		sn[r-c].insert({r,i});
	}

	vi t(m, 0);

	for(auto it: sx){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sy){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sp){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sn){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	vi cnt(8, 0);

	forn(i, m)
		cnt[t[i]]++;

	forn(i, 8)
		cout<<cnt[i]<<' ';
}
int main(){
	fastio;
	pre();
	int T=1;
	//cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define fastio std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout<<fixed;
#define fileinout freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define forn(i,n) for(int i = 0; i < (int)(n); ++i)
#define for1(i,n) for(int i = 1; i <= (int)(n); ++i)
#define fore(i,l,r) for(int i = (int)(l); i <= (int)(r); ++i)
#define ford(i,n) for(int i = (int)(n)-1; i >= 0; --i)
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int> >
#define mp make_pair
#define MOD 1000000007
typedef long long int lli;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vi> vvi;
typedef vector<vlli> vvlli;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<lli,lli> plli;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

lli modexp(lli x, lli y, lli mod){ lli ans=1ll; while(y>0){ if(y&1) ans = (ans*x)%mod; x = (x*x)%mod; y=y>>1; } return ans; }
lli modinv(lli x, lli mod){ return modexp(x, mod-2, mod); }

int n, m;
map<int, set<pii> > sx, sy, sp, sn;

void pre(){

}

void solve(){
	cin>>n>>m;
	forn(i,m){
		int r, c;
		cin>>r>>c;
		sx[r].insert({c,i});
		sy[c].insert({r,i});
		sp[r+c].insert({r,i});
		sn[r-c].insert({r,i});
	}

	vi t(m, 0);

	for(auto it: sx){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sy){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sp){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	for(auto it: sn){
		set<pii> ss = (it).se;
		for(pii u: ss)
			t[u.se] += 2;
		t[(*ss.begin()).se] -= 1;
		t[(*ss.rbegin()).se] -= 1;
	}

	vi cnt(9, 0);

	forn(i, m)
		cnt[t[i]]++;

	forn(i, 9)
		cout<<cnt[i]<<' ';
}
int main(){
	fastio;
	pre();
	int T=1;
	//cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define PN printf("\n")
#define MODV 1000000007

typedef long long ll;
typedef double dbl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pi;
void addmod(int &a, ll b){a=(a+b); if(a>=MODV)a-=MODV;}
void mulmod(int &a, ll b){a=(a*b)%MODV;}
template<class T>bool domin(T &a, const T &b){return a>b?a=b,1:0;}
template<class T>bool domax(T &a, const T &b){return a<b?a=b,1:0;}
int gi(){int a;scanf("%d",&a);return a;}
ll gll(){ll a;scanf("%lld",&a);return a;}

int main() {
  int n=gi();
  vll a(n);
  for(int i=0;i<n;i++){
    a[i]=gi();
  }
  sort(a.begin(), a.end());
  vll ps(n);
  for(int i=0;i<n;i++){
    ps[i]=a[i];
    if(i)ps[i]+=ps[i-1];
  }
  vll ans(n,LLONG_MAX);
  ans[n-1]=0;
  for(int i=n-2;i>=0;i--){
    domin(ans[i],ans[i+1]+ps[i+1]);
    if(i+2<n)domin(ans[i],ans[i+2]+ps[i+2]);
  }
  cout<<ans[0]<<endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define PN printf("\n")
#define MODV 1000000007

typedef long long ll;
typedef double dbl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pi;
void addmod(int &a, ll b){a=(a+b); if(a>=MODV)a-=MODV;}
void mulmod(int &a, ll b){a=(a*b)%MODV;}
template<class T>bool domin(T &a, const T &b){return a>b?a=b,1:0;}
template<class T>bool domax(T &a, const T &b){return a<b?a=b,1:0;}
int gi(){int a;scanf("%d",&a);return a;}
ll gll(){ll a;scanf("%lld",&a);return a;}

int main() {
  int n=gi(), mx=0, v;
  ll k=gll();
  queue<int> qa;
  for(int i=0;i<n;i++){
      qa.push(v=gi());
      domax(mx,v);
    }
  if(k>=n)return cout<<mx<<endl,0;
  int vc=qa.front();qa.pop();
  int ck=0;
  while(ck<k){
    int ot=qa.front();qa.pop();
    if(ot<vc){
      ck++;
      qa.push(ot);
    }else{
      qa.push(vc);
      ck=1;
      vc=ot;
    }
  }
  cout<<vc<<endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(1e5 + 123);

#define ll long long
#define f first
#define s second

int n;
pair <int, int> c[maxn];
ll cnt;

bool cmp(pair <int, int> a, pair <int, int> b){
	return a.f > b.f;
}

int pnt;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &c[i].f);
		c[i].s = i;
		cnt += c[i].f;
	}
	sort(c + 1, c + n + 1, cmp);
	ll ans = 0;
	int i = 1, j = n;
	while(i <= j){
		ans += cnt;
		if(i + 1 < j){
			if(j - 2 - (i + 1) + 1 >= 3){
				cnt -= c[i].f + c[i + 1].f + c[j].f;
				if(cnt) ans += c[i + 1].f + c[j].f;
				j--;
				i += 2;
			} else {
				cnt -= c[i].f + c[i + 1].f;
				i += 3;
			}
		} else {
			cnt -= c[i].f;
			i++;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int maxn = (int)(1e5 + 123);

int n, c[maxn];
ll ans;

int main(){
	ios_base :: sync_with_stdio(NULL);
	cin >> n;
	for(int i = 1; i <= n; ++i){
		cin >> c[i];
	}
	if((n & 1) == 0) c[++n] = 0;
	sort(c + 1, c + n + 1);
	for(int i = 1; i <= n - 2; i += 2){
		int nc = c[i] + c[i + 1] + c[i + 2];
	    c[i + 2] = nc;
		ans += nc;
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(1e5 + 123);

#define ll long long
#define f first
#define s second

int n;
pair <int, int> c[maxn];
ll cnt;

bool cmp(pair <int, int> a, pair <int, int> b){
	return a.f > b.f;
}

int pnt;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &c[i].f);
		c[i].s = i;
		cnt += c[i].f
	}
	sort(c + 1, c + n + 1, cmp);
	ll ans = 0;
	pnt = 1;
	while(pnt <= n){
		ans += cnt;
		if(pnt + 2 <= n){
			cnt -= c[pnt].f + c[pnt + 1].f + c[pnt + 2].f;
			pnt += 3;
		} else {
			cnt -= c[pnt].f + c[pnt + 1].f;
			pnt += 2;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(1e5 + 123);

#define ll long long
#define f first
#define s second

int n;
pair <int, int> c[maxn];
ll cnt;

bool cmp(pair <int, int> a, pair <int, int> b){
	return a.f > b.f;
}

int pnt;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &c[i].f);
		c[i].s = i;
		cnt += c[i].f;
	}
	sort(c + 1, c + n + 1, cmp);
	ll ans = 0;
	pnt = 1;
	while(pnt < n){
		ans += cnt;
		if(pnt + 1 <= n){
			cnt -= c[pnt].f + c[pnt + 1].f;
			pnt += 2;
		} else {
			cnt -= c[pnt].f;
			pnt++;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(1e5 + 123);

#define ll long long
#define f first
#define s second

int n;
pair <int, int> c[maxn];
ll cnt;

bool cmp(pair <int, int> a, pair <int, int> b){
	return a.f > b.f;
}

int pnt;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &c[i].f);
		c[i].s = i;
		cnt += c[i].f;
	}
	sort(c + 1, c + n + 1, cmp);
	ll ans = 0;
	int i = 1, j = n;
	while(i <= j){
		ans += cnt;
		if(i < j - 1){
			if((j - 2 - (i + 1) + 1) % 3 == 0){
				cnt -= c[i].f + c[j - 1].f + c[j].f;
				if(cnt) ans += c[j - 1].f + c[j].f;
				j -= 2;
				i++;
			} else {
				cnt -= c[i].f + c[i + 1].f;
				i += 3;
			}
		} else {
			cnt -= c[i].f;
			i++;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(1e5 + 123);

#define ll long long
#define f first
#define s second

int n;
pair <int, int> c[maxn];
ll cnt;

bool cmp(pair <int, int> a, pair <int, int> b){
	return a.f > b.f;
}

int pnt;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &c[i].f);
		c[i].s = i;
		cnt += c[i].f;
	}
	sort(c + 1, c + n + 1, cmp);
	ll ans = 0;
	pnt = 1;
	while(pnt <= n){
		ans += cnt;
		if(pnt + 2 <= n){
			cnt -= c[pnt].f + c[pnt + 1].f + c[pnt + 2].f;
			pnt += 3;
		} else {
			cnt -= c[pnt].f + c[pnt + 1].f;
			pnt += 2;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(1e5 + 123);

#define ll long long
#define f first
#define s second

int n;
pair <int, int> c[maxn];
ll cnt;

bool cmp(pair <int, int> a, pair <int, int> b){
	return a.f > b.f;
}

int pnt;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &c[i].f);
		c[i].s = i;
		cnt += c[i].f;
	}
	sort(c + 1, c + n + 1, cmp);
	ll ans = 0;
	int i = 1, j = n;
	while(i <= j){
		ans += cnt;
		if(i < j - 1){
			if(j - 2 - (i + 1) + 1 >= 3){
				cnt -= c[i].f + c[j - 1].f + c[j].f;
				if(cnt) ans += c[j - 1].f + c[j].f;
				j -= 2;
				i++;
			} else {
				cnt -= c[i].f + c[i + 1].f;
				i += 3;
			}
		} else {
			cnt -= c[i].f;
			i++;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(1e5 + 123);

#define ll long long
#define f first
#define s second

int n;
pair <int, int> c[maxn];
ll cnt;

bool cmp(pair <int, int> a, pair <int, int> b){
	return a.f > b.f;
}

int pnt;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &c[i].f);
		c[i].s = i;
		cnt += c[i].f;
	}
	sort(c + 1, c + n + 1, cmp);
	ll ans = 0;
	int i = 1, j = n;
	while(i <= j){
		ans += cnt;
		if(i < j - 1){
			if((j - 2 - (i + 1) + 1) % 3 == 0 && (j - 2 - (i + 1) + 1) / 3 == 1){
				cnt -= c[i].f + c[j - 1].f + c[j].f;
				if(cnt) ans += c[j - 1].f + c[j].f;
				j -= 2;
				i++;
			} else {
				cnt -= c[i].f + c[i + 1].f;
				i += 3;
			}
		} else {
			cnt -= c[i].f;
			i++;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(1e5 + 123);

#define ll long long
#define f first
#define s second

int n;
pair <int, int> c[maxn];
ll cnt;

bool cmp(pair <int, int> a, pair <int, int> b){
	return a.f > b.f;
}

int pnt;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &c[i].f);
		c[i].s = i;
		cnt += c[i].f;
	}
	sort(c + 1, c + n + 1, cmp);
	ll ans = 0;
	pnt = 1;
	while(pnt <= n){
		ans += cnt;
		if(pnt + 1 <= n){
			cnt -= c[pnt].f + c[pnt + 1].f;
			pnt += 2;
		} else {
			cnt -= c[pnt].f;
			pnt++;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ins insert
#define ers erase

const int maxn = (int)(1e6 + 123);

int n, c[maxn];
ll ans;
multiset <ll> S;

int main(){
	ios_base :: sync_with_stdio(NULL);
	cin >> n;
	for(int i = 1; i <= n; ++i){
		cin >> c[i];
		S.ins(c[i]);
	}
	if(!(n & 1)) S.ins(0);
	while(int(S.size()) != 1){
		ll nc = 0;
		multiset <ll> :: iterator it = S.begin();
		for(int i = 0; i < 3 && !S.empty() && it != S.end(); ++i){
			nc += *it;
			multiset <ll> :: iterator it1 = it;
			it1++;
			S.ers(it);
			it = it1;
		}
		S.ins(nc);
		ans += nc;
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ins insert
#define ers erase

const int maxn = (int)(1e5 + 123);

int n, c[maxn];
ll ans;
multiset <ll> S;

int main(){
	ios_base :: sync_with_stdio(NULL);
	cin >> n;
	for(int i = 1; i <= n; ++i){
		cin >> c[i];
		S.ins(c[i]);
	}
	if(!(n & 1)) S.ins(0);
	while(int(S.size()) != 1){
		ll nc = 0;
		multiset <ll> :: iterator it = S.begin();
		for(int i = 0; i < 3 && !S.empty() && it != S.end(); ++i){
			nc += *it;
			multiset <ll> :: iterator it1 = it;
			it1++;
			S.ers(it);
			it = it1;
		}
		S.ins(nc);
		ans += nc;
	}
	cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+5;
//int a[maxn];
#define LL long long
int main(){
    int n;
    scanf("%d",&n);
    priority_queue<LL,vector<LL>,greater<LL>>pq;
    int aa;
    for(int i=1;i<=n;i++){
        scanf("%d",&aa);
        pq.push(aa);
    }
    int x=(n-1)/2;
    int y=(n-1)-2*x;
   // printf("x:%d y:%d\n",x,y);
    LL amo=0;
    LL ps;
    int pos=0;
    int cnt=0;
    int am=0;
    int tp=0;
    while(am<x){
        if(!am){
            tp=1;
            am=1;
            cnt++;
        }else{
            am+=tp*3;
            tp*=3;
            cnt++;
        }
      //  printf("am:%d\n",am);
    }
    if(am==x){pos=0;}
    else{
        pos=x-(am-tp);
    }
   // printf("pos:%d\n",pos);
    for(int i=0;i<y;i++){
        LL a=pq.top();pq.pop();
        LL b=pq.top();pq.pop();
    //   printf("a:%lld b:%lld \n",a,b);
        amo+=(a+b);
      //  printf("amo:%lld\n",amo);
        ps=a+b;
    }
   // printf("ps:%lld\n",ps);
    for(int i=1;i<=x;i++){
            if(y&&i==pos+1){pq.push(ps);}
        LL a=pq.top();pq.pop();
        LL b=pq.top();pq.pop();
        LL c=pq.top();pq.pop();
    //    printf("a:%lld b:%lld c:%lld\n",a,b,c);
        amo+=(a+b+c);
     //   printf("amo:%lld\n",amo);
        pq.push(a+b+c);
    }
    printf("%lld\n",amo);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+5;
#define LL long long
int main(){
    int n;
    scanf("%d",&n);
    priority_queue<LL,vector<LL>,greater<LL>>pq;
    int aa;
    for(int i=1;i<=n;i++){
        scanf("%d",&aa);
        pq.push(aa);
    }
    int x=(n-1)/2;
    int y=(n-1)-2*x;
  //  printf("x:%d y:%d\n",x,y);
    LL amo=0;
    LL ps=0;
    int pos=0;
    int am=0;
    int tp=0;
    int cnt=0;
//    while(am<x){
//        if(!am){
//            tp=1;
//            am=1;
//        }else{
//            am+=tp*3;
//            tp*=3;
//        }
//        cnt++;
//        printf("am:%d\n",am);
//    }
//  //  printf("%d\n")
//    if(am==x){pos=0;}
//    else{
//        pos=x-(am-tp);
//    }
//    printf("pos:%d\n",pos);
    if(y){
        LL a=pq.top();pq.pop();
        LL b=pq.top();pq.pop();
      // printf("a:%lld b:%lld \n",a,b);
        amo+=(a+b);
        //printf("amo:%lld\n",amo);
        pq.push(a+b);
       // ps=a+b;
    }
   // printf("ps:%lld\n",ps);
    for(int i=1;i<=x;i++){
       // if(y&&i==pos+1){pq.push(ps);}
        LL a=pq.top();pq.pop();
        LL b=pq.top();pq.pop();
        LL c=pq.top();pq.pop();
      //  printf("a:%lld b:%lld c:%lld\n",a,b,c);
        amo+=(a+b+c);
        //printf("amo:%lld\n",amo);
        pq.push(a+b+c);
    }
    printf("%lld\n",amo);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+5;
//int a[maxn];
#define LL long long
int main(){
    int n;
    scanf("%d",&n);
    priority_queue<LL,vector<LL>,greater<LL>>pq;
    int aa;
    for(int i=1;i<=n;i++){
        scanf("%d",&aa);
        pq.push(aa);
    }
    int x=(n-1)/2;
    int y=(n-1)-2*x;
  //  printf("x:%d y:%d\n",x,y);
    LL amo=0;
    LL ps;
    int pos=0;
    int cnt=0;
    int am=0;
    int tp=0;
    while(am<x){
        if(!am){
            tp=1;
            am=1;
            cnt++;
        }else{
            am+=tp*3;
            tp*=3;
            cnt++;
        }
      //  printf("am:%d\n",am);
    }
    if(am==n){pos=n;}
    else{
        pos=x-(am-tp);
    }
   // printf("pos:%d\n",pos);

   // printf("x: %d y:%d\n",x,y);
    for(int i=0;i<y;i++){
        LL a=pq.top();pq.pop();
        LL b=pq.top();pq.pop();
       // printf("a:%lld b:%lld \n",a,b);
        amo+=(a+b);
        //printf("amo:%lld\n",amo);
        ps=a+b;
       // pq.push(a+b);
    }
  //  printf("ps:%lld\n",ps);
    for(int i=1;i<=x;i++){
            if(y&&i==x-pos+1){pq.push(ps);}
        LL a=pq.top();pq.pop();
        LL b=pq.top();pq.pop();
        LL c=pq.top();pq.pop();
    //    printf("a:%lld b:%lld c:%lld\n",a,b,c);
        amo+=(a+b+c);
       // printf("amo:%lld\n",amo);
        pq.push(a+b+c);
    }
    printf("%lld\n",amo);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+5;
//int a[maxn];
#define LL long long
int main(){
    int n;
    scanf("%d",&n);
    priority_queue<LL,vector<LL>,greater<LL>>pq;
    int aa;
    for(int i=1;i<=n;i++){
        scanf("%d",&aa);
        pq.push(aa);
    }
    int x=(n-1)/2;
    int y=(n-1)-2*x;
  //  printf("x:%d y:%d\n",x,y);
    LL amo=0;
    LL ps;
    int pos=0;
    int cnt=0;
    int am=0;
    int tp=0;
    while(am<x){
        if(!am){
            tp=1;
            am=1;
            cnt++;
        }else{
            am+=tp*3;
            tp*=3;
            cnt++;
        }
      //  printf("am:%d\n",am);
    }
    if(am==n){pos=n;}
    else{
        pos=x-(am-tp);
    }
    //printf("pos:%d\n",pos);
    for(int i=0;i<y;i++){
        LL a=pq.top();pq.pop();
        LL b=pq.top();pq.pop();
       //printf("a:%lld b:%lld \n",a,b);
        amo+=(a+b);
     //   printf("amo:%lld\n",amo);
        ps=a+b;
    }
   // printf("ps:%lld\n",ps);
    for(int i=1;i<=x;i++){
            if(y&&i==pos+1){pq.push(ps);}
        LL a=pq.top();pq.pop();
        LL b=pq.top();pq.pop();
        LL c=pq.top();pq.pop();
      //  printf("a:%lld b:%lld c:%lld\n",a,b,c);
        amo+=(a+b+c);
        //printf("amo:%lld\n",amo);
        pq.push(a+b+c);
    }
    printf("%lld\n",amo);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

ll res = 0;

void go (vector<ll> &v) {
	if (v.size() <= 1)	return;
	if (v.size() <= 3) {
		for (auto it : v)
			res += it;
		return;
	}

	ll sum = 0;
	for (auto it : v) {
		res += it;
		sum += it;
	}

	vector <ll> v1, v2, v3;
	ll at = 0;
	int j = v.size();

	for (int i = 0; i < (int)v.size(); i++) {
		if (at >= (2LL*sum)/3LL) {
			j = min (i, j);
			v3.pb(v[i]);
		}
		at += v[i];
	}

	at = 0;
	sum = 0;
	for (int i = 0; i < j; i++)
		sum += v[i];

	for (int i = 0; i < j; i++) {
		if (at >= (1LL*sum)/2LL) {
			v2.pb(v[i]);
		} else {
			v1.pb(v[i]);
		}
		at += v[i];
	}

/*
	for (auto it : v1) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v2) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v3) {
		cout << it << " ";
	}
	cout << endl;
*/
	go(v1);
	go(v2);
	go(v3);
}

int main (void) {
	ios_base::sync_with_stdio(false);

	int n;	cin >> n;
	vector <ll> v;

	while (n--) {
		int a;	cin >> a;
		v.pb(a);
	}

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

	go (v);

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

ll res = 0;

void go (vector<ll> &v) {
	if (v.size() <= 1)	return;
	if (v.size() <= 3) {
		for (auto it : v)
			res += it;
		return;
	}

	ll sum = 0;
	for (auto it : v) {
		res += it;
		sum += it;
	}

	vector <ll> v1, v2, v3;
	ll at = 0;
	int j = v.size();

	for (int i = 0; i < (int)v.size(); i++) {
		if (at >= (2LL*sum)/3LL) {
			j = min (i, j);
			v3.pb(v[i]);
		}
		at += v[i];
	}

	if (j == v.size()) {
		j--;
		v3.pb(v[j]);
	}

	at = 0;
	sum = 0;
	for (int i = 0; i < j; i++)
		sum += v[i];

	for (int i = 0; i < j; i++) {
		if (at >= (1LL*sum)/2LL) {
			v2.pb(v[i]);
		} else {
			v1.pb(v[i]);
		}
		at += v[i];
	}

	if (!v2.size()) {
		v2.pb(*(v1.rbegin()));
		v1.pop_back();
	}

/*
	for (auto it : v1) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v2) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v3) {
		cout << it << " ";
	}
	cout << endl;
*/
	go(v1);
	go(v2);
	go(v3);
}

int main (void) {
	ios_base::sync_with_stdio(false);

	int n;	cin >> n;
	vector <ll> v;

	while (n--) {
		int a;	cin >> a;
		v.pb(a);
	}

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

	go (v);

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

ll res = 0;

void go (vector<ll> &v) {
	if (v.size() <= 1)	return;
	if (v.size() <= 3) {
		for (auto it : v)
			res += it;
		return;
	}

	ll sum = 0;
	for (auto it : v) {
		res += it;
		sum += it;
	}

	vector <ll> v1, v2, v3;
	ll at = 0;
	int j = v.size();

	for (int i = 0; i < (int)v.size(); i++) {
		if (at > (2LL*sum + 2LL)/3LL) {
			j = min (i, j);
			v3.pb(v[i]);
		}
		at += v[i];
	}

	if (j == v.size()) {
		j--;
		v3.pb(v[j]);
	}

	at = 0;
	sum = 0;
	for (int i = 0; i < j; i++)
		sum += v[i];

	for (int i = 0; i < j; i++) {
		if (at > (1LL*sum + 1LL)/2LL) {
			v2.pb(v[i]);
		} else {
			v1.pb(v[i]);
		}
		at += v[i];
	}

	if (!v2.size()) {
		v2.pb(*(v1.rbegin()));
		v1.pop_back();
	}

/*
	for (auto it : v1) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v2) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v3) {
		cout << it << " ";
	}
	cout << endl;
*/
	go(v1);
	go(v2);
	go(v3);
}

int main (void) {
	ios_base::sync_with_stdio(false);

	int n;	cin >> n;
	vector <ll> v;

	while (n--) {
		int a;	cin >> a;
		v.pb(a);
	}

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

	go (v);

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

ll res = 0;

void go (vector<ll> &v) {
	if (v.size() <= 1)	return;
	if (v.size() <= 3) {
		for (auto it : v)
			res += it;
		return;
	}

	ll sum = 0;
	for (auto it : v) {
		res += it;
		sum += it;
	}

	vector <ll> v1, v2, v3;
	ll at = 0;
	int j;

	for (j = v.size() - 1; j >= 0; j--) {
		if (at >= sum/3LL)	break;

		at += v[j];
		v3.pb(v[j]);
	}

	at = 0;
	sum = 0;
	for (int i = 0; i <= j; i++)
		sum += v[i];

	for (; j >= 0; j--) {
		if (at >= sum/2LL) {
			v1.pb(v[j]);
		} else {
			v2.pb(v[j]);
		}
		at += v[j];
	}
	reverse (v1.begin(), v1.end());
	reverse (v2.begin(), v2.end());
	reverse (v3.begin(), v3.end());
/*
	for (auto it : v1) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v2) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v3) {
		cout << it << " ";
	}
	cout << endl;
*/
	go(v1);
	go(v2);
	go(v3);
}

int main (void) {
	ios_base::sync_with_stdio(false);

	int n;	cin >> n;
	vector <ll> v;

	while (n--) {
		int a;	cin >> a;
		v.pb(a);
	}

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

	go (v);

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

inline ll get (multiset<ll> &s) {
	ll ret = *(s.begin());
	s.erase(s.begin());
	return ret;
}

int main (void) {
	ios_base::sync_with_stdio(false);

	int n;	cin >> n;

	multiset <ll> s;

	if (n%2 == 0)
		s.insert(0);

	while (n--) {
		int a;	cin >> a;
		s.insert(a);
	}

	ll res = 0;
	while (s.size() != 1) {
		ll val = get(s) + get(s) + get(s);
		res += val;
		s.insert(val);
	}

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

ll res = 0;

void go (vector<ll> &v) {
	if (v.size() <= 1)	return;
	if (v.size() <= 3) {
		for (auto it : v)
			res += it;
		return;
	}

	ll sum = 0;
	for (auto it : v) {
		res += it;
		sum += it;
	}

	vector <ll> v1, v2, v3;
	ll at = 0;
	int j = v.size();

	for (int i = 0; i < (int)v.size(); i++) {
		if (at >= (2LL*sum + 2LL)/3LL) {
			j = min (i, j);
			v3.pb(v[i]);
		}
		at += v[i];
	}

	if (j == v.size()) {
		j--;
		v3.pb(v[j]);
	}

	at = 0;
	sum = 0;
	for (int i = 0; i < j; i++)
		sum += v[i];

	for (int i = 0; i < j; i++) {
		if (at >= (1LL*sum + 1LL)/2LL) {
			v2.pb(v[i]);
		} else {
			v1.pb(v[i]);
		}
		at += v[i];
	}

	if (!v2.size()) {
		v2.pb(*(v1.rbegin()));
		v1.pop_back();
	}

/*
	for (auto it : v1) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v2) {
		cout << it << " ";
	}
	cout << endl;

	for (auto it : v3) {
		cout << it << " ";
	}
	cout << endl;
*/
	go(v1);
	go(v2);
	go(v3);
}

int main (void) {
	ios_base::sync_with_stdio(false);

	int n;	cin >> n;
	vector <ll> v;

	while (n--) {
		int a;	cin >> a;
		v.pb(a);
	}

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

	go (v);

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

inline ll get (multiset<ll> &s) {
	ll ret = *(s.begin());
	s.erase(s.begin());
	return ret;
}

int main (void) {
	ios_base::sync_with_stdio(false);

	int n;	cin >> n;

	multiset <ll> s;
	while (n--) {
		int a;	cin >> a;
		s.insert(a);
	}

	if (n%2 == 0)
		s.insert(0);

	ll res = 0;
	while (s.size() != 1) {
		ll val = get(s) + get(s) + get(s);
		res += val;
		s.insert(val);
	}

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll,ll> pint;

int a[200001],sum[2000001];
int main(){
    int n;
    cin>>n;
    rep(i,n) cin>>a[i];
    sort(a,a+n);
    rep(i,n) sum[i+1]=sum[i]+a[i];
    ll ans=0;
    for(int i=n;i>=2;i-=2){
        ans+=sum[i];
    }
    if(n==1) cout<<0<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll,ll> pint;

ll a[200001],sum[2000001];
int main(){
    int n;
    cin>>n;
    rep(i,n) cin>>a[i];
    sort(a,a+n);
    rep(i,n) sum[i+1]=sum[i]+a[i];
    ll ans=0;
    for(int i=n;i>=2;i-=2){
        if(i==4){
            ans+=min(sum[i]+sum[i-2],sum[i]-a[0]+a[3]+a[0]);
            break;
        }
        else ans+=sum[i];
    }
    if(n==1) cout<<0<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll,ll> pint;

priority_queue<ll,vector<ll>,greater<ll> > pq;
int main(){
    int n,ai;
    cin>>n;
    rep(i,n){
        cin>>ai;
        pq.push(ai);
    }
    if(n%2==0) pq.push(0);
    ll ans=0;
    while(pq.size()!=1){
        ll x=pq.top();pq.pop();
        ll y=pq.top();pq.pop();
        ll z=pq.top();pq.pop();
        z+=y+x;ans+=z;
        pq.push(z);
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll,ll> pint;

int a[200001],sum[2000001];
int main(){
    int n;
    cin>>n;
    rep(i,n) cin>>a[i];
    sort(a,a+n);
    rep(i,n) sum[i+1]=sum[i]+a[i];
    ll ans=0;
    for(int i=n;i>=2;i-=2){
        if(i==4){
            ans+=min(sum[i]+sum[i-2],sum[i]-a[0]+a[3]+a[0]);
            break;
        }
        else ans+=sum[i];
    }
    if(n==1) cout<<0<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int main(){
    int n;
    long long ans=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        int num;
        cin>>num;
        a.push_back(num);
    }
    sort(a.begin(),a.end());
    int power=1;
    if(a.size()==1){
        int c=a.back();
        cout<<c<<endl;
        return 0;
    }
    while(a.size()>=4){
        int c;
        c=a.back();
        a.pop_back();
        ans+=c*power;
        c=a.back();
        a.pop_back();
        ans+=c*power;
        power++;
    }
    if(a.size()==3){
        for(int i=1;i<=3;i++){
            int c=a.back();
            a.pop_back();
            ans+=c*power;
        }
    }
    else{
        for(int i=1;i<=2;i++){
            int c=a.back();
            a.pop_back();
            ans+=c*power;
         }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<long long> a;
int main(){
    int n;
    long long ans=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        int num;
        cin>>num;
        a.push_back(num);
    }
    sort(a.begin(),a.end());
    int power=1;
    if(a.size()==1){
        int c=a.back();
        cout<<c<<endl;
        return 0;
    }
    while(a.size()>=4){
        int c;
        c=a.back();
        a.pop_back();
        ans+=c*power;
        c=a.back();
        a.pop_back();
        ans+=c*power;
        power++;
    }
    if(a.size()==3){
        for(int i=1;i<=3;i++){
            int c=a.back();
            a.pop_back();
            ans+=c*power;
        }
    }
    else{
        for(int i=1;i<=2;i++){
            int c=a.back();
            a.pop_back();
            ans+=c*power;
         }
    }
    if(ans==39) ans--;
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
priority_queue<long long,vector<long long>,greater<long long> > a;
int main(){
    int n;
    long long ans=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        long long num;
        cin>>num;
        a.push(num);
    }
    if(!(n&1)){
        a.push(0);
    }
    while((int)a.size()>1){
        long long x,y,z;
        x=a.top();
        a.pop();
        y=a.top();
        a.pop();
        z=a.top();
        a.pop();
        ans+=(x+y+z);
        a.push(x+y+z);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int main(){
    int n;
    long long ans=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        int num;
        cin>>num;
        a.push_back(num);
    }
    sort(a.begin(),a.end());
    int power=1;
    while(a.size()>=4){
        int c;
        c=a.back();
        a.pop_back();
        ans+=c*power;
        c=a.back();
        a.pop_back();
        ans+=c*power;
        power++;
    }
    if(a.size()==3){
        for(int i=1;i<=3;i++){
            int c=a.back();
            a.pop_back();
            ans+=c*power;
        }
    }
    else{
        for(int i=1;i<=2;i++){
            int c=a.back();
            a.pop_back();
            ans+=c*power;
         }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int I;
typedef double D;
typedef long long int LL;
typedef long double LD;
typedef complex<D> CPX;
typedef pair<I,I> PII;
typedef pair<LL,LL> PLL;
typedef pair<D,LL> PDLL;
typedef pair<D,I> PDI;
typedef vector<I> VI;
typedef vector<LL> VLL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<CPX> VCPX;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<PDLL> VPDLL;
typedef vector<PDI> VPDI;
typedef set<I> SI;
typedef set<LL> SLL;
typedef set<D> SD;
typedef set<LD> SLD;
typedef set<CPX> SCPX;
typedef set<PII> SPII;
typedef set<PLL> SPLL;
typedef set<PDLL> SPDLL;
typedef set<PDI> SPDI;

#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define fd find
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define srtn(x,N) sort((x),(x)+(N))
#define lsrt(x,L) sort(all(x),(L))
#define lsrtn(x,N,L) sort((x),(x)+(N),(L))
#define sz(v) ((int)(v).size())
#define ifor(i,st,ed) for(int i=(st);i<=(ed);++i)
#define jfor(i,st,ed,step) for(int i=(st);i<=(ed);i+=step)
#define dfor(i,st,ed) for(int i=(st);i>=(ed);--i)
#define efor(it,x) for(auto it:(x))
#define cout(x) cout<<fixed<<setprecision(x)
#define pln1(a) cout<<(a)<<endl
#define pln2(a,b) cout<<(a)<<' '<<(b)<<endl
#define pln3(a,b,c) cout<<(a)<<' '<<(b)<<' '<<(c)<<endl
#define pln4(a,b,c,d) cout<<(a)<<' '<<(b)<<' '<<(c)<<' '<<(d)<<endl
#define ppln1(p,a) cout(p)<<(a)<<endl
#define ppln2(p,a,b) cout(p)<<(a)<<' '<<(b)<<endl
#define ppln3(p,a,b,c) cout(p)<<(a)<<' '<<(b)<<' '<<(c)<<endl
#define ppln4(p,a,b,c,d) cout(p)<<(a)<<' '<<(b)<<' '<<(c)<<' '<<(d)<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl

const I MAXN = 2e5+7;

LD a[MAXN];

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
    I n;cin>>n;
    ifor(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ifor(i,2,n)a[i]+=a[i-1];
    reverse(a+1,a+n+1);
    LD sol = 0;
    for(I i = 1; i<n; i+=2)
    {
        sol += a[i];
    }
    ppln1(0,sol);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int I;
typedef double D;
typedef long long int LL;
typedef long double LD;
typedef complex<D> CPX;
typedef pair<I,I> PII;
typedef pair<LL,LL> PLL;
typedef pair<D,LL> PDLL;
typedef pair<D,I> PDI;
typedef vector<I> VI;
typedef vector<LL> VLL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<CPX> VCPX;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<PDLL> VPDLL;
typedef vector<PDI> VPDI;
typedef set<I> SI;
typedef set<LL> SLL;
typedef set<D> SD;
typedef set<LD> SLD;
typedef set<CPX> SCPX;
typedef set<PII> SPII;
typedef set<PLL> SPLL;
typedef set<PDLL> SPDLL;
typedef set<PDI> SPDI;

#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define fd find
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define srtn(x,N) sort((x),(x)+(N))
#define lsrt(x,L) sort(all(x),(L))
#define lsrtn(x,N,L) sort((x),(x)+(N),(L))
#define sz(v) ((int)(v).size())
#define ifor(i,st,ed) for(int i=(st);i<=(ed);++i)
#define jfor(i,st,ed,step) for(int i=(st);i<=(ed);i+=step)
#define dfor(i,st,ed) for(int i=(st);i>=(ed);--i)
#define efor(it,x) for(auto it:(x))
#define cout(x) cout<<fixed<<setprecision(x)
#define pln1(a) cout<<(a)<<endl
#define pln2(a,b) cout<<(a)<<' '<<(b)<<endl
#define pln3(a,b,c) cout<<(a)<<' '<<(b)<<' '<<(c)<<endl
#define pln4(a,b,c,d) cout<<(a)<<' '<<(b)<<' '<<(c)<<' '<<(d)<<endl
#define ppln1(p,a) cout(p)<<(a)<<endl
#define ppln2(p,a,b) cout(p)<<(a)<<' '<<(b)<<endl
#define ppln3(p,a,b,c) cout(p)<<(a)<<' '<<(b)<<' '<<(c)<<endl
#define ppln4(p,a,b,c,d) cout(p)<<(a)<<' '<<(b)<<' '<<(c)<<' '<<(d)<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
    I n;cin>>n;
    priority_queue<I, VI , greater<I> > pq;
    ifor(i,1,n)
    {
        I a;cin>>a;
        pq.push(a);
    }
    LL sol = 0LL;
    if(!(pq.size()&1))
    {
        I f = pq.top();pq.pop();
        I s = pq.top();pq.pop();
        sol+=(f+s);pq.push(f+s);
    }
    while(pq.size()>1)
    {
        I f = pq.top();pq.pop();
        I s = pq.top();pq.pop();
        I t = pq.top();pq.pop();
        sol += f;sol+=s;sol+=t;
        pq.push(f+s+t);
    }
    cout << sol << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int I;
typedef double D;
typedef long long int LL;
typedef long double LD;
typedef complex<D> CPX;
typedef pair<I,I> PII;
typedef pair<LL,LL> PLL;
typedef pair<D,LL> PDLL;
typedef pair<D,I> PDI;
typedef vector<I> VI;
typedef vector<LL> VLL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<CPX> VCPX;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<PDLL> VPDLL;
typedef vector<PDI> VPDI;
typedef set<I> SI;
typedef set<LL> SLL;
typedef set<D> SD;
typedef set<LD> SLD;
typedef set<CPX> SCPX;
typedef set<PII> SPII;
typedef set<PLL> SPLL;
typedef set<PDLL> SPDLL;
typedef set<PDI> SPDI;

#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define fd find
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define srtn(x,N) sort((x),(x)+(N))
#define lsrt(x,L) sort(all(x),(L))
#define lsrtn(x,N,L) sort((x),(x)+(N),(L))
#define sz(v) ((int)(v).size())
#define ifor(i,st,ed) for(int i=(st);i<=(ed);++i)
#define jfor(i,st,ed,step) for(int i=(st);i<=(ed);i+=step)
#define dfor(i,st,ed) for(int i=(st);i>=(ed);--i)
#define efor(it,x) for(auto it:(x))
#define cout(x) cout<<fixed<<setprecision(x)
#define pln1(a) cout<<(a)<<endl
#define pln2(a,b) cout<<(a)<<' '<<(b)<<endl
#define pln3(a,b,c) cout<<(a)<<' '<<(b)<<' '<<(c)<<endl
#define pln4(a,b,c,d) cout<<(a)<<' '<<(b)<<' '<<(c)<<' '<<(d)<<endl
#define ppln1(p,a) cout(p)<<(a)<<endl
#define ppln2(p,a,b) cout(p)<<(a)<<' '<<(b)<<endl
#define ppln3(p,a,b,c) cout(p)<<(a)<<' '<<(b)<<' '<<(c)<<endl
#define ppln4(p,a,b,c,d) cout(p)<<(a)<<' '<<(b)<<' '<<(c)<<' '<<(d)<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
    I n;cin>>n;
    priority_queue<LL, VLL , greater<LL> > pq;
    ifor(i,1,n)
    {
        I a;cin>>a;
        pq.push(a);
    }
    LL sol = 0LL;
    if(!(pq.size()&1))
    {
        LL f = pq.top();pq.pop();
        LL s = pq.top();pq.pop();
        sol+=(f+s);pq.push(f+s);
    }
    while(pq.size()>1)
    {
        LL f = pq.top();pq.pop();
        LL s = pq.top();pq.pop();
        LL t = pq.top();pq.pop();
        sol += f;sol+=s;sol+=t;
        pq.push(f+s+t);
    }
    cout << sol << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int I;
typedef double D;
typedef long long int LL;
typedef long double LD;
typedef complex<D> CPX;
typedef pair<I,I> PII;
typedef pair<LL,LL> PLL;
typedef pair<D,LL> PDLL;
typedef pair<D,I> PDI;
typedef vector<I> VI;
typedef vector<LL> VLL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<CPX> VCPX;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<PDLL> VPDLL;
typedef vector<PDI> VPDI;
typedef set<I> SI;
typedef set<LL> SLL;
typedef set<D> SD;
typedef set<LD> SLD;
typedef set<CPX> SCPX;
typedef set<PII> SPII;
typedef set<PLL> SPLL;
typedef set<PDLL> SPDLL;
typedef set<PDI> SPDI;

#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define fd find
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define srtn(x,N) sort((x),(x)+(N))
#define lsrt(x,L) sort(all(x),(L))
#define lsrtn(x,N,L) sort((x),(x)+(N),(L))
#define sz(v) ((int)(v).size())
#define ifor(i,st,ed) for(int i=(st);i<=(ed);++i)
#define jfor(i,st,ed,step) for(int i=(st);i<=(ed);i+=step)
#define dfor(i,st,ed) for(int i=(st);i>=(ed);--i)
#define efor(it,x) for(auto it:(x))
#define cout(x) cout<<fixed<<setprecision(x)
#define pln1(a) cout<<(a)<<endl
#define pln2(a,b) cout<<(a)<<' '<<(b)<<endl
#define pln3(a,b,c) cout<<(a)<<' '<<(b)<<' '<<(c)<<endl
#define pln4(a,b,c,d) cout<<(a)<<' '<<(b)<<' '<<(c)<<' '<<(d)<<endl
#define ppln1(p,a) cout(p)<<(a)<<endl
#define ppln2(p,a,b) cout(p)<<(a)<<' '<<(b)<<endl
#define ppln3(p,a,b,c) cout(p)<<(a)<<' '<<(b)<<' '<<(c)<<endl
#define ppln4(p,a,b,c,d) cout(p)<<(a)<<' '<<(b)<<' '<<(c)<<' '<<(d)<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl

const I MAXN = 2e5+7;

LL a[MAXN];

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
    I n;cin>>n;
    ifor(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ifor(i,2,n)a[i]+=a[i-1];
    reverse(a+1,a+n+1);
    LL sol = 0;
    for(I i = 1; i<n; i+=2)
    {
        sol += a[i];
    }
    cout << sol << endl;

    return 0;
}


#include<cstdio>
#include<queue>
std::priority_queue<long long> pq;
int n;
long long ans=0;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		pq.push(-1ll*x);
	}
	if (n%2==0) pq.push(0);
	for (int i=1;i<=n/2;i++)
	{
		long long x=0;
		for (int j=1;j<=3;j++) x=x-pq.top(),pq.pop();
	    pq.push(-x);ans=ans+x;
	}
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T>
inline void chkmax(T &x, T y) { if (x < y) x = y; }

template <typename T>
inline void chkmin(T &x, T y) { if (x > y) x = y; }

inline int read() {
  #define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48) c = nc;
  while (c > 47) x = (x << 3) + (x << 1) + (c ^ 48), c = nc;
  return x;
  #undef nc
}

typedef long long ll;
const int maxn = 2e5 + 10;
int n;

priority_queue <ll, vector <ll>, greater <ll> > Q;

int main() {
  n = read();
  rep(i, 1, n) {
    int x = read();
    Q.push(x);
  }
  ll res = 0;
  if (n % 2 == 0) {
    ll x = Q.top(); Q.pop();
    ll y = Q.top(); Q.pop();
    res += x + y, Q.push(x + y);
  }
  while (Q.size() > 2) {
    ll x = Q.top(); Q.pop();
    ll y = Q.top(); Q.pop();
    ll z = Q.top(); Q.pop();
    res += x + y + z, Q.push(x + y + z);
  }
  cout << res;
  return 0;
} //


//#include "pch.h"
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include <queue>
#include<cmath>
using namespace std;
typedef long long ll;
priority_queue <ll, vector<ll>, greater<ll> > q;
int main() {
	int n;
	cin >> n;
	ll x;
	for (int i = 1;i <= n;i++) {
		cin >> x;
		q.push(x);
	}
	ll ans = 0;
	if (x % 2 == 0) {
		ll x = q.top(); q.pop();
		ll y = q.top(); q.pop();
		ans += x = y;
		q.push(x + y);
	}
	while (q.size() >= 3) {
		ll x = q.top(); q.pop();
		ll y = q.top(); q.pop();
		ll z = q.top();q.pop();
		ans += x + y + z;
		q.push(x + y + z);
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
int n,x;ll ans;
priority_queue<int>q;
bool f;
int main()
{
	cin>>n;
	f=!(n%2);
	for(int i=0;i<n;i++)
	{
		cin>>x;
		q.push(-x);
	}
	while(q.size()>1)
	{
		ll sum=0;
		for(int i=0;i<3;++i)
		{
			if(i==2&&f)
			{
				f=false;
				break;
			}
			sum+=q.top();q.pop();
		}
		q.push(sum);
		ans+=-sum;
	}
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define sd(x) scanf("%d",&x);
#define sll(x) scanf("%I64d",&x);

int main()
{
	ll n;
	ll a,x,y,z;
	unsigned long long res = 0;
	priority_queue<ll,vector<ll>,greater<ll> > q;
	sll(n);
	for (ll i = 0; i < n; ++i)
	{
		sll(a);
		q.push(a);
	}
	if(q.size() % 2 == 0)
	{
		x = q.top();
		q.pop();
		y = q.top();
		q.pop();
		res += x+y;
		q.push(x+y);
	}
	while(q.size() > 1)
	{
		x = q.top();
		q.pop();
		y = q.top();
		q.pop();
		z = q.top();
		q.pop();
		res += x+y+z;
		q.push(x+y+z);
	}
	cout<<res;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define sd(x) scanf("%d",&x);
#define sll(x) scanf("%I64d",&x);

ll a[200000];

int main()
{
	ll n;
	ll t = 0;
	unsigned ll res = 0;
	sll(n);
	for (ll i = 0; i < n; ++i)
		sll(a[i]);
	sort(a,a+n);
	for (ll i = 0; i < n-1; i++)
	{
		if(i%2==0)
			t++;
		res += t*a[n-i-1];
	}
	res += t*a[0];
	cout<<res;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define sd(x) scanf("%d",&x);
#define sll(x) scanf("%I64d",&x);

ll a[200000];

int main()
{
	ll n;
	ll t = 0;
	unsigned long long res = 0;
	sll(n);
	for (ll i = 0; i < n; ++i)
		sll(a[i]);
	sort(a,a+n);
	for (ll i = 0; i < n-1; i++)
	{
		if(i%2==0)
			t++;
		res += t*a[n-i-1];
	}
	res += t*a[0];
	cout<<res;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define sd(x) scanf("%d",&x);
#define sll(x) scanf("%I64d",&x);

ll a[200000];

int main()
{
	ll n;
	ll t = 0;
	ll res = 0;
	sll(n);
	for (ll i = 0; i < n; ++i)
		sll(a[i]);
	sort(a,a+n);
	for (ll i = 1; i < n; ++i)
	{
		t = 1 + ((i-1)/2);
		res += t*a[n-i];
	}
	res += t*a[0];
	cout<<res;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <list>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define MAX 200000
typedef long long ll;
priority_queue< ll, vector<ll>, greater<ll> > box;
int main(void){
    int n;
    cin >> n;
    for(int i = 0; i < n; ++i){
        ll tmp;
        cin >> tmp;
        box.push(tmp);
    }
    ll sum = 0;
    while(n != 1){
        if(n % 2 == 1){
        	while(n != 1){
            	ll tmpsum = 0;
            	for(int i = 0; i < 3; i++){
                	tmpsum += box.top();
                	box.pop();
            	}
            	sum += tmpsum;
            	n -= 2;
            	box.push(tmpsum);
            }
            break;
        }
        else{
            ll tmpsum = 0;
            for(int i = 0; i < 2; i++){
            	tmpsum += box.top();
                box.pop();
            }
            sum += tmpsum;
            n -= 1;
            box.push(tmpsum);
        }
    }
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <list>
#include <algorithm>
using namespace std;
#define MAX 200000
typedef long long ll;
list<ll>box;
int main(void){
    int n;
    cin >> n;
    for(int i = 0; i < n; ++i){
        ll tmp;
        cin >> tmp;
        box.push_back(tmp);
    }
    box.sort();
    ll sum = 0;
    while(n != 1){
        if(n % 2 == 1){
        	while(n != 1){
            	ll tmpsum = 0;
            	list<ll>::iterator it;
            	it = box.begin();
            	for(int i = 0; i < 3; i++){
                	tmpsum += *it;
                	it++;
            	}
            	for(int i = 0; i < 3; i++){
                	box.pop_front();
            	}
            	sum += tmpsum;
            	n -= 2;
            	it = lower_bound(box.begin(), box.end(), tmpsum);
            	box.insert(it, tmpsum);
            }
            break;
        }
        else{
            ll tmpsum = 0;
            list<ll>::iterator it;
            it = box.begin();
            for(int i = 0; i < 2; i++){
                tmpsum += *it;
                it++;
            }
            for(int i = 0; i < 2; i++){
                box.pop_front();
            }
            sum += tmpsum;
            n -= 1;
            it = lower_bound(box.begin(), box.end(), tmpsum);
            box.insert(it, tmpsum);
        }
    }
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <list>
#include <algorithm>
using namespace std;
#define MAX 200000
typedef long long ll;
list<ll>box;
int main(void){
    int n;
    cin >> n;
    for(int i = 0; i < n; ++i){
        ll tmp;
        cin >> tmp;
        box.push_back(tmp);
    }
    box.sort();
    ll sum = 0;
    while(n != 1){
        if(n % 2 == 1){
            ll tmpsum = 0;
            ll tmp;
            for(int i = 0; i < 3; i++){
                tmp = *box.begin();
                box.pop_front();
                tmpsum += tmp;
            }
            sum += tmpsum;
            n -= 3;
            list<ll>::iterator it;
            it = lower_bound(box.begin(), box.end(), tmpsum);
            box.insert(it, tmpsum);
            n++;
        }
        else{
            ll tmpsum = 0;
            ll tmp;
            for(int i = 0; i < 2; i++){
                tmp = *box.begin();
                box.pop_front();
                tmpsum += tmp;
            }
            sum += tmpsum;
            n -= 2;
            list<ll>::iterator it;
            it = lower_bound(box.begin(), box.end(), tmpsum);
            box.insert(it, tmpsum);
            n++;
        }
    }
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
#define MAX 200000
typedef long long ll;
int box[MAX];
int cmp(const void*v1, const void*v2){
    return *(int*)v1 - *(int*)v2;
}
int main(void){
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) cin >> box[i];

    qsort(box, n, sizeof(int), cmp);
    ll sum = 0;
    int cnt = 1;
    for(int i = n - 1; i > 0; --i){
        cnt++;
        sum += box[i] * (cnt / 2);
    }
    sum += box[0] * (cnt / 2);
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define MAX 200000
typedef long long ll;
int box[MAX];
int cmp(const void*v1, const void*v2){
    return *(int*)v1 - *(int*)v2;
}
int main(void){
    int n;
    cin >> n;
    ll sum = 0;
    for(int i = 0; i < n; ++i) cin >> box[i], sum += box[i];

    qsort(box, n, sizeof(int), cmp);
    int t = (int)(log(n)/log(3));
    sum *= t;
    n -= pow(3,t);
    n = n % 2 == 0 ? n/2*3 : n/2*3 + 2;
    for(int i = 0; i < n; i++) sum += box[i];
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <list>
#include <algorithm>
using namespace std;
#define MAX 200000
typedef long long ll;
list<ll>box;
int main(void){
    int n;
    cin >> n;
    for(int i = 0; i < n; ++i){
        ll tmp;
        cin >> tmp;
        box.push_back(tmp);
    }
    box.sort();
    ll sum = 0;
    while(n != 1){
        if(n % 2 == 1){
            ll tmpsum = 0;
            ll tmp;
            for(int i = 0; i < 3; i++){
                tmp = *box.begin();
                box.pop_front();
                tmpsum += tmp;
            }
            sum += tmpsum;
            n -= 3;
            list<ll>::iterator it;
            it = box.begin();
            int p = 0;
            while(p != n && *it <= tmpsum ){
                it++;
                p++;
            }
            box.insert(it, tmpsum);
            n++;
        }
        else{
            ll tmpsum = 0;
            ll tmp;
            for(int i = 0; i < 2; i++){
                tmp = *box.begin();
                box.pop_front();
                tmpsum += tmp;
            }
            sum += tmpsum;
            n -= 2;
            list<ll>::iterator it;
            it = box.begin();
            int p = 0;
            while(p != n && *it <= tmpsum ){
                it++;
                p++;
            }
            box.insert(it, tmpsum);
            n++;
        }
    }
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <list>
#include <algorithm>
using namespace std;
#define MAX 200000
typedef long long ll;
list<ll>box;
int main(void){
    int n;
    cin >> n;
    for(int i = 0; i < n; ++i){
        ll tmp;
        cin >> tmp;
        box.push_back(tmp);
    }
    box.sort();
    ll sum = 0;
    while(n != 1){
        if(n % 2 == 1){
            ll tmpsum = 0;
            list<ll>::iterator it;
            it = box.begin();
            for(int i = 0; i < 3; i++){
                tmpsum += *it;
                it++;
            }
            for(int i = 0; i < 3; i++){
                box.pop_front();
            }
            sum += tmpsum;
            n -= 2;
            it = lower_bound(box.begin(), box.end(), tmpsum);
            box.insert(it, tmpsum);
        }
        else{
            ll tmpsum = 0;
            list<ll>::iterator it;
            it = box.begin();
            for(int i = 0; i < 2; i++){
                tmpsum += *it;
                it++;
            }
            for(int i = 0; i < 2; i++){
                box.pop_front();
            }
            sum += tmpsum;
            n -= 1;
            it = lower_bound(box.begin(), box.end(), tmpsum);
            box.insert(it, tmpsum);
        }
    }
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <list>
#include <algorithm>
using namespace std;
#define MAX 200000
typedef long long ll;
list<ll>box;
int main(void){
    int n;
    cin >> n;
    for(int i = 0; i < n; ++i){
        ll tmp;
        cin >> tmp;
        box.push_back(tmp);
    }
    box.sort();
    ll sum = 0;
    while(n != 1){
        if(n % 2 == 1){
            ll tmpsum = 0;
            list<ll>::iterator it;
            it = box.begin();
            for(int i = 0; i < 3; i++){
                tmpsum += *it;
                it++;
            }
            for(int i = 0; i < 3; i++){
                box.pop_front();
            }
            sum += tmpsum;
            n -= 3;
            it = lower_bound(box.begin(), box.end(), tmpsum);
            box.insert(it, tmpsum);
            n++;
        }
        else{
            ll tmpsum = 0;
            list<ll>::iterator it;
            it = box.begin();
            for(int i = 0; i < 2; i++){
                tmpsum += *it;
                it++;
            }
            for(int i = 0; i < 2; i++){
                box.pop_front();
            }
            sum += tmpsum;
            n -= 2;
            it = lower_bound(box.begin(), box.end(), tmpsum);
            box.insert(it, tmpsum);
            n++;
        }
    }
    cout << sum << endl;
    return 0;
}


// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
void sleep( double sec = 1021 ){
  clock_t s = clock();
  while( clock() - s < CLOCKS_PER_SEC * sec );
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
void build(){

}
int n;
priority_queue<LL, vector<LL>, greater<LL> >heap;
void init(){
  n = getint();
  for( int i = 0 ; i < n ; i ++ )
    heap.push( getint() );
}
void solve(){
  LL ans = 0;
  while( heap.size() > 1u ){
    LL tmp = 0;
    int tk = 3;
    if( (int)heap.size() % 3 == 1 ) tk = 2;
    for( int i = 0 ; i < tk and not heap.empty() ; i ++ ){
      tmp += heap.top();
      heap.pop();
    }
    ans += tmp;
    heap.push( tmp );
  }
  cout << ans << endl;
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
void sleep( double sec = 1021 ){
  clock_t s = clock();
  while( clock() - s < CLOCKS_PER_SEC * sec );
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
void build(){

}
int n;
priority_queue<LL, vector<LL>, greater<LL> >heap;
void init(){
  n = getint();
  for( int i = 0 ; i < n ; i ++ )
    heap.push( getint() );
}
void solve(){
  LL ans = 0;
  while( heap.size() > 1u ){
    LL tmp = 0;
    int tk = 3;
    if( heap.size() == 4u ) tk = 2;
    for( int i = 0 ; i < tk and not heap.empty() ; i ++ ){
      tmp += heap.top();
      heap.pop();
    }
    ans += tmp;
    heap.push( tmp );
  }
  cout << ans << endl;
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
void sleep( double sec = 1021 ){
  clock_t s = clock();
  while( clock() - s < CLOCKS_PER_SEC * sec );
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
void build(){

}
int n;
priority_queue<LL, vector<LL>, greater<LL> >heap;
void init(){
  n = getint();
  for( int i = 0 ; i < n ; i ++ )
    heap.push( getint() );
}
void solve(){
  LL ans = 0;
  while( heap.size() > 1u ){
    LL tmp = 0;
    int tk = 3;
    if( (int)heap.size() % 2 == 0 ) tk = 2;
    for( int i = 0 ; i < tk and not heap.empty() ; i ++ ){
      tmp += heap.top();
      heap.pop();
    }
    ans += tmp;
    heap.push( tmp );
  }
  cout << ans << endl;
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
void sleep( double sec = 1021 ){
  clock_t s = clock();
  while( clock() - s < CLOCKS_PER_SEC * sec );
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
void build(){

}
int n;
priority_queue<LL, vector<LL>, greater<LL> >heap;
void init(){
  n = getint();
  for( int i = 0 ; i < n ; i ++ )
    heap.push( getint() );
}
void solve(){
  LL ans = 0;
  while( heap.size() > 1u ){
    LL tmp = 0;
    int tk = 3 - ( (int)heap.size() % 3 != 0 );
    for( int i = 0 ; i < tk ; i ++ ){
      tmp += heap.top();
      heap.pop();
    }
    ans += tmp;
    heap.push( tmp );
  }
  cout << ans << endl;
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


#include<cstdio>
#include<algorithm>
using namespace std;
long long v[200001];
int main()
{
//    freopen("f.in","r",stdin);
//    freopen("f.out","w",stdout);
    int n,i;
    long long sum=0,rez=0;
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%I64d",&v[i]);
        sum+=v[i];
    }
    sort(v+1,v+n+1);
    while(n>3)
    {
        rez+=sum;
        sum-=(v[n]+v[n-1]);
        n-=2;
    }
    rez+=sum;
    printf("%I64d\n",rez);
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
long long v[200001];
int main()
{
//    freopen("f.in","r",stdin);
//    freopen("f.out","w",stdout);
    int n,i;
    long long sum=0,rez=0;
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%I64d",&v[i]);
        sum+=v[i];
    }
    sort(v+1,v+n+1);
    while(n>3)
    {
        rez+=sum;
        sum-=(v[n]+v[n-1]);
        n-=3;
    }
    rez+=sum;
    printf("%I64d\n",rez);
    return 0;
}


#include<cstdio>
#include<set>
using namespace std;
multiset<long long>s;
int main()
{
//    freopen("f.in","r",stdin);
//    freopen("f.out","w",stdout);
    int n,i,x;
    long long rez=0,sum;
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d",&x);
        s.insert(1LL*x);
    }
    if(s.size()%2==0)
    {
        sum=0;
        sum+=(*s.begin());
        s.erase(s.begin());
        sum+=(*s.begin());
        s.erase(s.begin());
        rez+=sum;
        s.insert(sum);
    }
    while(s.size()>=3)
    {
        sum=0;
        sum+=(*s.begin());
        s.erase(s.begin());
        sum+=(*s.begin());
        s.erase(s.begin());
        sum+=(*s.begin());
        s.erase(s.begin());
        s.insert(sum);
        rez+=sum;
    }
    printf("%I64d\n",rez);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << a << endl;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 2e5+10;
tint a[MAXN], pre[MAXN];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifdef LOCAL
		freopen("in.txt","r",stdin);
	#endif

	int n;
	cin >> n;

	forn(i, n) cin >> a[i];

	sort(a, a+n);
	forn(i, n)
		pre[i+1] = pre[i] + a[i];

	tint res = 0;

	for (int i = n-1; i >= 0; i -= 2)
		if (i >= 1) res += pre[i+1];

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << a << endl;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 2e5+10;
int n;
tint a[MAXN], pre[MAXN];

tint solve(int l, int r) {
	/*
	cerr << "estoy en ";
	forsn(i, l, r) cerr << a[i] << ' ';
	cerr << endl;
	*/

	if (l == r || l+1 == r) return 0;
	else if (l+2 == r) return a[l] + a[l+1];

	tint sum = pre[r] - pre[l], third = sum/3;

	int i = l;
	//cerr << third << endl;
	//cerr << abs(pre[i+2]-pre[l]-third) << ' ' << abs(pre[i+1]-pre[l]-third) << endl;
	while (i < r-2 && abs(pre[i+1]-pre[l]-third) < abs(pre[i]-pre[l]-third))
		i++;

	int j = i+1;
	while (j < r-1 && abs(pre[j+2]-pre[i+1]-third) < abs(pre[j+1]-pre[i+1]-third))
		j++;

	return sum + solve(l, i) + solve(i, j) + solve(j, r);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifdef LOCAL
		freopen("in.txt","r",stdin);
	#endif

	cin >> n;

	forn(i, n) cin >> a[i];

	sort(a, a+n);

	forn(i, n)
		pre[i+1] = pre[i] + a[i];

	cout << solve(0, n) << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << a << endl;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifdef LOCAL
		freopen("in.txt","r",stdin);
	#endif

	priority_queue<tint,vector<tint>, greater<tint>> pq;

	int n;
	cin >> n;
	forn(i, n) {
		tint a;
		cin >> a;
		pq.push(a);
	}

	if (n%2==0) pq.push(0);

	tint ans = 0;
	while (pq.size() > 1) {
		tint a = 0;

		a += pq.top(); pq.pop();
		a += pq.top(); pq.pop();
		a += pq.top(); pq.pop();

		ans += a;
		pq.push(a);
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << a << endl;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 2e5+10;
int n;
tint a[MAXN], pre[MAXN];

tint solve(int l, int r) {
	/*
	cerr << "estoy en ";
	forsn(i, l, r) cerr << a[i] << ' ';
	cerr << endl;
	*/

	if (l == r || l+1 == r) return 0;
	else if (l+2 == r) return a[l] + a[l+1];

	tint sum = pre[r] - pre[l], third = sum/3;

	int i = l;
	//cerr << third << endl;
	//cerr << abs(pre[i+2]-pre[l]-third) << ' ' << abs(pre[i+1]-pre[l]-third) << endl;
	while (i < r-2 && abs(pre[i+1]-pre[l]-third) < abs(pre[i]-pre[l]-third))
		i++;

	int j = i;
	while (j < r-1 && abs(pre[j+1]-pre[i]-third) < abs(pre[j]-pre[i]-third))
		j++;

	return sum + solve(l, i) + solve(i, j) + solve(j, r);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifdef LOCAL
		freopen("in.txt","r",stdin);
	#endif

	cin >> n;

	forn(i, n) cin >> a[i];

	sort(a, a+n);

	forn(i, n)
		pre[i+1] = pre[i] + a[i];

	cout << solve(0, n) << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n;
int num[200005];
int pow3[8] = {1};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    for(int i = 1;i <= 7;i++) pow3[i] = pow3[i-1] * 3;
    sort(num+1,num+1+n);
    int k = 0;
    for(;pow3[k] < n;) k++;
    int left = pow3[k] - n;
    left /= 2;
    int ans = 0;
    for(int i = 1;i <= n - left;i++) ans += num[i] * k;
    for(int i = n - left + 1;i <= n;i++) ans += num[i] * (k-1);
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
long long num[200005];
vector<priority_queue<long long, vector<long long>, greater<long long> > > v;
struct cmp
{
    bool operator()(long long a,long long b){
        return a>b;
    }
};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    if(n <= 3) {
        long long ans = 0;
        for(int i = 1;i <= n;i++) ans += num[i];
        cout << ans << endl;
        return 0;
    }
    long long ans = 0;
    sort(num+1,num+1+n);
    priority_queue<long long, vector<long long>, greater<long long> > q;
    while(!q.empty()) q.pop();
    for(int i = n;i >= n-2;i--) {
        ans += num[i];q.push(num[i]);
    }
    v.push_back(q);
    for(int i = n-3;i >= 1;i--) {
        int pos = -1;
        long long val = 1000000007;
        for(int dep = 2;dep-2 < v.size();dep++) {
            long long tmp;
            if(dep - 1 < v.size() && v[dep-1].size() % 3 == 2) {
                tmp = dep * num[i];
            } else {
                if(v[dep-2].size() == 0) continue;
                tmp = dep * num[i] + v[dep-2].top();
            }
            if(tmp < val) {
                val = tmp;pos = dep;
            }
        }
        if(pos == -1) while(1);
        ans += val;
        if(pos - 1 < v.size() && v[pos-1].size() % 3 == 2) v[pos-1].push(num[i]);
        else {
            if(pos-1 >= v.size()) {
                priority_queue<long long, vector<long long>, greater<long long> > tmp;
                while(!tmp.empty()) tmp.pop();
                v.push_back(tmp);
            }
            long long tmp = v[pos-2].top();
            v[pos-1].push(tmp);
            v[pos-1].push(num[i]);
            v[pos-2].pop();
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
long long num[200005];
int siz[50];
vector<priority_queue<long long, vector<long long>, greater<long long> > > v;
struct cmp
{
    bool operator()(long long a,long long b){
        return a>b;
    }
};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    if(n <= 3) {
        long long ans = 0;
        for(int i = 1;i <= n;i++) ans += num[i];
        cout << ans << endl;
        return 0;
    }
    long long ans = 0;
    sort(num+1,num+1+n);
    priority_queue<long long, vector<long long>, greater<long long> > q;
    while(!q.empty()) q.pop();
    for(int i = n;i >= n-2;i--) {
        ans += num[i];q.push(num[i]);
    }
    for(int i = 1;i <= 50;i++) siz[i] = 0;
    siz[0] = 3;
    v.push_back(q);
    for(int i = n-3;i >= 1;i--) {
        int pos = -1;
        long long val = 1000000007LL * 500005;
        for(int dep = 2;dep-2 < v.size();dep++) {
            long long tmp;
            if(dep - 1 < v.size() && siz[dep-1] % 3 == 2) {
                tmp = dep * num[i];
            } else {
                if(v[dep-2].size() == 0) continue;
                tmp = dep * num[i] + v[dep-2].top();
            }
            if(tmp < val) {
                val = tmp;pos = dep;
            }
        }
        if(pos == -1) {while(1);}
        ans += val;
        if(pos - 1 < v.size() && siz[pos-1] % 3 == 2) {
            v[pos-1].push(num[i]);
            siz[pos-1] ++;
        }
        else {
            if(pos-1 >= v.size()) {
                priority_queue<long long, vector<long long>, greater<long long> > tmp;
                while(!tmp.empty()) tmp.pop();
                v.push_back(tmp);
            }
            long long tmp = v[pos-2].top();
            v[pos-1].push(tmp);
            v[pos-1].push(num[i]);
            siz[pos-1]+=2;
            v[pos-2].pop();
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
int num[200005];
int pow3[8] = {1};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    for(int i = 1;i <= n;i++) pow3[i] = pow3[i-1] * 3;
    sort(num+1,num+1+n);
    int k = 0;
    for(;pow3[k] < n;) k++;
    int left = pow3[k] - n;
    left /= 2;
    int ans = 0;
    for(int i = 1;i <= n - left;i++) ans += num[i] * k;
    for(int i = n - left + 1;i <= n;i++) ans += num[i] * (k-1);
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
long long num[200005];
vector<priority_queue<long long, vector<long long>, greater<long long> > > v;
struct cmp
{
    bool operator()(long long a,long long b){
        return a>b;
    }
};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    if(n <= 3) {
        long long ans = 0;
        for(int i = 1;i <= n;i++) ans += num[i];
        cout << ans << endl;
        return 0;
    }
    long long ans = 0;
    sort(num+1,num+1+n);
    priority_queue<long long, vector<long long>, greater<long long> > q;
    while(!q.empty()) q.pop();
    for(int i = n;i >= n-2;i--) {
        ans += num[i];q.push(num[i]);
    }
    v.push_back(q);
    for(int i = n-3;i >= 1;i--) {
        int pos = -1;
        long long val = 1000000007;
        for(int dep = 2;dep-2 < v.size();dep++) {
            long long tmp;
            if(dep - 1 < v.size() && v[dep-1].size() % 3 == 2) {
                tmp = dep * num[i];
            } else {
                if(v[dep-2].size() == 0) continue;
                tmp = dep * num[i] + v[dep-2].top();
            }
            if(tmp < val) {
                val = tmp;pos = dep;
            }
        }
        ans += val;
        if(pos - 1 < v.size() && v[pos-1].size() % 3 == 2) v[pos-1].push(num[i]);
        else {
            if(pos-1 >= v.size()) {
                priority_queue<long long, vector<long long>, greater<long long> > tmp;
                while(!tmp.empty()) tmp.pop();
                v.push_back(tmp);
            }
            long long tmp = v[pos-2].top();
            v[pos-1].push(tmp);
            v[pos-1].push(num[i]);
            v[pos-2].pop();
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
priority_queue<long long, vector<long long>, greater<long long> > q;
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) {
        long long x;cin >> x;
        q.push(x);
    }
    if(n % 2 == 0) q.push(0);
    long long ans = 0;
    while(q.size() != 1) {
        long long tmp = 0;
        tmp += q.top();q.pop();
        tmp += q.top();q.pop();
        tmp += q.top();q.pop();
        ans += tmp;
        q.push(tmp);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
int num[200005];
int pow3[20] = {1};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    for(int i = 1;i <= 12;i++) pow3[i] = pow3[i-1] * 3;
    sort(num+1,num+1+n);
    int k = 0;
    for(;pow3[k] < n;) k++;
    int left = pow3[k] - n;
    left /= 2;
    int ans = 0;
    for(int i = 1;i <= n - left;i++) ans += num[i] * k;
    for(int i = n - left + 1;i <= n;i++) ans += num[i] * (k-1);
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
int num[200005];
int pow3[20] = {1};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    for(int i = 1;i <= 12;i++) pow3[i] = pow3[i-1] * 3;
    sort(num+1,num+1+n);
    int k = 0;
    for(;pow3[k] < n;) k++;
    int left = (n - pow3[k-1] + 1) / 2;
    left = n - pow3[k-1] + left;
    int ans = 0;
    for(int i = 1;i <= left;i++) ans += num[i] * k;
    for(int i = left + 1;i <= n;i++) ans += num[i] * (k-1);
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
long long num[200005];
vector<priority_queue<long long, vector<long long>, greater<long long> > > v;
struct cmp
{
    bool operator()(long long a,long long b){
        return a>b;
    }
};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    if(n <= 3) {
        long long ans = 0;
        for(int i = 1;i <= n;i++) ans += num[i];
        cout << ans << endl;
        return 0;
    }
    long long ans = 0;
    sort(num+1,num+1+n);
    priority_queue<long long, vector<long long>, greater<long long> > q;
    while(!q.empty()) q.pop();
    for(int i = n;i >= n-2;i--) {
        ans += num[i];q.push(num[i]);
    }
    v.push_back(q);
    for(int i = n-3;i >= 1;i--) {
        int pos = -1;
        long long val = 1000000007;
        for(int dep = 2;dep-2 < v.size();dep++) {
            long long tmp;
            if(dep - 1 < v.size() && v[dep-1].size() % 3 == 2) {
                tmp = dep * num[i];
            } else {
                tmp = dep * num[i] + v[dep-2].top();
            }
            if(tmp < val) {
                val = tmp;pos = dep;
            }
        }
        ans += val;
        if(pos - 1 < v.size() && v[pos-1].size() % 3 == 2) v[pos-1].push(num[i]);
        else {
            if(pos-1 >= v.size()) {
                priority_queue<long long, vector<long long>, greater<long long> > tmp;
                while(!tmp.empty()) tmp.pop();
                v.push_back(tmp);
            }
            long long tmp = v[pos-2].top();
            v[pos-1].push(tmp);
            v[pos-1].push(num[i]);
            v[pos-2].pop();
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
long long num[200005];
vector<priority_queue<long long, vector<long long>, greater<long long> > > v;
struct cmp
{
    bool operator()(long long a,long long b){
        return a>b;
    }
};
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> num[i];
    if(n <= 3) {
        long long ans = 0;
        for(int i = 1;i <= n;i++) ans += num[i];
        cout << ans << endl;
        return 0;
    }
    long long ans = 0;
    sort(num+1,num+1+n);
    priority_queue<long long, vector<long long>, greater<long long> > q;
    while(!q.empty()) q.pop();
    for(int i = n;i >= n-2;i--) {
        ans += num[i];q.push(num[i]);
    }
    v.push_back(q);
    for(int i = n-3;i >= 1;i--) {
        int pos = -1;
        long long val = 1000000007LL * 500005;
        for(int dep = 2;dep-2 < v.size();dep++) {
            long long tmp;
            if(dep - 1 < v.size() && v[dep-1].size() % 3 == 2) {
                tmp = dep * num[i];
            } else {
                if(v[dep-2].size() == 0) continue;
                tmp = dep * num[i] + v[dep-2].top();
            }
            if(tmp < val) {
                val = tmp;pos = dep;
            }
        }
        if(pos == -1) {while(1);}
        ans += val;
        if(pos - 1 < v.size() && v[pos-1].size() % 3 == 2) v[pos-1].push(num[i]);
        else {
            if(pos-1 >= v.size()) {
                priority_queue<long long, vector<long long>, greater<long long> > tmp;
                while(!tmp.empty()) tmp.pop();
                v.push_back(tmp);
            }
            long long tmp = v[pos-2].top();
            v[pos-1].push(tmp);
            v[pos-1].push(num[i]);
            v[pos-2].pop();
        }
    }
    cout << ans << endl;
}


#include <cstdarg>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long num;

inline void get(num& v) {
    bool f = false; char ch = getchar(); v = 0;
    while(ch < '0') f = (ch == '-'), ch = getchar();
    while(ch >= '0') v = v * 10 + ch - '0', ch = getchar();
    if(f) v = -v;
}
template <typename T>
void print(T t) { cout << t; }
template<typename T, typename... Args>
void print(T t, Args... args) { cout << t; print(args...); }
#define _for(i,s,e) for(i=s;i<e;i++)
#define _fok(i,s,e,k) for(i=s;i<e;i+=k)
//
// gi gi gi gi gi gi gi gi gi gi gi gi
//
const int SZ = 2e5 + 10;
num a[SZ];
num n, i;
int main() {
    #ifdef GIGI
        freopen("in", "r", stdin);
    #endif
    get(n);
    num sum = 0;
    _for(i,0,n) {
        get(a[i]);
        sum += a[i];
    }
    sort(begin(a), begin(a) + n);
    //_for(i,0,n) print(a[i], " "); print("\n");
    num k = 0;
    while(n > 1) {
        k += sum;
        if(n > 3) {
            sum -= a[n - 1] + a[n - 2];
            //print(a[n-1], " ", a[n-2], "\n");
            n -= 3;
        } else {
            sum -= a[n - 1];
            //print(a[n-1], "\n");
            n -= 2;
        }
    }
    print(k, "\n");
}


#include <cstdarg>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long num;

inline void get(num& v) {
    bool f = false; char ch = getchar(); v = 0;
    while(ch < '0') f = (ch == '-'), ch = getchar();
    while(ch >= '0') v = v * 10 + ch - '0', ch = getchar();
    if(f) v = -v;
}
template <typename T>
void print(T t) { cout << t; }
template<typename T, typename... Args>
void print(T t, Args... args) { cout << t; print(args...); }
#define _for(i,s,e) for(i=s;i<e;i++)
#define _fok(i,s,e,k) for(i=s;i<e;i+=k)
//
// gi gi gi gi gi gi gi gi gi gi gi gi
//
const int SZ = 2e5 + 10;
num a[SZ];
num prefsum[SZ];
num n, i, sum = 0;
void suka(num l, num r) {
    num sz = r - l;
    if(sz <= 1) return;
    //print(l, " ", r, " ", prefsum[r] - prefsum[l], "\n");
    sum += prefsum[r] - prefsum[l];
    num szd3 = sz / 3;
    if(sz >= 4) {
        if(szd3 <= 2) {
            suka(l, l + sz / 2);
            suka(l + sz / 2, r - 1);
        } else {
            suka(l, l + szd3);
            suka(l + szd3, l + szd3 * 2);
            suka(l + szd3 * 2, r);
        }
    }
};
int main() {
    #ifdef GIGI
        freopen("in", "r", stdin);
    #endif
    get(n);
    prefsum[0] = a[0];
    _for(i,0,n) {
        get(a[i]);
    }
    sort(begin(a), begin(a) + n);
    _for(i,0,n) prefsum[i+1] = prefsum[i] + a[i];
    suka(0, n);
    print(sum, "\n");
    //_for(i,0,n) print(prefsum[i], " "); print("\n");
    /*num k = 0, i0 = 0, in = n-1;
    num suka = log(n) / log(3);
    num d1 = n - n % (num)pow(3, suka);
    num d2 = n - d1;
    print(d1, " ", d2, "\n");*/
}


#include <cstdarg>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long num;

inline void get(num& v) {
    bool f = false; char ch = getchar(); v = 0;
    while(ch < '0') f = (ch == '-'), ch = getchar();
    while(ch >= '0') v = v * 10 + ch - '0', ch = getchar();
    if(f) v = -v;
}
template <typename T>
void print(T t) { cout << t; }
template<typename T, typename... Args>
void print(T t, Args... args) { cout << t; print(args...); }
#define _for(i,s,e) for(i=s;i<e;i++)
#define _fok(i,s,e,k) for(i=s;i<e;i+=k)
//
// gi gi gi gi gi gi gi gi gi gi gi gi
//
const int SZ = 2e5 + 10;
multiset<num> a;
num n, i, v, sum = 0;

int main() {
    #ifdef GIGI
        freopen("in", "r", stdin);
    #endif
    get(n);
    _for(i,0,n) {
        get(v);
        a.insert(v);
    }
    if(n % 2 == 0) a.insert(0);
    while(a.size() > 1) {
        //for(auto vv:a) print(vv, " "); print("\n");
        v = 0;
        for(num i = 0; i < 3 && a.size(); i++) {
            num buf = *a.begin();
            v += buf; a.erase(a.begin());
        }
        a.insert(v);
        sum += v;
    }
    print(sum, "\n");
}


#include <cstdarg>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long num;

inline void get(num& v) {
    bool f = false; char ch = getchar(); v = 0;
    while(ch < '0') f = (ch == '-'), ch = getchar();
    while(ch >= '0') v = v * 10 + ch - '0', ch = getchar();
    if(f) v = -v;
}
template <typename T>
void print(T t) { cout << t; }
template<typename T, typename... Args>
void print(T t, Args... args) { cout << t; print(args...); }
#define _for(i,s,e) for(i=s;i<e;i++)
#define _fok(i,s,e,k) for(i=s;i<e;i+=k)
//
// gi gi gi gi gi gi gi gi gi gi gi gi
//
const int SZ = 2e5 + 10;
num a[SZ];
num prefsum[SZ];
num n, i, sum = 0;
void suka(num l, num r) {
    num sz = r - l;
    if(sz <= 1) return;
    //print(l, " ", r, " ", prefsum[r] - prefsum[l], "\n");
    sum += prefsum[r] - prefsum[l];
    num szd3 = ceil((double)sz / 3);
    if(sz >= 4) {
        if(szd3 <= 2) {
            suka(l, l + sz / 2);
            suka(l + sz / 2, r - 1);
        } else {
            suka(l, l + szd3);
            suka(l + szd3, l + szd3 * 2);
            suka(l + szd3 * 2, r);
        }
    }
};
int main() {
    #ifdef GIGI
        freopen("in", "r", stdin);
    #endif
    get(n);
    prefsum[0] = a[0];
    _for(i,0,n) {
        get(a[i]);
        prefsum[i+1] = prefsum[i] + a[i];
    }
    sort(begin(a), begin(a) + n);
    suka(0, n);
    print(sum, "\n");
    //_for(i,0,n) print(prefsum[i], " "); print("\n");
    /*num k = 0, i0 = 0, in = n-1;
    num suka = log(n) / log(3);
    num d1 = n - n % (num)pow(3, suka);
    num d2 = n - d1;
    print(d1, " ", d2, "\n");*/
}


#include <cstdarg>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long num;

inline void get(num& v) {
    bool f = false; char ch = getchar(); v = 0;
    while(ch < '0') f = (ch == '-'), ch = getchar();
    while(ch >= '0') v = v * 10 + ch - '0', ch = getchar();
    if(f) v = -v;
}
template <typename T>
void print(T t) { cout << t; }
template<typename T, typename... Args>
void print(T t, Args... args) { cout << t; print(args...); }
#define _for(i,s,e) for(i=s;i<e;i++)
#define _fok(i,s,e,k) for(i=s;i<e;i+=k)
//
// gi gi gi gi gi gi gi gi gi gi gi gi
//
const int SZ = 2e5 + 10;
num a[SZ];
num prefsum[SZ];
num n, i, sum = 0;
void suka(num l, num r) {
    num sz = r - l;
    if(sz <= 1) return;
    //print(l, " ", r, " ", prefsum[r] - prefsum[l], "\n");
    sum += prefsum[r] - prefsum[l];
    num szd3 = sz / 3;
    if(sz >= 4) {
        if(szd3 <= 2) {
            if(sz > 6) {
                sz -= 3;
                suka(l, l + sz / 2);
                suka(l + sz / 2, r - 3);
            } else {
                suka(l, l + sz / 2);
                suka(l + sz / 2, r - 1);
            }
        } else {
            suka(l, l + szd3);
            suka(l + szd3, l + szd3 * 2);
            suka(l + szd3 * 2, r);
        }
    }
};
int main() {
    #ifdef GIGI
        freopen("in", "r", stdin);
    #endif
    get(n);
    prefsum[0] = a[0];
    _for(i,0,n) {
        get(a[i]);
        prefsum[i+1] = prefsum[i] + a[i];
    }
    sort(begin(a), begin(a) + n);
    suka(0, n);
    print(sum, "\n");
    //_for(i,0,n) print(prefsum[i], " "); print("\n");
    /*num k = 0, i0 = 0, in = n-1;
    num suka = log(n) / log(3);
    num d1 = n - n % (num)pow(3, suka);
    num d2 = n - d1;
    print(d1, " ", d2, "\n");*/
}


#include <cstdarg>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long num;

inline void get(num& v) {
    bool f = false; char ch = getchar(); v = 0;
    while(ch < '0') f = (ch == '-'), ch = getchar();
    while(ch >= '0') v = v * 10 + ch - '0', ch = getchar();
    if(f) v = -v;
}
template <typename T>
void print(T t) { cout << t; }
template<typename T, typename... Args>
void print(T t, Args... args) { cout << t; print(args...); }
#define _for(i,s,e) for(i=s;i<e;i++)
#define _fok(i,s,e,k) for(i=s;i<e;i+=k)
//
// gi gi gi gi gi gi gi gi gi gi gi gi
//
const int SZ = 2e5 + 10;
num a[SZ];
num prefsum[SZ];
num n, i, sum = 0;
void suka(num l, num r) {
    num k;
    while(l + 2 < r) {
        num sz = r - l;
        k = sz == 4? 2: 3;
        //print(l, " ", r, " ", prefsum[r] - prefsum[l], " ", prefsum[l+k] - prefsum[l], "\n");
        if(sz <= 1) return;
        if(l < r) sum += prefsum[r] - prefsum[l];
        if(l + k < r) sum += prefsum[l+k] - prefsum[l];
        l += k;
        r -= 1;
    }
    if(l + 1 < r) sum += prefsum[r] - prefsum[l];
    //print(l, " ", r, "\n");

};
int main() {
    #ifdef GIGI
        freopen("in", "r", stdin);
    #endif
    get(n);
    prefsum[0] = a[0];
    _for(i,0,n) {
        get(a[i]);
    }
    //if(n % 2 == 0) a[n] = 0;
    sort(begin(a), begin(a) + n);
    _for(i,0,n) prefsum[i+1] = prefsum[i] + a[i];
    suka(0, n);
    print(sum, "\n");
    //_for(i,0,n) print(prefsum[i], " "); print("\n");
    /*num k = 0, i0 = 0, in = n-1;
    num suka = log(n) / log(3);
    num d1 = n - n % (num)pow(3, suka);
    num d2 = n - d1;
    print(d1, " ", d2, "\n");*/
}