#include <bits/stdc++.h>

using namespace std;

long long noColors ;

priority_queue < long long ,  vector < long long > > allBile ;

long long sol ;

int main(){

    scanf("%d",&noColors );

    if ( noColors %2 == 0 ){
        allBile.push( 0 );
    }

    for ( long long i = 0 ; i < noColors ; i++ ){
        long long x ;
        scanf("%d",&x);

        allBile.push( -x );

    }

    while ( allBile.size() >= 3 ){
        long long val1 = -allBile.top() ;
        allBile.pop();
        long long val2 = -allBile.top() ;
        allBile.pop();
        long long val3 = -allBile.top() ;
        allBile.pop();

        val1 += val2 + val3 ;

        sol += val1 ;

        allBile.push( -val1 );

    }

    if ( allBile.size() != 1 ){
        while ( !allBile.empty() ){

            sol += allBile.top() ;
            allBile.pop() ;

        }
    }

    printf("%I64d",sol);

    return 0;
}


/*
PROG : milk2
LANG : C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define se second
#define sqr(a) ((a) * (a))
#define pi 3.1415926535897932
#define read ifstream in("input.txt")
#define write ofstream out("output.txt")
#define vii vector<int>
#define mii map<int, int>

using namespace std;

int n, m, k;

int main()
{
    //ifstream in("moocast.in");
    //ofstream out("moocast.out");
    cin >> n;
    multiset<ll> s;
    ll sum = 0;
    FOR(i, 0, n){
        int a;
        cin >> a;
        s.insert(a);
        sum += a;
    }

    ll ans = 0;
    while(s.size() > 1){
        if (s.size() % 2 == 0){
            s.insert(0);
            continue;
        }
        ll a = *s.begin(), b = 0, c = 0;
        s.erase(s.begin());
        if (!s.empty())
        b = *s.begin(), s.erase(s.begin());
        if (!s.empty())
        c = *s.begin(), s.erase(s.begin());
        ans += a + b + c;
        s.insert(a + b + c);
    }

    cout << ans << endl;

    return 0;
    ///dont forget about initialization array please
}


/*
PROG : milk2
LANG : C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define se second
#define sqr(a) ((a) * (a))
#define pi 3.1415926535897932
#define read ifstream in("input.txt")
#define write ofstream out("output.txt")
#define vii vector<int>
#define mii map<int, int>

using namespace std;

int n, m, k;

int main()
{
    //ifstream in("moocast.in");
    //ofstream out("moocast.out");
    cin >> n;
    set<int> s;
    ll sum = 0;
    FOR(i, 0, n){
        int a;
        cin >> a;
        s.insert(-a);
        sum += a;
    }

    ll ans = 0;
    while(!s.empty()){
        int a = -*s.begin(), b = 0, c = 0;
        s.erase(s.begin());
        if (!s.empty())
            b = -*s.begin(), s.erase(s.begin());
        if (!s.empty())
            c = -*s.begin(), s.erase(s.begin());
        ans += sum;
        sum -= (a + b);
        if (!s.empty() && c)
            s.insert(-c);
    }

    cout << ans << endl;

    return 0;
    ///dont forget about initialization array please
}


/*
PROG : milk2
LANG : C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define se second
#define sqr(a) ((a) * (a))
#define pi 3.1415926535897932
#define read ifstream in("input.txt")
#define write ofstream out("output.txt")
#define vii vector<int>
#define mii map<int, int>

using namespace std;

int n, m, k;

int main()
{
    //ifstream in("moocast.in");
    //ofstream out("moocast.out");
    cin >> n;
    multiset<int> s;
    ll sum = 0;
    FOR(i, 0, n){
        int a;
        cin >> a;
        s.insert(-a);
        sum += a;
    }

    ll ans = 0;
    while(!s.empty()){
        int a = -*s.begin(), b = 0, c = 0;
        s.erase(s.begin());
        if (!s.empty())
            b = -*s.begin(), s.erase(s.begin());
        if (!s.empty())
            c = -*s.begin(), s.erase(s.begin());
        ans += sum;
        sum -= (a + b);
    }

    cout << ans << endl;

    return 0;
    ///dont forget about initialization array please
}


/*
PROG : milk2
LANG : C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define se second
#define sqr(a) ((a) * (a))
#define pi 3.1415926535897932
#define read ifstream in("input.txt")
#define write ofstream out("output.txt")
#define vii vector<int>
#define mii map<int, int>

using namespace std;

int n, m, k;

int main()
{
    //ifstream in("moocast.in");
    //ofstream out("moocast.out");
    cin >> n;
    multiset<int> s;
    ll sum = 0;
    FOR(i, 0, n){
        int a;
        cin >> a;
        s.insert(-a);
        sum += a;
    }

    ll ans = 0;
    while(!s.empty()){
        int a = -*s.begin(), b = 0, c = 0;
        s.erase(s.begin());
        if (!s.empty())
            b = -*s.begin(), s.erase(s.begin());
        if (!s.empty())
            c = -*s.begin(), s.erase(s.begin());
        ans += sum;
        sum -= (a + b);
        if (!s.empty())
            s.insert(-c);
    }

    cout << ans << endl;

    return 0;
    ///dont forget about initialization array please
}


/*
PROG : milk2
LANG : C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define se second
#define sqr(a) ((a) * (a))
#define pi 3.1415926535897932
#define read ifstream in("input.txt")
#define write ofstream out("output.txt")
#define vii vector<int>
#define mii map<int, int>

using namespace std;

int n, m, k;

int main()
{
    //ifstream in("moocast.in");
    //ofstream out("moocast.out");
    cin >> n;
    set<int> s;
    ll sum = 0;
    FOR(i, 0, n){
        int a;
        cin >> a;
        s.insert(-a);
        sum += a;
    }

    ll ans = 0;
    while(!s.empty()){
        int a = -*s.begin(), b = 0, c = 0;
        s.erase(s.begin());
        if (!s.empty())
            b = -*s.begin(), s.erase(s.begin());
        if (!s.empty())
            c = -*s.begin(), s.erase(s.begin());
        ans += sum;
        sum -= (a + b);
        if (!s.empty())
            s.insert(-c);
    }

    cout << ans << endl;

    return 0;
    ///dont forget about initialization array please
}


/*
PROG : milk2
LANG : C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define se second
#define sqr(a) ((a) * (a))
#define pi 3.1415926535897932
#define read ifstream in("input.txt")
#define write ofstream out("output.txt")
#define vii vector<int>
#define mii map<int, int>

using namespace std;

int n, m, k;

int main()
{
    //ifstream in("moocast.in");
    //ofstream out("moocast.out");
    cin >> n;
    set<int> s;
    ll sum = 0;
    FOR(i, 0, n){
        int a;
        cin >> a;
        s.insert(-a);
        sum += a;
    }

    ll ans = 0;
    while(!s.empty()){
        int a = -*s.begin(), b = 0, c = 0;
        s.erase(s.begin());
        if (!s.empty())
            b = -*s.begin(), s.erase(s.begin());
        if (!s.empty())
            c = -*s.begin(), s.erase(s.begin());
        ans += sum;
        sum -= (a + b);
    }

    cout << ans << endl;

    return 0;
    ///dont forget about initialization array please
}


/*
PROG : milk2
LANG : C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define se second
#define sqr(a) ((a) * (a))
#define pi 3.1415926535897932
#define read ifstream in("input.txt")
#define write ofstream out("output.txt")
#define vii vector<int>
#define mii map<int, int>

using namespace std;

int n, m, k;

int main()
{
    //ifstream in("moocast.in");
    //ofstream out("moocast.out");
    cin >> n;
    multiset<int> s;
    ll sum = 0;
    FOR(i, 0, n){
        int a;
        cin >> a;
        s.insert(a);
        sum += a;
    }

    ll ans = 0;
    while(s.size() > 1){
        if (s.size() % 2 == 0){
            s.insert(0);
            continue;
        }
        int a = *s.begin(), b = 0, c = 0;
        s.erase(s.begin());
        if (!s.empty())
        b = *s.begin(), s.erase(s.begin());
        if (!s.empty())
        c = *s.begin(), s.erase(s.begin());
        ans += a + b + c;
        s.insert(a + b + c);
    }

    cout << ans << endl;

    return 0;
    ///dont forget about initialization array please
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long int;
using ii = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
using graph = vector<vi>;
using matrix = graph;
const int MAXN = 2e5+5;;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll v[MAXN], sum[MAXN];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i<=n; ++i) {
        scanf("%lld", v+i);
    }
    sort(v+1, v+n+1);
    for (int i = 1; i<=n; ++i) {
        sum[i] = v[i] + sum[i-1];
    }
    ll ans = 0;
    for (int i = n; i>1; i-=2) {
        ans += sum[i];
    }
    printf("%lld\n", ans);
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = unsigned long long int;
using ii = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
using graph = vector<vi>;
using matrix = graph;
const int MAXN = 2e5+5;;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll v[MAXN], sum[MAXN];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i<=n; ++i) {
        scanf("%llu", v+i);
    }
    sort(v+1, v+n+1);
    for (int i = 1; i<=n; ++i) {
        sum[i] = v[i] + sum[i-1];
    }
    ll ans = 0;
    for (int i = n; i>1; i-=2) {
        ans += sum[i];
    }
    printf("%llu\n", ans);
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long int;
using ii = pair<int,int>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
using graph = vector<vi>;
using matrix = graph;
const int MAXN = 2e5+5;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

int main() {
    int n;
    scanf("%d", &n);
    priority_queue<ll> pq;
    ll ans = 0;
    int x;
    for (int i = 0; i<n; ++i) {
        scanf("%d", &x);
        pq.push(-x);
    }
    if (~n&1) pq.push(0ll);
    ll a, b, c;
    while (pq.size()>1) {
        a = pq.top(); pq.pop();
        b = pq.top(); pq.pop();
        c = pq.top(); pq.pop();
        a += b + c;
        ans += a;
        pq.push(a);
    }
    printf("%lld\n", -ans);
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = unsigned long long int;
using ii = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
using graph = vector<vi>;
using matrix = graph;
const int MAXN = 2e5+5;;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll sum[MAXN], dp[MAXN];
int v[MAXN];
bool seen[MAXN];
ll sol(int i) {
    if (i <= 1) return 0;
    if (seen[i]) return dp[i];
    seen[i] = 1;
    return dp[i] = sum[i] + min(sol(i-2), sol(i-1));
}
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i<=n; ++i) {
        scanf("%d", v+i);
    }
    sort(v+1, v+n+1);
    int cur = 0;
    for (int i = 1; i<=n; ++i) {
        sum[i] = sum[i-1] + v[i];
    }
    printf("%llu\n", sol(n));
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = unsigned long long int;
using ii = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
using graph = vector<vi>;
using matrix = graph;
const int MAXN = 2e5+5;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll sum[MAXN], dp[MAXN];
ll v[MAXN];
bool seen[MAXN];
ll sol(int i) {
    if (i <= 1) return 0;
    if (seen[i]) return dp[i];
    seen[i] = 1;
    return dp[i] = sum[i] + min(sol(i-2), sol(i-1));
}
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i<=n; ++i) {
        scanf("%llu", v+i);
    }
    sort(v+1, v+n+1);
    for (int i = 1; i<=n; ++i) {
        sum[i] = sum[i-1] + v[i];
    }
    printf("%llu\n", sol(n));
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
priority_queue<ll,vector<ll>,greater<ll> > q;

int main(){
	while(!q.empty())q.pop();
	int n;
	cin>>n;
	int x;
	for(int i=0;i<n;i++){
		cin>>x;
		q.push(x);
	}
	if(n==1)cout<<0<<endl;
	else{
		ll wpl=0;
		if(n%2==0)q.push(0);
		while(q.size()>2){
			ll min1=q.top();
			q.pop();
			ll min2=q.top();
			q.pop();
			ll min3=q.top();
			q.pop();
			ll temp=min1+min2+min3;
			wpl+=temp;
			q.push(temp);
		}
		cout<<wpl<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		q.push(x);
	}
	int wpl=0;
	if(n==1)wpl=q.top();
	if(n%2==0)q.push(0);
	while(q.size()>2){
		int min1=q.top();
		q.pop();
		int min2=q.top();
		q.pop();
		int min3=q.top();
		q.pop();
		int temp=min1+min2+min3;
		wpl+=temp;
		q.push(temp);
	}
	cout<<wpl<<endl;
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;

int main(){
	while(!q.empty())q.pop();
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		q.push(x);
	}
	ll wpl=0;
	if(n%2==0)q.push(0);
	while(q.size()>2){
		int min1=q.top();
		q.pop();
		int min2=q.top();
		q.pop();
		int min3=q.top();
		q.pop();
		int temp=min1+min2+min3;
		wpl+=temp;
		q.push(temp);
	}
	cout<<wpl<<endl;
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;

int main(){
	while(!q.empty())q.pop();
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		q.push(x);
	}
	if(n==1)cout<<0<<endl;
	else{
		ll wpl=0;
		if(n%2==0)q.push(0);
		while(q.size()>2){
			int min1=q.top();
			q.pop();
			int min2=q.top();
			q.pop();
			int min3=q.top();
			q.pop();
			int temp=min1+min2+min3;
			wpl+=temp;
			q.push(temp);
		}
	cout<<wpl<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
priority_queue<ll,vector<ll>,greater<ll> > q;

int main(){
	while(!q.empty())q.pop();
	int n;
	cin>>n;
	int x;
	for(int i=0;i<n;i++){
		cin>>x;
		q.push(x);
	}
	if(n==1)cout<<0<<endl;
	else{
		ll wpl=0;
		if(n%2==0)q.push(0);
		while(q.size()>2){
			int min1=q.top();
			q.pop();
			int min2=q.top();
			q.pop();
			int min3=q.top();
			q.pop();
			int temp=min1+min2+min3;
			wpl+=temp;
			q.push(temp);
		}
		cout<<wpl<<endl;
	}

	return 0;
}


/*
题意：在1号盒子里有n中颜色的球，每种颜色ai个，要将第i种颜色的球放进第i个盒子里。每次只能将一个盒子中的球全部拿出，分成2份或3份，
      惩罚的代价是这个盒子中球的总数，问分好球的总惩罚代价最小是多少？
思路：将题目反过来想，如果每次都分成2份，其实就是选取两个盒子中的球加起来，怎样使累加的和最小。即每次选取球最少的两个盒子，用构造哈夫曼树
      的方式求出的总惩罚代价最小。但也可以分成3份，那就构造一个三叉哈夫曼树，但存在一个问题，可能因为结点个数不够而存在二叉的结点。
      构造二叉哈夫曼树时，每次合并后，小根堆里的结点个数减少了1个，当小根堆中只剩1个结点时即为根结点。
	  构造三叉哈夫曼树时，每次合并后，小根堆里的结点个数减少了2个，当小根堆中只剩1个结点时即为根结点。若小根堆中只剩2个结点，
	  则只能构成二叉，还差1个结点构成三叉，所以加个0将叶子结点变为奇数个就可以把存在的一个二叉变三叉了。
*/
#include <stdio.h>
#include <queue>
using namespace std;
typedef long long ll;
priority_queue<ll,vector<ll>,greater<ll> > q;

int main(){
	while(!q.empty()) q.pop();//清空队列
	int n,x;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&x);
		q.push(x);
	}
	if(n==1) printf("0\n");//当n为1时，不需要分球
	else{
		ll wpl=0;
		if(n%2==0) q.push(0);//当n为2或为2的倍数时，加入一个0使n为奇数
		while(q.size()>2){//三叉哈夫曼树与二叉哈夫曼树相似
			ll min1=q.top();
			q.pop();
			ll min2=q.top();
			q.pop();
			ll min3=q.top();
			q.pop();
			wpl+=min1+min2+min3;
			q.push(min1+min2+min3);
		}
		printf("%lld",wpl);
	}
	return 0;
}


// Problem : D. Boxes And Balls
// Contest : Codeforces - Educational Codeforces Round 31
// URL : https://codeforces.com/contest/884/problem/D
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		q.push(x);
	}
	int wpl=0;
	if(n==1)cout<<q.top()<<endl;
	if(n%2==0)q.push(0);
	while(q.size()>2){
		int min1=q.top();
		q.pop();
		int min2=q.top();
		q.pop();
		int min3=q.top();
		q.pop();
		int temp=min1+min2+min3;
		wpl+=temp;
		q.push(temp);
	}
	cout<<wpl<<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;

int n;
int seq[200050];
long long ans;

int main()
{
	scanf("%d",&n);
	long long cur = 0;
	priority_queue<long long,vector<long long>,greater<long long> > que;
	for(int i = 1;i <= n; ++ i)
		scanf("%d",&seq[i]),
		que.push(seq[i]);
	if(n % 2 == 0)
	{
		int u = que.top();
		que.pop();
		int v = que.top();
		que.pop();
		que.push(1ll * u + v);
		ans += u + v;
	}
	while(que.size() > 1)
	{
		long long tmp = 0;
		for(int i = 1;i <= 3; ++ i)
		{
			long long u = que.top(); que.pop();
			tmp += u;
		}
		que.push(tmp);
		ans += tmp;
	}
	printf("%lld\n",ans);
	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 "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 (n% 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 "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 (n% 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
#define int ll
using namespace std;
const int N=2e5+5;
int n,x;ll ans;
priority_queue<ll>q;
bool f;
signed 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<bits/stdc++.h>
#define MAXN 200005
using namespace std;
typedef long long ll;
int n;
ll sum,ans;
priority_queue<int> que;
int main()
{
    scanf("%d",&n);
    sum=0,ans=0;
    for(int i=0;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        sum+=x;
        que.push(x);
    }
    while(que.size()>3)
    {
        ans+=sum;
        sum-=que.top();
        que.pop();
        sum-=que.top();
        que.pop();
    }
    ans+=sum;
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define MAXN 200305
using namespace std;
typedef long long ll;
ll n;
ll ans,sum;
priority_queue<ll, vector<ll>, greater<ll> > que;
int main()
{
    scanf("%I64d",&n);
    if(n==1)
    {
        printf("0\n");
        return 0;
    }
    ans=0;
    for(ll i=0;i<n;i++)
    {
        ll x;
        scanf("%I64d",&x);
        que.push(x);
    }
    if(que.size()%2==0&&n>3)
    {
        ans+=que.top();
        que.pop();
        ans+=que.top();
        que.pop();
        que.push(ans);
    }
    while(que.size()>3)
    {
        ll x=0;
        x+=que.top();
        que.pop();
        x+=que.top();
        que.pop();
        x+=que.top();
        que.pop();
        ans+=x;
        que.push(x);
    }
    while(que.size())
    {
        ans+=que.top();
        que.pop();
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define MAXN 200305
using namespace std;
typedef long long ll;
ll n;
ll ans,sum;
priority_queue<ll, vector<ll>, greater<ll> > que;
int main()
{
    scanf("%I64d",&n);
    ans=0;
    for(ll i=0;i<n;i++)
    {
        ll x;
        scanf("%I64d",&x);
        que.push(x);
    }
    if(que.size()%2==0)
    {
        ans+=que.top();
        que.pop();
        ans+=que.top();
        que.pop();
        que.push(ans);
    }
    while(que.size()>3)
    {
        ll x=0;
        x+=que.top();
        que.pop();
        x+=que.top();
        que.pop();
        x+=que.top();
        que.pop();
        ans+=x;
        que.push(x);
    }
    while(que.size())
    {
        ans+=que.top();
        que.pop();
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;
typedef long long ll;
int n;
ll ans,sum;
priority_queue<int, vector<int>, greater<int> > que;
int main()
{
    scanf("%d",&n);
    ans=0;
    for(int i=0;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        que.push(x);
    }
    if(que.size()%2==0)
    {
        ans+=que.top();
        que.pop();
        ans+=que.top();
        que.pop();
        que.push(ans);
    }
    while(que.size()>3)
    {
        ll x=0;
        x+=que.top();
        que.pop();
        x+=que.top();
        que.pop();
        x+=que.top();
        que.pop();
        ans+=x;
        que.push(x);
    }
    while(que.size())
    {
        ans+=que.top();
        que.pop();
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define MAXN 200305
using namespace std;
typedef long long ll;
ll n;
ll ans,sum;
priority_queue<ll, vector<ll>, greater<ll> > que;
int main()
{
    scanf("%I64d",&n);
    if(n==1)
    {
        printf("0\n");
        return 0;
    }
    ans=0;
    for(ll i=0;i<n;i++)
    {
        ll x;
        scanf("%I64d",&x);
        que.push(x);
    }
    if(que.size()%2==0)
    {
        ans+=que.top();
        que.pop();
        ans+=que.top();
        que.pop();
        que.push(ans);
    }
    while(que.size()>3)
    {
        ll x=0;
        x+=que.top();
        que.pop();
        x+=que.top();
        que.pop();
        x+=que.top();
        que.pop();
        ans+=x;
        que.push(x);
    }
    while(que.size())
    {
        ans+=que.top();
        que.pop();
    }
    printf("%I64d\n",ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>

#define lop(num)      for (int _cnt_ = 0; _cnt_ < num; _cnt_++)
#define loop(i, num)  for (int i = 0; i < num; i++)
#define pool(i, num)  for (int i = num - 1; i >= 0; i--)

using namespace std;

int n, mini = 1e9;

long long sum, ans;

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

int main() {

    cin >> n;

    if ((n & 1) == 0) {

        pq.push(0);

    }

    loop (i, n) {

        int num;

        cin >> num;

        pq.push(num);

    }

    while (pq.size() != 1) {

        lop (3) {

            sum += pq.top();

            pq.pop();

        }

        pq.push(sum);

        ans += sum;

    }

    cout << ans << endl;

}

// 13 4 4
// 5 4 4
// 1 4

// 5 4 5
// 2 3


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>

#define loop(i, num)  for (int i = 0; i < num; i++)
#define pool(i, num)  for (int i = num - 1; i >= 0; i--)

using namespace std;

int n, mini = 1e9;

long long sum, ans;

priority_queue<int> pq;

int main() {

    cin >> n;

    loop (i, n) {

        int num;

        cin >> num;

        sum += num;

        mini = min(mini, num);

        pq.push(num);

    }

    while (!pq.empty()) {

        if (sum == mini)  break;

        ans += pq.top();

        sum -= pq.top();

        pq.pop();

        if (pq.empty() || sum == mini)  break;

        ans += pq.top();

        sum -= pq.top();

        pq.pop();

        ans += sum;

    }

    cout << ans << endl;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>

#define loop(i, num)  for (int i = 0; i < num; i++)
#define pool(i, num)  for (int i = num - 1; i >= 0; i--)

using namespace std;

int n, mini = 1e9;

long long sum, ans;

priority_queue<int> pq;

int main() {

    cin >> n;

    loop (i, n) {

        int num;

        cin >> num;

        sum += num;

        mini = min(mini, num);

        pq.push(num);

    }

    while (!pq.empty()) {

        if (sum == mini)  break;

        ans += pq.top();

        sum -= pq.top();

        pq.pop();

        if (pq.empty())  break;

        ans += pq.top();

        sum -= pq.top();

        pq.pop();

        ans += sum;

    }

    cout << ans << endl;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>

#define lop(num)      for (int _cnt_ = 0; _cnt_ < num; _cnt_++)
#define loop(i, num)  for (int i = 0; i < num; i++)
#define pool(i, num)  for (int i = num - 1; i >= 0; i--)

using namespace std;

int n, mini = 1e9;

long long sum, ans;

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

int main() {

    cin >> n;

    if ((n & 1) == 0) {

        pq.push(0);

    }

    loop (i, n) {

        int num;

        cin >> num;

        pq.push(num);

    }

    while (pq.size() != 1) {

        sum = 0;

        lop (3) {

            sum += pq.top();

            pq.pop();

        }

        pq.push(sum);

        ans += sum;

    }

    cout << ans << endl;

}

// 13 4 4
// 5 4 4
// 1 4

// 5 4 5
// 2 3


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>

#define lop(num)      for (int _cnt_ = 0; _cnt_ < num; _cnt_++)
#define loop(i, num)  for (int i = 0; i < num; i++)
#define pool(i, num)  for (int i = num - 1; i >= 0; i--)

using namespace std;

int n, mini = 1e9;

long long sum, ans;

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

int main() {

    cin >> n;

    if ((n & 1) == 0) {

        pq.push(0);

    }

    loop (i, n) {

        int num;

        cin >> num;

        pq.push(num);

    }

    while (pq.size() != 1) {

        sum = 0;

        lop (3) {

            sum += pq.top();

            pq.pop();

        }

        pq.push(sum);

        ans += sum;

    }

    cout << ans << endl;

}

// 13 4 4
// 5 4 4
// 1 4

// 5 4 5
// 2 3


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>

#define lop(num)      for (int _cnt_ = 0; _cnt_ < num; _cnt_++)
#define loop(i, num)  for (int i = 0; i < num; i++)
//#define looop
#define pool(i, num)  for (int i = num - 1; i >= 0; i--)

using namespace std;

int n, mini = 1e9;

long long sum, ans;

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

int main() {

    cin >> n;

    if ((n & 1) == 0) {

        pq.push(0);

    }

    loop (i, n) {

        int num;

        cin >> num;

        pq.push(num);

    }

    while (pq.size() != 1) {

        int sum = 0;

        lop (3) {

            sum += pq.top();

            pq.pop();

        }

        pq.push(sum);

        ans += sum;

    }

    cout << ans << endl;

}

// 13 4 4
// 5 4 4
// 1 4

// 5 4 5
// 2 3


/******************************************
*    AUTHOR:         CHIRAG AGARWAL       *
*    INSTITUITION:   BITS PILANI, PILANI  *
******************************************/
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
const int MAX=2e5+5;
LL arr[MAX];
LL pre[MAX];

LL solve(int l,int r)
{
	if(l==r)
	{
		return 0ll;
	}
	if((l+1)==r)
	{
		return (pre[r]-pre[l-1]);
	}
	else
	{
		return (pre[r]-pre[l-1]+solve(l,r-2));
	}
}

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


/******************************************
*    AUTHOR:         CHIRAG AGARWAL       *
*    INSTITUITION:   BITS PILANI, PILANI  *
******************************************/
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
const int MAX=2e5+5;
LL arr[MAX];
LL pre[MAX];

LL solve(int l,int r)
{
	if(l>r)
	{
		return 0;
	}
	if(l==r)
	{
		return 0ll;
	}
	if((l+1)==r)
	{
		return (pre[r]-pre[l-1]);
	}
	else
	{
		LL ans=pre[r]-pre[l-1];
		LL lft=ans;
		LL val=0;
		int i;
		LL sum=arr[r];
		for(i=r-1;i>=l;i--)
		{
			if((sum+arr[i])>(lft/3))
			{

				break;
			}
			sum+=arr[i];
		}
		val+=solve(i+1,r);
		lft-=(pre[r]-pre[i]);
		sum=arr[i];
		int j;
		for(j=i-1;j>=l;j--)
		{
			if((sum+arr[j])>(lft/2))
			{
				break;
			}
			sum+=arr[j];
		}/*
		if(l==1&&r==6)
		{
			printf("%d %d %lld\n",i,j,lft);
		}*/
		val+=solve(j+1,i);
		val+=solve(l,j);
		return (ans+val);
	}
}

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&arr[i]);
	}
	sort(arr+1,arr+n+1);
	for(int i=1;i<=n;i++)
	{
		pre[i]=pre[i-1]+arr[i];
	}
	if(n==1)
	{
		printf("0\n");
	}
	else if(n<=3)
	{
		printf("%lld\n",pre[n]);
	}
	else
	{
		printf("%lld\n",solve(1,n));
	}
	return 0;
}


/******************************************
*    AUTHOR:         CHIRAG AGARWAL       *
*    INSTITUITION:   BITS PILANI, PILANI  *
******************************************/
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
const int MAX=2e5+5;
LL arr[MAX];
LL pre[MAX];

LL solve(int l,int r)
{
	if(l==r)
	{
		return 0ll;
	}
	if((l+1)==r)
	{
		return (pre[r]-pre[l-1]);
	}
	else
	{
		LL ans=pre[r]-pre[l-1];
		LL val=1e15;
		for(int j=l;j<r;j++)
		{
			for(int k=j+1;k<r;k++)
			{
				val=min(val,solve(l,j)+solve(j+1,k)+solve(k+1,r));
			}
		}
		return (ans+val);
	}
}

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&arr[i]);
	}
	sort(arr+1,arr+n+1);
	for(int i=1;i<=n;i++)
	{
		pre[i]=pre[i-1]+arr[i];
	}
	if(n==1)
	{
		printf("0\n");
	}
	else if(n<=3)
	{
		printf("%lld\n",pre[n]);
	}
	else
	{
		printf("%lld\n",solve(1,n));
	}
	return 0;
}


/******************************************
*    AUTHOR:         CHIRAG AGARWAL       *
*    INSTITUITION:   BITS PILANI, PILANI  *
******************************************/
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
const int MAX=2e5+5;
LL arr[MAX];

set<pair<LL,int> > st;

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&arr[i]);
		st.insert(make_pair(arr[i],i));
	}
	LL ans=0;
	int cur=n;
	LL fin=0;
	while(st.size()>1)
	{
		if(st.size()%2==0)
		{
			pair<LL,int> tmp=*st.begin();
			LL ans=tmp.first;
			st.erase(st.begin());
			tmp=*st.begin();
			ans+=tmp.first;
			st.erase(st.begin());
			st.insert(make_pair(ans,++cur));
			fin+=ans;
		}
		else
		{
			pair<LL,int> tmp=*st.begin();
			LL ans=tmp.first;
			st.erase(st.begin());
			tmp=*st.begin();
			ans+=tmp.first;
			st.erase(st.begin());
			tmp=*st.begin();
			ans+=tmp.first;
			st.erase(st.begin());
			st.insert(make_pair(ans,++cur));
			fin+=ans;
//			printf("cam%lld\n",ans);
		}
	}
	printf("%lld\n",fin);
	return 0;
}


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

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

int main()
{
	ll sum = 0;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int x;
		scanf("%d", &x);
		q.push(x);
	}
	if (n == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	if (!(n % 2)) q.push(0);

	while (q.size() > 3)
	{
		ll t1 = q.top();
		q.pop();
		ll t2 = q.top();
		q.pop();
		ll t3 = q.top();
		q.pop();
		sum += t1 + t2 + t3;
		q.push(t1 + t2 + t3);
	}

	while (!q.empty())
	{
		sum += q.top();
		q.pop();
	}
	cout << sum << endl;
	//system("pause");
}


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

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

	int main()
	{
		ll sum = 0;
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			int x;
			scanf("%d", &x);
			q.push(x);
		}
		if (!(n % 2)) q.push(0);

		while (q.size() > 3)
		{
			ll t1 = q.top();
			q.pop();
			ll t2 = q.top();
			q.pop();
			ll t3 = q.top();
			q.pop();
			sum += t1 + t2 + t3;
			q.push(t1 + t2 + t3);
		}

		while (q.size() > 1)
		{
			sum += q.top();
			q.pop();
		}
		cout << sum << endl;
		//system("pause");
	}


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

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

	int main()
	{
		ll sum = 0;
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			int x;
			scanf("%d", &x);
			q.push(x);
		}
		if (!(n % 2)) q.push(0);

		while (q.size() > 3)
		{
			ll t1 = q.top();
			q.pop();
			ll t2 = q.top();
			q.pop();
			ll t3 = q.top();
			q.pop();
			sum += t1 + t2 + t3;
			q.push(t1 + t2 + t3);
		}

		while (!q.empty())
		{
			sum += q.top();
			q.pop();
		}
		cout << sum << endl;
		//system("pause");
	}


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

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

	int main()
	{
		ll sum = 0;
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			int x;
			scanf("%d", &x);
			q.push(x);
		}
		if (n == 1)
		{
		    cout << q.top() << endl;
		    return 0;
		}
		if (!(n % 2)) q.push(0);

		while (q.size() > 3)
		{
			ll t1 = q.top();
			q.pop();
			ll t2 = q.top();
			q.pop();
			ll t3 = q.top();
			q.pop();
			sum += t1 + t2 + t3;
			q.push(t1 + t2 + t3);
		}

		while (!q.empty())
		{
			sum += q.top();
			q.pop();
		}
		cout << sum << endl;
		//system("pause");
	}


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

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

	int main()
	{
		ll sum = 0;
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			int x;
			scanf("%d", &x);
			q.push(x);
		}
		if (!(n % 2)) q.push(0);

		while (q.size() >= 3)
		{
			ll t1 = q.top();
			q.pop();
			ll t2 = q.top();
			q.pop();
			ll t3 = q.top();
			q.pop();
			sum += t1 + t2 + t3;
			q.push(t1 + t2 + t3);
		}

		while (!q.empty())
		{
			sum += q.top();
			q.pop();
		}
		cout << sum << endl;
		//system("pause");
	}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define clr(ma) memset(ma,0,sizeof(ma))
#define pi pair<int ,int>
#define mk make_pair
#define pb push_back
#define setBit(m,i) (m|(1<<i))
#define cont(i,ma) ((ma.find(i))!=(ma.end()))
#define in(i) scanf("%d",&i)
#define in2(i,j) scanf("%d%d",&i,&j)
#define in3(i,j,k) scanf("%d%d%d",&i,&j,&k)
#define in4(i,j,k,l) scanf("%d%d%d%d",&i,&j,&k,&l)
#define fi(ss) freopen (ss,"r",stdin)
#define fo(ss) freopen (ss,"w",stdout)
#define forn(i,n) for(int i = 0;i<(n);i++)
#define rep(n) for(int __i__ = 0;__i__<(n);__i__++)
#define MOD 1000000007
#define MAX 100001

int n;
int main(){
	//fi("input");
    int n,k;
    in(n);
    ll sm = 0;
    vector<int> cnt = vector<int>(n);
    forn(i,n) {
    	in(cnt[i]);
    	sm += cnt[i];
    }

    sort(cnt.begin(), cnt.end());
    k = cnt.size() - 1;

    ll penalty = 0;
    while(k> 0){
    	penalty += sm;
    	sm -= cnt[k--];
    	if(k>3){
    		int lst3 = cnt[k] + cnt[k-1] + cnt[k-2];
    		penalty += lst3;
    		sm -= lst3;
    		k-=3;
    	}
    }

    cout << penalty;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define clr(ma) memset(ma,0,sizeof(ma))
#define pi pair<int ,int>
#define mk make_pair
#define pb push_back
#define setBit(m,i) (m|(1<<i))
#define cont(i,ma) ((ma.find(i))!=(ma.end()))
#define in(i) scanf("%d",&i)
#define in2(i,j) scanf("%d%d",&i,&j)
#define in3(i,j,k) scanf("%d%d%d",&i,&j,&k)
#define in4(i,j,k,l) scanf("%d%d%d%d",&i,&j,&k,&l)
#define fi(ss) freopen (ss,"r",stdin)
#define fo(ss) freopen (ss,"w",stdout)
#define forn(i,n) for(int i = 0;i<(n);i++)
#define rep(n) for(int __i__ = 0;__i__<(n);__i__++)
#define MOD 1000000007
#define MAX 100001

int n;
vector<int> cnt;

ll evaluate(int l, int r){
	int s = r-l;

	if(s<2) return cnt[l];
	if(s==2) return cnt[l] + cnt[l+1];
	if(s==3) return cnt[l] + cnt[l+1] + cnt[l+2];
	if(s==4) return 2*cnt[l] + 2*cnt[l+1] + cnt[l+2] + cnt[l+3];

	if(s%3==0)s/=3;
	else s = 1 + s/3;

	int a = l+s;
	int b = a+s;

	ll res = 0;
	for(int i =l; i < r; i++) res += cnt[i];

	ll rs1 = evaluate(l, a) + evaluate(a,b) + evaluate(b,r);
	ll rs2 = evaluate(l,a+1) + evaluate(a+1,r-1);

	return res + min(rs1,rs2);
}

int main(){
	//fi("input");
    int n;
    in(n);
    ll sm = 0;
    cnt = vector<int>(n);
    forn(i,n) {
    	in(cnt[i]);
    	sm += cnt[i];
    }

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

    cout << evaluate(0,n);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define clr(ma) memset(ma,0,sizeof(ma))
#define pi pair<int ,int>
#define mk make_pair
#define pb push_back
#define setBit(m,i) (m|(1<<i))
#define cont(i,ma) ((ma.find(i))!=(ma.end()))
#define in(i) scanf("%d",&i)
#define in2(i,j) scanf("%d%d",&i,&j)
#define in3(i,j,k) scanf("%d%d%d",&i,&j,&k)
#define in4(i,j,k,l) scanf("%d%d%d%d",&i,&j,&k,&l)
#define fi(ss) freopen (ss,"r",stdin)
#define fo(ss) freopen (ss,"w",stdout)
#define forn(i,n) for(int i = 0;i<(n);i++)
#define rep(n) for(int __i__ = 0;__i__<(n);__i__++)
#define MOD 1000000007
#define MAX 100001

int n;
vector<int> cnt;

ll evaluate(int l, int r){
	int s = r-l;

	if(s<2) return 0;
	if(s==2) return cnt[l] + cnt[l+1];
	if(s==3) return cnt[l] + cnt[l+1] + cnt[l+2];
	if(s==4) return 2*cnt[l] + 2*cnt[l+1] + cnt[l+2] + cnt[l+3];

	if(s%3==0)s/=3;
	else s = 1 + s/3;

	int a = l+s;
	int b = a+s;

	ll res = 0;
	for(int i =l; i < r; i++) res += cnt[i];

	ll rs1 = evaluate(l, a) + evaluate(a,b) + evaluate(b,r);
	ll rs2 = evaluate(l,a+1) + evaluate(a+1,r-1);

	return res + min(rs1,rs2);
}

int main(){
	//i("input");
    int n;
    in(n);
    ll sm = 0;
    cnt = vector<int>(n);
    forn(i,n) {
    	in(cnt[i]);
    	sm += cnt[i];
    }

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

    cout << evaluate(0,n);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define clr(ma) memset(ma,0,sizeof(ma))
#define pi pair<int ,int>
#define mk make_pair
#define pb push_back
#define setBit(m,i) (m|(1<<i))
#define cont(i,ma) ((ma.find(i))!=(ma.end()))
#define in(i) scanf("%d",&i)
#define in2(i,j) scanf("%d%d",&i,&j)
#define in3(i,j,k) scanf("%d%d%d",&i,&j,&k)
#define in4(i,j,k,l) scanf("%d%d%d%d",&i,&j,&k,&l)
#define fi(ss) freopen (ss,"r",stdin)
#define fo(ss) freopen (ss,"w",stdout)
#define forn(i,n) for(int i = 0;i<(n);i++)
#define rep(n) for(int __i__ = 0;__i__<(n);__i__++)
#define MOD 1000000007
#define MAX 100001

int n;
int main(){
    int n,k;
    in(n);
    ll sm = 0;
    vector<int> cnt = vector<int>(n);
    forn(i,n) {
    	in(cnt[i]);
    	sm += cnt[i];
    }

    sort(cnt.begin(), cnt.end());
    k = cnt.size() - 1;

    ll penalty = 0;
    while(k> 0){
    	penalty += sm;
    	sm -= cnt[k--];
    	if(k>=0) sm-=cnt[k--];
    }

    cout << penalty;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define clr(ma) memset(ma,0,sizeof(ma))
#define pi pair<int ,int>
#define mk make_pair
#define pb push_back
#define setBit(m,i) (m|(1<<i))
#define cont(i,ma) ((ma.find(i))!=(ma.end()))
#define in(i) scanf("%d",&i)
#define in2(i,j) scanf("%d%d",&i,&j)
#define in3(i,j,k) scanf("%d%d%d",&i,&j,&k)
#define in4(i,j,k,l) scanf("%d%d%d%d",&i,&j,&k,&l)
#define fi(ss) freopen (ss,"r",stdin)
#define fo(ss) freopen (ss,"w",stdout)
#define forn(i,n) for(int i = 0;i<(n);i++)
#define rep(n) for(int __i__ = 0;__i__<(n);__i__++)
#define MOD 1000000007
#define MAX 100001

int n;
int main(){
	//fi("input");
    int n,k;
    in(n);
    ll sm = 0;
    vector<int> cnt = vector<int>(n);
    forn(i,n) {
    	in(cnt[i]);
    	sm += cnt[i];
    }

    sort(cnt.begin(), cnt.end());
    k = cnt.size() - 1;

    ll penalty = 0;
    while(k> 0){
    	penalty += sm;
    	sm -= cnt[k--];
    	if(k>3){
    		int lst3 = cnt[k] + cnt[k-1] + cnt[k-2];
    		penalty += lst3;
    		sm -= lst3;
    		k-=3;
    	} else if( k>=0){
    		sm -= cnt[k--];
    	}
    }

    cout << penalty;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define clr(ma) memset(ma,0,sizeof(ma))
#define pi pair<int ,int>
#define mk make_pair
#define pb push_back
#define setBit(m,i) (m|(1<<i))
#define cont(i,ma) ((ma.find(i))!=(ma.end()))
#define in(i) scanf("%d",&i)
#define in2(i,j) scanf("%d%d",&i,&j)
#define in3(i,j,k) scanf("%d%d%d",&i,&j,&k)
#define in4(i,j,k,l) scanf("%d%d%d%d",&i,&j,&k,&l)
#define fi(ss) freopen (ss,"r",stdin)
#define fo(ss) freopen (ss,"w",stdout)
#define forn(i,n) for(int i = 0;i<(n);i++)
#define rep(n) for(int __i__ = 0;__i__<(n);__i__++)
#define MOD 1000000007
#define MAX 100001

const ll INF = 0xFFFFFFFFFFF;

int n;
int main(){
	//fi("input");
    int n,k;
    in(n);
    vector<int> cnt = vector<int>(n);
    priority_queue<ll, vector<ll>, greater<ll>> Q;
    forn(i,n) {
    	in(k);
    	Q.push(k);
    }

    ll penalty = 0;
    if(Q.size() % 2 == 0){
    	ll a = Q.top(); Q.pop();
    	a += Q.top(); Q.pop();
    	penalty += a;
    	Q.push(a);
    }

    while(Q.size() > 1){
    	ll a = Q.top(); Q.pop();
    	a += Q.top(); Q.pop();
    	a += Q.top(); Q.pop();
    	penalty += a;
    	Q.push(a);
    }

    cout << penalty;
    return 0;
}


//MY code , MIND it !!
#include<bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define ll long long int
using namespace std;
int main()
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   ll n,a[200005],t1,t2,t3;
   priority_queue<int,vector<int>,greater<int> >q;
   ll ans=0;
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
       q.push(a[i]);
   }
   int tmp=0;
   if(n%2==0)
   {
       q.push(0);
   }
   while(!q.empty())
   {
       //ans+=s[n-tmp-1];
       t1=q.top();
       q.pop();
       t2=q.top();
       q.pop();
       t3=q.top();
       q.pop();
       ans+=t1+t2+t3;
       q.push(t1+t2+t3);
       if(q.size()==1)
       {
           break;
       }
   }
   cout<<ans<<"\n";

return 0;
}


//MY code , MIND it !!
#include<bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define ll long long int
using namespace std;
int main()
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   ll n,a[200005],t1,t2,t3;
   priority_queue<ll,vector<ll>,greater<ll> >q;
   ll ans=0;
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
       q.push(a[i]);
   }
   int tmp=0;
   if(n%2==0)
   {
       q.push(0);
   }
   while(!q.empty())
   {
       //ans+=s[n-tmp-1];
       t1=q.top();
       q.pop();
       t2=q.top();
       q.pop();
       t3=q.top();
       q.pop();
       ans+=t1+t2+t3;
       q.push(t1+t2+t3);
       if(q.size()==1)
       {
           break;
       }
   }
   cout<<ans<<"\n";

return 0;
}


//MY code , MIND it !!
#include<bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define ll long long int
using namespace std;
int main()
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   int n,a[200005],s[200005];
   priority_queue<int>q;
   ll ans=0;
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
   sort(a,a+n);
   s[0]=a[0];
   for(int i=1;i<n;i++)
   {
       s[i]=s[i-1]+a[i];
       //cout<<s[i]<<"\n";
   }
   for(int i=0;i<n;i++)
   {
       q.push(a[i]);
   }
   int tmp=0;
   while(!q.empty())
   {
       //ans+=s[n-tmp-1];
       if(n-tmp<=1)
       {
           break;
       }
       else if(n-tmp==2)
       {
           q.pop();
           ans+=s[n-tmp-1];
           tmp+=1;
       }
       else
       {
           q.pop();
           q.pop();
           ans+=s[n-tmp-1];
           tmp+=2;
       }
   }
   cout<<ans<<"\n";

return 0;
}


//MY code , MIND it !!
#include<bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define ll long long int
using namespace std;
int main()
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   ll n,a[200005],t1,t2,t3;
   priority_queue<ll,vector<ll>,greater<ll> >q;
   ll ans=0;
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
   if(n==1)
   {
       cout<<"0\n";
       return 0;
   }
   for(int i=0;i<n;i++)
   {
       q.push(a[i]);
   }
   int tmp=0;
   if(n%2==0)
   {
       q.push(0);
   }
   while(!q.empty())
   {
       //ans+=s[n-tmp-1];
       t1=q.top();
       q.pop();
       t2=q.top();
       q.pop();
       t3=q.top();
       q.pop();
       ans+=t1+t2+t3;
       q.push(t1+t2+t3);
       if(q.size()==1)
       {
           break;
       }
   }
   cout<<ans<<"\n";

return 0;
}


//MY code , MIND it !!
#include<bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define ll long long int
using namespace std;
int main()
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   int n,a[200005],t1,t2,t3;
   priority_queue<int,vector<int>,greater<int> >q;
   int ans=0;
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
       q.push(a[i]);
   }
   int tmp=0;
   if(n%2==0)
   {
       q.push(0);
   }
   while(!q.empty())
   {
       //ans+=s[n-tmp-1];
       t1=q.top();
       q.pop();
       t2=q.top();
       q.pop();
       t3=q.top();
       q.pop();
       ans+=t1+t2+t3;
       q.push(t1+t2+t3);
       if(q.size()==1)
       {
           break;
       }
   }
   cout<<ans<<"\n";

return 0;
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<long long>q;
const int maxn=2e5;
int n;
long long tot, ans, pos, sum[maxn];
int pw(int sz)
{
    int d=2, x=1;
    while(d!=sz)
    {
        d*=2;
        x++;
    }
    return x;
}
int pwr(int sz)
{
    int d=3, x=1;
    while(d!=sz)
    {
        d*=3;
        x++;
    }
    return x;
}
void solve(int sz, vector<int>v)
{
    if(sz%2==0)pos+=(v.at(0)+v.at(1))*pw(sz);
    else if(sz%3==0)pos+=(v.at(0)+v.at(1)+v.at(2))*pwr(sz);
    else
    {
        int x=sz;
        while (x%3!=0)x++;
        vector<int>v1(v.begin(), v.begin()+x/3);
        vector<int>v2(v.begin()+x/3, v.begin()+(x/3)*2);
        vector<int>v3(v.begin()+(x/3)*2, v.end());
        pos+=sum[sz-1];
        solve(v1.size(), v1);
        solve(v2.size(), v2);
        solve(v3.size(), v3);
    }
    return;
}
int main()
{
    vector<int>v;
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin>>n;
    for(int i=0; i<n; i++)
    {
        long long x;
        cin>>x;
        tot+=x;
        q.push(x);
        v.push_back(x);
        if(i==0)sum[i]=x;
        else sum[i]=x+sum[i-1];
    }
    sort(v.begin(), v.end());
    while(q.size()>1)
    {
            long long x1=0, x2=0;
            x1=q.top();
            q.pop();
            if(!q.empty())
            {
            x2=q.top();
            q.pop();
            }
            ans+=tot;
            tot-=(x1+x2);
    }
    solve(n, v);
    cout<<max(ans, pos)<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
multiset<int>ms;
int ans;
int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        int x;
        cin>>x;
        ms.insert(x);
    }
    if(n%2==0)ms.insert(0);
    while(ms.size()!=1)
    {
        int a, b, c;
        a=*ms.begin(), ms.erase(ms.begin());
        b=*ms.begin(), ms.erase(ms.begin());
        c=*ms.begin(), ms.erase(ms.begin());
        ans+=a+b+c;
        ms.insert(a+b+c);
    }
    cout<<ans<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<long long>q;
int n;
long long tot, ans;
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin>>n;
    for(int i=0; i<n; i++)
    {
        long long x;
        cin>>x;
        tot+=x;
        q.push(x);
    }
    while(!(q.empty()||q.size()==1))
    {
            long long x1=0, x2=0;
            x1=q.top();
            q.pop();
            if(!q.empty())
            {
            x2=q.top();
            q.pop();
            }
            ans+=tot;
            tot-=(x1+x2);
    }
    cout<<ans<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int;
multiset<int>ms;
int ans;
int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        int x;
        cin>>x;
        ms.insert(x);
    }
    if(n%2==0)ms.insert(0);
    while(ms.size()!=1)
    {
        int a, b, c;
        a=*ms.begin(), ms.erase(ms.begin());
        b=*ms.begin(), ms.erase(ms.begin());
        c=*ms.begin(), ms.erase(ms.begin());
        ans+=a+b+c;
        ms.insert(a+b+c);
    }
    cout<<ans<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
multiset<ll>ms;
ll ans;
int main()
{
    ll n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        ll x;
        cin>>x;
        ms.insert(x);
    }
    if(n%2==0)ms.insert(0);
    while(ms.size()!=1)
    {
        ll a, b, c;
        a=*ms.begin(), ms.erase(ms.begin());
        b=*ms.begin(), ms.erase(ms.begin());
        c=*ms.begin(), ms.erase(ms.begin());
        ans+=a+b+c;
        ms.insert(a+b+c);
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iomanip>
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <regex>
#include <queue>

using namespace std;
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
const ll llmax=9223372036854775807;
const ll llmin=-9223372036854775808;
const int intmin=-2147483648;
const int intmax=2147483647;
ll gcd(ll a,ll b){
    if(a==0 || b==0) return a+b;
    else return gcd(b,a%b);
}

ll  power(ll x, ll y,ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y/2, m) % m;
    p = (p * p) % m;

    return (y%2 == 0)? p : (x * p) % m;
}
 ll comp6(deque<ll> dq){
     ll sum=0;
     for(int i=0;i<dq.size();i++)
         sum+=dq[i];
     ll s4=sum,s5=sum;
     ll fr=dq.front();
     sum-=fr;
     dq.pop_front();
     s5+=sum;
     fr=dq.front();
     sum-=fr;
     dq.pop_front();
     s4+=sum;
     s4+=dq.back();
     dq.pop_back();
     s4+=dq.back();
     dq.pop_back();
     return min(s4,s5);
 }
ll comp7(deque<ll> dq){
    ll sum=0;
    for(int i=0;i<dq.size();i++)
        sum+=dq[i];
    ll s6=sum,s5=sum;
    int ff=dq.front();
    sum-=ff;
    dq.pop_front();
    s6+=sum;
    ff=dq.front();
    sum-=ff;
    dq.pop_front();
    s5+=sum;
    for(int i=dq.size()-1;i>=dq.size()-4;i--)
        s5+=dq[i];
    return min(s5,s6);
}
int main() {

    //ios::sync_with_stdio(0), cin.tie(0);
    bool test=false;
    if(test)
        freopen("/Users/aj/code/HackerRank/input.txt", "r", stdin);
    int n;
    cin>>n;
    vector<ll> a;
    ll sum=0;
    for(int i=0;i<n;i++){
        ll v=0;
        cin>>v;
        a.emplace_back(v);
        sum+=v;
    }
    sort(a.begin(),a.end());
    deque<ll> dq;
    for(int i=n-1;i>=0;i--)
        dq.push_back(a[i]);
    ll sht=0;
    while(dq.size()>=7){
        sht+=sum;
        ll f=dq.front();
        dq.pop_front();
        sum-=f;
        f=dq.front();
        dq.pop_front();
        sum-=f;
    }
    if(dq.size()==6){
        cout<<comp6(dq);
        return 0;
    }
    if(dq.size()==7){
        cout<<comp7(dq);
        return 0;
    }
    if(n==1) {
        cout<<0;
        return 0;
    }
    else if(dq.size()<=3)
        sht+=sum;
    else if(dq.size()==4 || dq.size()==5){
        sht+=sum;
        ll f=dq.front();
        dq.pop_front();
        sum-=f;
        f=dq.front();
        dq.pop_front();
        sum-=f;
        sht+=sum;
    }
    cout<<sht;
    return 0;
}


#include <iomanip>
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <regex>
#include <queue>

using namespace std;
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
const ll llmax=9223372036854775807;
const ll llmin=-9223372036854775808;
const int intmin=-2147483648;
const int intmax=2147483647;
ll gcd(ll a,ll b){
    if(a==0 || b==0) return a+b;
    else return gcd(b,a%b);
}

ll  power(ll x, ll y,ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y/2, m) % m;
    p = (p * p) % m;

    return (y%2 == 0)? p : (x * p) % m;
}

int main() {

    //ios::sync_with_stdio(0), cin.tie(0);
    bool test=false;
    if(test)
        freopen("/Users/aj/code/HackerRank/input.txt", "r", stdin);
    int n;
    cin>>n;
    vector<ll> a;
    ll sum=0;
    for(int i=0;i<n;i++){
        ll v=0;
        cin>>v;
        a.emplace_back(v);
        sum+=v;
    }
    sort(a.begin(),a.end());
    deque<ll> dq;
    for(int i=n-1;i>=0;i--)
        dq.push_back(a[i]);
    ll sht=0;
    while(dq.size()>3){
        sht+=sum;
        ll f=dq.front();
        dq.pop_front();
        sum-=f;
        f=dq.front();
        dq.pop_front();
        sum-=f;
    }
    if(n==1)
        sht=0;
    else if(n>1 && dq.size()<=3)
        sht+=sum;
    cout<<sht;
    return 0;
}


#include <iomanip>
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <regex>
#include <queue>

using namespace std;
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
const ll llmax=9223372036854775807;
const ll llmin=-9223372036854775808;
const int intmin=-2147483648;
const int intmax=2147483647;
ll gcd(ll a,ll b){
    if(a==0 || b==0) return a+b;
    else return gcd(b,a%b);
}

ll  power(ll x, ll y,ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y/2, m) % m;
    p = (p * p) % m;

    return (y%2 == 0)? p : (x * p) % m;
}

int main() {

    //ios::sync_with_stdio(0), cin.tie(0);
    bool test=false;
    if(test)
        freopen("/Users/aj/code/HackerRank/input.txt", "r", stdin);
    int n;
    cin>>n;
    vector<ll> a;
    ll sum=0;
    for(int i=0;i<n;i++){
        ll v=0;
        cin>>v;
        a.emplace_back(v);
        sum+=v;
    }
    sort(a.begin(),a.end());
    deque<ll> dq;
    for(int i=n-1;i>=0;i--)
        dq.push_back(a[i]);
    ll sht=0;
    while(dq.size()>3){
        sht+=sum;
        ll f=dq.front();
        dq.pop_front();
        sum-=f;
        f=dq.front();
        dq.pop_front();
        sum-=f;
    }
    if(n==1)
        sht=0;
    else if(dq.size()<=3)
        sht+=sum;
    cout<<sht;
    return 0;
}


#include <iomanip>
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <regex>
#include <queue>

using namespace std;
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
const ll llmax=9223372036854775807;
const ll llmin=-9223372036854775808;
const int intmin=-2147483648;
const int intmax=2147483647;
ll gcd(ll a,ll b){
    if(a==0 || b==0) return a+b;
    else return gcd(b,a%b);
}

ll  power(ll x, ll y,ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y/2, m) % m;
    p = (p * p) % m;

    return (y%2 == 0)? p : (x * p) % m;
}

int main() {

    //ios::sync_with_stdio(0), cin.tie(0);
    bool test=false;
    if(test)
        freopen("/Users/aj/code/HackerRank/input.txt", "r", stdin);
    int n;
    cin>>n;
    vector<ll> a;
    ll sum=0;
    for(int i=0;i<n;i++){
        ll v=0;
        cin>>v;
        a.emplace_back(v);
        sum+=v;
    }
    sort(a.begin(),a.end());
    deque<ll> dq;
    for(int i=n-1;i>=0;i--)
        dq.push_back(a[i]);
    ll sht=0;
    while(dq.size()>3){
        sht+=sum;
        ll f=dq.front();
        dq.pop_front();
        sum-=f;
        f=dq.front();
        dq.pop_front();
        sum-=f;
    }
    if(n==1) {
        cout<<0;
        return 0;
    }
    else if(dq.size()<=3)
        sht+=sum;
    cout<<sht;
    return 0;
}


#include <iomanip>
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <regex>
#include <queue>

using namespace std;
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
const ll llmax=9223372036854775807;
const ll llmin=-9223372036854775808;
const int intmin=-2147483648;
const int intmax=2147483647;
ll gcd(ll a,ll b){
    if(a==0 || b==0) return a+b;
    else return gcd(b,a%b);
}

ll  power(ll x, ll y,ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y/2, m) % m;
    p = (p * p) % m;

    return (y%2 == 0)? p : (x * p) % m;
}

int main() {

    //ios::sync_with_stdio(0), cin.tie(0);
    bool test=false;
    if(test)
        freopen("/Users/aj/code/HackerRank/input.txt", "r", stdin);
    int n;
    cin>>n;
    multiset<ll> arr;
    for(int i=0;i<n;i++){
        ll v;
        cin>>v;
        arr.emplace(v);
    }
    if(n%2==0)
        arr.emplace(0);
    ll cnt=0;
    if(n==1){
        cout<<0;
        return 0;
    }
    while(arr.size()!=1){
        ll sum=0;
        for(int i=0;i<3;i++) {
            ll f = *arr.begin();
            arr.erase(arr.begin());
            sum+=f;
        }
        cnt+=sum;
        arr.emplace(sum);
    }
    cout<<cnt;

    return 0;
}


#include <iomanip>
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <regex>
#include <queue>

using namespace std;
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
const ll llmax=9223372036854775807;
const ll llmin=-9223372036854775808;
const int intmin=-2147483648;
const int intmax=2147483647;
ll gcd(ll a,ll b){
    if(a==0 || b==0) return a+b;
    else return gcd(b,a%b);
}

ll  power(ll x, ll y,ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y/2, m) % m;
    p = (p * p) % m;

    return (y%2 == 0)? p : (x * p) % m;
}

int main() {

    //ios::sync_with_stdio(0), cin.tie(0);
    bool test=false;
    if(test)
        freopen("/Users/aj/code/HackerRank/input.txt", "r", stdin);
    int n;
    cin>>n;
    vector<ll> a;
    ll sum=0;
    for(int i=0;i<n;i++){
        ll v=0;
        cin>>v;
        a.emplace_back(v);
        sum+=v;
    }
    sort(a.begin(),a.end());
    deque<ll> dq;
    for(int i=n-1;i>=0;i--)
        dq.push_back(a[i]);
    ll sht=0;
    while(dq.size()>3){
        sht+=sum;
        ll f=dq.front();
        dq.pop_front();
        sum-=f;
        f=dq.front();
        dq.pop_front();
        sum-=f;
    }
    if(n==1)
        sht=sum;
    else if(n>1 && dq.size()<=3)
        sht+=sum;
    cout<<sht;
    return 0;
}


#include <iomanip>
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <regex>
#include <queue>

using namespace std;
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
const ll llmax=9223372036854775807;
const ll llmin=-9223372036854775808;
const int intmin=-2147483648;
const int intmax=2147483647;
ll gcd(ll a,ll b){
    if(a==0 || b==0) return a+b;
    else return gcd(b,a%b);
}

ll  power(ll x, ll y,ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y/2, m) % m;
    p = (p * p) % m;

    return (y%2 == 0)? p : (x * p) % m;
}
 ll comp6(deque<ll> dq){
     ll sum=0;
     for(int i=0;i<dq.size();i++)
         sum+=dq[i];
     ll s4=sum,s5=sum;
     ll fr=dq.front();
     sum-=fr;
     dq.pop_front();
     s5+=sum;
     fr=dq.front();
     sum-=fr;
     dq.pop_front();
     s4+=sum;
     s4+=dq.back();
     dq.pop_back();
     s4+=dq.back();
     dq.pop_back();
     return min(s4,s5);
 }
ll comp7(deque<ll> dq){
    ll sum=0;
    for(int i=0;i<dq.size();i++)
        sum+=dq[i];
    ll s6=sum,s5=sum;
    ll ff=dq.front();
    sum-=ff;
    dq.pop_front();
    s6+=sum;
    ff=dq.front();
    sum-=ff;
    dq.pop_front();
    s5+=sum;
    for(int i=dq.size()-1;i>=dq.size()-4;i--)
        s5+=dq[i];
    return min(s5,s6);
}
int main() {

    //ios::sync_with_stdio(0), cin.tie(0);
    bool test=false;
    if(test)
        freopen("/Users/aj/code/HackerRank/input.txt", "r", stdin);
    int n;
    cin>>n;
    vector<ll> a;
    ll sum=0;
    for(int i=0;i<n;i++){
        ll v=0;
        cin>>v;
        a.emplace_back(v);
        sum+=v;
    }
    sort(a.begin(),a.end());
    deque<ll> dq;
    for(int i=n-1;i>=0;i--)
        dq.push_back(a[i]);
    ll sht=0;
    while(dq.size()>=7){
        sht+=sum;
        ll f=dq.front();
        dq.pop_front();
        sum-=f;
        f=dq.front();
        dq.pop_front();
        sum-=f;
    }
    if(dq.size()==6){
        cout<<sht+comp6(dq);
        return 0;
    }
    if(dq.size()==7){
        cout<<sht+comp7(dq);
        return 0;
    }
    if(n==1) {
        cout<<0;
        return 0;
    }
    else if(dq.size()<=3)
        sht+=sum;
    else if(dq.size()==4 || dq.size()==5){
        sht+=sum;
        ll f=dq.front();
        dq.pop_front();
        sum-=f;
        f=dq.front();
        dq.pop_front();
        sum-=f;
        sht+=sum;
    }
    cout<<sht;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0;i<n;i++)
	{
		cin >> a[i];
	}
	if (n % 2 == 0)
		a.push_back(0);
	multiset<long long> groups;
	long long sum = 0;
	for (int i = 0;i<a.size();i++)
	{
		groups.insert(a[i]);
	}
	while (groups.size()>1)
	{
		auto help = groups.begin();
		auto v1 = help;
		auto v2 = ++help;
		auto v3 = ++help;
		sum += (*v1) + (*v2) + (*v3);
		groups.erase(groups.begin(), ++help);
		groups.insert((*v1) + (*v2) + (*v3));
	}
	cout << sum;
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<long long> a(n);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    if(!(n&1))
        a.push_back(0);
    multiset<long long> groups;
    long long sum=0;
    for(int i=0;i<a.size();i++)
    {
        groups.insert(a[i]);
    }
    while(groups.size()>1)
    {
        auto help=groups.begin();
        auto v1=help;
        auto v2=++help;
        auto v3=++help;
        groups.erase(groups.begin(),++help);
        if(v3==groups.end())
            {
                sum+=(*v1)+(*v2);
                groups.insert((*v1)+(*v2));
            }
        else
          {
              sum+=(*v1)+(*v2)+(*v3);
                      groups.insert((*v1)+(*v2)+(*v3));
          }
    }
    cout<<sum;
    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    if(!(n&1))
        a.push_back(0);
    multiset<long long> groups;
    long long sum=0;
    for(int i=0;i<a.size();i++)
    {
        groups.insert(a[i]);
    }
    auto it=groups.begin();
    while(groups.size()!=1)
    {
        auto help=groups.begin();
        auto v1=help;
        auto v2=++help;
        auto v3=++help;
        groups.erase(groups.begin(),++help);
        sum+=(*v1)+(*v2)+(*v3);
        groups.insert((*v1)+(*v2)+(*v3));
    }
    cout<<sum;
    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<long long> a(n);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    if(!(n&1))
        a.push_back(0);
    multiset<long long> groups;
    long long sum=0;
    for(int i=0;i<a.size();i++)
    {
        groups.insert(a[i]);
    }
    while(groups.size()>1)
    {
        auto help=groups.begin();
        auto v1=help;
        auto v2=++help;
        auto v3=++help;
        groups.erase(groups.begin(),++help);
        if(v2==groups.end())
            {
                sum+=(*v1)+(*v2);
                groups.insert((*v1)+(*v2));
            }
        else
          {
              sum+=(*v1)+(*v2)+(*v3);
                      groups.insert((*v1)+(*v2)+(*v3));
          }
    }
    cout<<sum;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0;i<n;i++)
	{
		cin >> a[i];
	}
	if (n % 2 == 0)
		a.push_back(0);
	multiset<long long> groups;
	long long sum = 0;
	for (int i = 0;i<a.size();i++)
	{
		groups.insert(a[i]);
	}
	while (groups.size()>1)
	{
		auto help = groups.begin();
		auto v1 = help;
		auto v2 = ++help;
		auto v3 = ++help;
		long long newone = (*v1) + (*v2) + (*v3);
		sum += newone;
		groups.erase(groups.begin(), ++help);
		groups.insert(newone);
	}
	cout << sum;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
	ifstream fin("output.txt");
	int n;
	fin >> n;
	vector<int> a(n);
	for (int i = 0;i<n;i++)
	{
		fin >> a[i];
	}
	if (n % 2 == 0)
		a.push_back(0);
	multiset<long long> groups;
	long long sum = 0;
	for (int i = 0;i<a.size();i++)
	{
		groups.insert(a[i]);
	}
	while (groups.size()>1)
	{
		auto help = groups.begin();
		auto v1 = help;
		auto v2 = ++help;
		auto v3 = ++help;
		long long newone = (*v1) + (*v2) + (*v3);
		sum += newone;
		groups.erase(groups.begin(), ++help);
		groups.insert(newone);
	}
	cout << sum;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0;i<n;i++)
	{
		cin >> a[i];
	}
	if (n % 2 == 0)
		a.push_back(0);
	multiset<long long> groups;
	long long sum = 0;
	int as=a.size();
	for (int i = 0;i<as;i++)
	{
		groups.insert(a[i]);
	}
	while (groups.size()>1)
	{
		auto help = groups.begin();
		auto v1 = help;
		auto v2 = ++help;
		auto v3 = ++help;
		long long newone = (*v1) + (*v2) + (*v3);
		sum += newone;
		groups.erase(groups.begin(), ++help);
		groups.insert(newone);
	}
	cout << sum;
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<long long> a(n);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    if(!(n&1))
        a.push_back(0);
    multiset<long long> groups;
    long long sum=0;
    for(int i=0;i<a.size();i++)
    {
        groups.insert(a[i]);
    }
    while(groups.size()>1)
    {
        auto help=groups.begin();
        auto v1=help;
        auto v2=++help;
        auto v3=++help;
        groups.erase(groups.begin(),++help);
        sum+=(*v1)+(*v2)+(*v3);
        groups.insert((*v1)+(*v2)+(*v3));
    }
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
long long a, tot, sol;

priority_queue <long long, vector <long long>, greater<long long> > Q;
//bool m[ 200000]

int main(){

    scanf ("%d", &n);
    for (int i = 1; i <= n; i++){
    	 scanf ("%lld", &a);
    	 Q.push (a);
    	 tot += a;
    }

    //int p = 1;
    while ((int)Q.size() > 3){
           long long x = Q.top(); Q.pop();
           long long y = Q.top(); Q.pop();
           Q.push (x+y);
           sol += x+y;
           //printf ("%lld %lld\n", x, y);
    }

    while (!Q.empty()){
    	   sol += Q.top();
    	   Q.pop();
    }

	printf ("%lld\n", sol);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
long long a, tot, sol;
struct par{
       long long v, id;
       bool operator<(const par &r)const{
            return  v < r.v;
       }
};

priority_queue <par> Q;
//bool m[ 200000]

int main(){

    scanf ("%d", &n);
    for (int i = 1; i <= n; i++){
    	 scanf ("%lld", &a);
    	 Q.push ((par){a, i});
    	 tot += a;
    }

    //int p = 1;
    while ((int)Q.size() > 3){
    	   par x = Q.top(); Q.pop();
    	   par y = Q.top(); Q.pop();
    	   sol += tot;
    	   if (Q.empty()) break;

           tot -= x.v;
           tot -= y.v;
           //m[x.id] = m[y.id] = true;
           //while (m[p])
     }

     if ((int)Q.size() > 1)
     while (!Q.empty()){
     	    sol += Q.top().v; Q.pop();
     }

     printf ("%lld\n", sol);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
long long a, tot, sol;

priority_queue <long long, vector <long long>, greater<long long> > Q;
//bool m[ 200000]

int main(){

    scanf ("%d", &n);
    for (int i = 1; i <= n; i++){
    	 scanf ("%lld", &a);
    	 Q.push (a);
    	 tot += a;
    }

    //int p = 1;
    while ((int)Q.size() >= 3){
           long long x = Q.top(); Q.pop();
           long long y = Q.top(); Q.pop();

           if ((int)Q.size() == 2){
               sol += x+y;
               Q.push(x+y);
               continue;
           }

           long long z = Q.top(); Q.pop();
           Q.push (x+y+z);
           sol += x+y+z;
           //printf ("%lld %lld\n", x, y);
    }

    if ((int)Q.size() > 1)
    while (!Q.empty()){
    	   sol += Q.top();
    	   Q.pop();
    }

	printf ("%lld\n", sol);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
long long a, tot, sol;
struct par{
       long long v, id;
       bool operator<(const par &r)const{
            return  v < r.v;
       }
};

priority_queue <par> Q;
//bool m[ 200000]

int main(){

    scanf ("%d", &n);
    for (int i = 1; i <= n; i++){
    	 scanf ("%lld", &a);
    	 Q.push ({a, i});
    	 tot += a;
    }

    //int p = 1;
    while ((int)Q.size() > 1){
    	   par x = Q.top(); Q.pop();
    	   par y = Q.top(); Q.pop();
    	   sol += tot;
    	   if (Q.empty()) break;

           tot -= x.v;
           tot -= y.v;
           //m[x.id] = m[y.id] = true;
           //while (m[p])
     }

	 printf ("%lld\n", sol);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
long long a, tot, sol;

priority_queue <long long, vector <long long>, greater<long long> > Q;
//bool m[ 200000]

int main(){

    scanf ("%d", &n);
    for (int i = 1; i <= n; i++){
    	 scanf ("%lld", &a);
    	 Q.push (a);
    	 tot += a;
    }

    if (n%2==0){
    	long long x = Q.top(); Q.pop();
        long long y = Q.top(); Q.pop();
        Q.push (x+y);
        sol += x+y;
    }
    //printf ("%lld\n", sol);
    //int p = 1;
    while ((int)Q.size() >= 3){
           long long x = Q.top(); Q.pop();
           long long y = Q.top(); Q.pop();
           long long z = Q.top(); Q.pop();
           Q.push (x+y+z);
           sol += x+y+z;
           //printf ("%lld %lld\n", x, y);
    }

    if ((int)Q.size() > 1)
    while (!Q.empty()){
    	   sol += Q.top();
    	   Q.pop();
    }

	printf ("%lld\n", sol);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
long long a, tot, sol;

priority_queue <long long, vector <long long>, greater<long long> > Q;
//bool m[ 200000]

int main(){

    scanf ("%d", &n);
    for (int i = 1; i <= n; i++){
    	 scanf ("%lld", &a);
    	 Q.push (a);
    	 tot += a;
    }

    if (n == 1){
    	printf ("0\n");
    	return 0;
    }

    //int p = 1;
    while ((int)Q.size() > 3){
           long long x = Q.top(); Q.pop();
           long long y = Q.top(); Q.pop();
           Q.push (x+y);
           sol += x+y;
           // 	printf ("%lld %lld\n", x, y);
    }

    while (!Q.empty()){
    	   sol += Q.top();
    	   Q.pop();
    }

	printf ("%lld\n", sol);

	return 0;
}


//PUTOVAL SAM PA NESM MOGL DA RADIM
// NIKVO DOK NEJE VEC PROSLO SAHAT VREME OD TAKMICENJE!
#include <bits/stdc++.h>
#define day 86400
#define izlezni cout
#define ulezni cin

using namespace std;

int n, ans, sum;
vector<int> nums;

int main(){
	ios_base::sync_with_stdio(false);
	ulezni.tie(nullptr);
	ulezni >> n;
	for(int i = 0; i < n; ++i){
		int a;
		cin >> a;
		sum += a;
		nums.push_back(a);
	}
	sort(nums.begin(), nums.end());
	reverse(nums.begin(), nums.end());
	if(n & 1 == 0){
		for(int i = 0; i < n; ++i){
			ans += sum;
			sum -= nums[i];
			sum -= nums[i+1];
		}
	} else {
		for(int i = 0; i < n - 3; ++i)
		{
			ans += sum;
			sum -= nums[i];
			sum -= nums[i+1];
		}
		ans += sum;
	}
	izlezni << ans;
}


#include <bits/stdc++.h>

using namespace std;

long long n, ans;
priority_queue<int, vector<int>, greater<int> > heap;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int a;
		cin >> a;
		heap.push(a);
	}
	if(n%2 == 0){
		long long a= heap.top();
		heap.pop();
		long long b = heap.top();
		heap.pop();
		ans += a+b;
		heap.push(a+b);
	}
	while(heap.size() != 1){
		long long a= heap.top(); heap.pop();
		long long b = heap.top(); heap.pop();
		long long c = heap.top(); heap.pop();
		ans += a+b+c;
		heap.push(a+b+c);
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

long long n, ans;
priority_queue<long long, vector<long long>, greater<long long> > heap;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int a;
		cin >> a;
		heap.push(a);
	}
	if(n%2 == 0){
		long long a= heap.top();
		heap.pop();
		long long b = heap.top();
		heap.pop();
		ans += a+b;
		heap.push(a+b);
	}
	while(heap.size() != 1){
		long long a= heap.top(); heap.pop();
		long long b = heap.top(); heap.pop();
		long long c = heap.top(); heap.pop();
		ans += a+b+c;
		heap.push(a+b+c);
	}
	cout << ans;
}


//PUTOVAL SAM PA NESM MOGL DA RADIM
// NIKVO DOK NEJE VEC PROSLO SAHAT VREME OD TAKMICENJE!
#include <bits/stdc++.h>
#define day 86400
#define izlezni cout
#define ulezni cin

using namespace std;

long long n, ans, sum;
vector<long long> nums;

int main(){
	ios_base::sync_with_stdio(false);
	ulezni.tie(nullptr);
	ulezni >> n;
	for(int i = 0; i < n; ++i){
		int a;
		cin >> a;
		sum += a;
		nums.push_back(a);
	}
	sort(nums.begin(), nums.end());
	reverse(nums.begin(), nums.end());
	if(n & 1 == 0){
		for(int i = 0; i < n; ++i){
			ans += sum;
			sum -= nums[i];
			sum -= nums[i+1];
		}
	} else {
		for(int i = 0; i < n - 3; ++i)
		{
			ans += sum;
			sum -= nums[i];
			sum -= nums[i+1];
		}
		ans += sum;
	}
	izlezni << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 200100
#define ll long long
int a[maxn],n;
ll tot,ans;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		tot+=a[i];
	}
	sort(a+1,a+1+n);
	while(n>=2)
	{
		ans+=tot;
		tot-=(a[n]+a[n-1]);
		n-=2;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define P pair<ll,int>
#define maxn 200100
set<P>s;
set<P>::iterator it;
int n,a[maxn];
ll ans;
P t[3];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		s.insert(P(a[i],i));
	}
	if(n==1)
	{
		puts("0");
		exit(0);
	}
	if(n&1) s.insert(P(0,++n));
	while((int)s.size()>1)
	{
		it=s.begin();
		ll tot=0;
		for(int i=0;i<2;++i)
		{
			tot+=(*it).first;
			t[i]=(*it);
			++it;
		}
		for(int i=0;i<2;++i) s.erase(t[i]);
		ans+=tot;
		s.insert(P(tot,++n));
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 200110
#define P pair<int,int>
#define ll long long
int a[maxn],n;
queue<P>Q;
int rou;
ll tot,ans;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		tot+=a[i];
	}
	sort(a+1,a+1+n);
	Q.push(P(n,0));
	int las=-1;
	while(!Q.empty())
	{
		P tmp=Q.front(); Q.pop();
		if(tmp.second!=las)
		{
			ans+=tot;
			las=tmp.second;
		}
		if(tmp.first>5)
		{
			tot-=a[n--];
			int lx=tmp.first;
			lx--;
			Q.push(P(lx/2,tmp.second+1));
			Q.push(P(lx-lx/2,tmp.second+1));
		}
		else if(tmp.first>3)
		{
			tot-=a[n--];
			tot-=a[n--];
			Q.push(P(2,tmp.second+1));
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define P pair<ll,int>
#define maxn 200100
set<P>s;
set<P>::iterator it;
int n,a[maxn];
ll ans;
P t[3];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		s.insert(P(a[i],i));
	}
	while((int)s.size()>1)
	{
		if((int)s.size()<4)
		{
			for(it=s.begin();it!=s.end();++it)
				ans+=(*it).first;
			break;
		}
		else if((int)s.size()==4)
		{
			it=s.begin();
			ll tot=0;
			for(int i=0;i<2;++i)
			{
				tot+=(*it).first;
				t[i]=(*it);
				it++;
			}
			ans+=tot;
			for(int i=0;i<2;++i) s.erase(t[i]);
			s.insert(P(tot,++n));
		}
		else
		{
			it=s.begin();
			ll tot=0;
			for(int i=0;i<3;++i)
			{
				tot+=(*it).first;
				t[i]=(*it);
				++it;
			}
			for(int i=0;i<3;++i) s.erase(t[i]);
			ans+=tot;
			s.insert(P(tot,++n));
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define P pair<ll,int>
#define maxn 200100
set<P>s;
set<P>::iterator it;
int n,a[maxn];
ll ans;
P t[3];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		s.insert(P(a[i],i));
	}
	if(n==1)
	{
		puts("0");
		exit(0);
	}
	if(n%2==0) s.insert(P(0,++n));
	while((int)s.size()>1)
	{
		it=s.begin();
		ll tot=0;
		for(int i=0;i<3;++i)
		{
			tot+=(*it).first;
			t[i]=(*it);
			++it;
		}
		for(int i=0;i<3;++i) s.erase(t[i]);
		ans+=tot;
		s.insert(P(tot,++n));
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 200110
#define P pair<int,int>
#define ll long long
int a[maxn],n;
queue<P>Q;
int rou;
ll tot,ans;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		tot+=a[i];
	}
	sort(a+1,a+1+n);
	Q.push(P(n,0));
	int las=-1;
	while(!Q.empty())
	{
		P tmp=Q.front(); Q.pop();
		if(tmp.second!=las)
		{
			ans+=tot;
			las=tmp.second;
		}
		if(tmp.first>4)
		{
			tot-=a[n--];
			int lx=tmp.first;
			lx--;
			Q.push(P(lx/2,tmp.second+1));
			Q.push(P(lx-lx/2,tmp.second+1));
		}
		else if(tmp.first==4)
		{
			tot-=a[n--];
			tot-=a[n--];
			Q.push(P(2,tmp.second+1));
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define mp make_pair
#define pb push_back
#define LL long long
#define gch getchar()
#define x first
#define y second
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define e 2147000000
#define R uf()

/*
int find(int x) {
	if ( dsu[x] == x ) return x;
	return dsu[x] = find(dsu[x]);
}
*/

LL NUM; // change LL to long long
char C;
int uf() {
	NUM = 0;
	C = gch;
	while ( C > '9' || C < '0' ) C = gch;
	while ( C <= '9' && C >= '0' ) {
		NUM = NUM * 10 + C - '0';
		C = gch;
	}
	return NUM;
}

LL n, a[200005], ans, r1, r2, r3;
priority_queue<LL> pq;

int main() {
	n = R;
	for ( int i = 1; i <= n; i++ ) {
		pq.push(-R);
	}
	if ( !(n % 2) ) pq.push(0);
	while ( pq.size() > 1 ) {
		r1 = pq.top();
		pq.pop();
		r2 = pq.top();
		pq.pop();
		r3 = pq.top();
		pq.pop();
		ans = ans - r1 - r2 - r3;
		pq.push(r1 + r2 + r3);
	}
	printf("%lld\n", ans);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define mp make_pair
#define pb push_back
#define LL long long
#define gch getchar()
#define x first
#define y second
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define e 2147000000
#define R uf()

/*
int find(int x) {
	if ( dsu[x] == x ) return x;
	return dsu[x] = find(dsu[x]);
}
*/

LL NUM; // change LL to long long
char C;
int uf() {
	NUM = 0;
	C = gch;
	while ( C > '9' || C < '0' ) C = gch;
	while ( C <= '9' && C >= '0' ) {
		NUM = NUM * 10 + C - '0';
		C = gch;
	}
	return NUM;
}

LL n, a[200005], ans, r1, r2, r3;
priority_queue<int> pq;

int main() {
	n = R;
	for ( int i = 1; i <= n; i++ ) {
		pq.push(-R);
	}
	if ( !(n % 2) ) pq.push(0);
	while ( pq.size() > 1 ) {
		r1 = pq.top();
		pq.pop();
		r2 = pq.top();
		pq.pop();
		r3 = pq.top();
		pq.pop();
		ans = ans - r1 - r2 - r3;
		pq.push(r1 + r2 + r3);
	}
	printf("%lld\n", ans);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define mp make_pair
#define pb push_back
#define LL long long
#define gch getchar()
#define x first
#define y second
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define e 2147000000
#define R uf()

/*
int find(int x) {
	if ( dsu[x] == x ) return x;
	return dsu[x] = find(dsu[x]);
}
*/

LL NUM; // change LL to long long
char C;
int uf() {
	NUM = 0;
	C = gch;
	while ( C > '9' || C < '0' ) C = gch;
	while ( C <= '9' && C >= '0' ) {
		NUM = NUM * 10 + C - '0';
		C = gch;
	}
	return NUM;
}

LL n, a[200005], p, sum, ans;

int main() {
	n = R;
	for ( int i = 1; i <= n; i++ ) {
		a[i] = R;
		sum += a[i];
	}
	sort(a + 1, a + n + 1);
	p = n;
	while ( p > 2 ) {
		ans += sum;
		sum = sum - a[p] - a[p - 1];
		p -= 2;
	}
	if ( p == 2 ) ans += sum;
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define REV(i,j,k) for(int i=j;i>=k;i--)
#define FORR(i,j,k,l) for(int i=j;i<=k;i+=l)
#define inf         freopen("in.txt", "r", stdin)
#define outf        freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c)    scanf("%d %d %d", &a, &b, &c)
#define clean(mat,n)   FOR(i,0,n) mat[i].clear()
#define minn          (long long)-1000000000000000000
#define maxx          (long long) 1000000000000000000
#define mod          1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define LL            long long
#define NL '\n'
#define cnd tree[idx]
#define lnd (idx<<1)
#define rnd ((idx<<1)+1)
#define PB push_back
#define F first
#define S second
#define MAX 200010
#define SZ 100010
#define MP make_pair
#define valid(nx,ny)  ((nx >= 0) && (nx < row) && (ny >= 0) && (ny < col))
typedef pair<int,int> pr;

int ar[MAX];
LL val,c,sum[MAX],d;

void rec(int st,int en)
{

    if(st>=en)return;

    int x,y,z;
    LL vv=0;

    val=sum[en];
    if(st!=0)val-=sum[st-1],vv=sum[st-1];
    c=val/3;
    ///if(en-st<=2)return val;

    x=lower_bound(sum+st,sum+en+1,vv+c)-sum;
    x=min(en,x);

    vv=sum[x];
    y=lower_bound(sum+x+1,sum+en+1,vv+c)-sum;
    y=min(en,y);

    if(x==y) x=(st+x)/2;

    x=max(st,x);
    y=max(x+1,y);
    y=min(y,en-1);

    if(x-1>=st&&x==y) x--;
    else if(x==y)y++;

    d+=val;

    rec(st,max(st,x));
    rec(max(st,x)+1,max(max(st,x)+1,y));
    rec(max(max(st,x)+1,y)+1,en);

}
int main()
{
    ios::sync_with_stdio(false);
    //inf;
    //outf;
    int i,j,n,m;

    cin>>n;
    FOR(i,0,n-1) cin>>ar[i];
    sort(ar,ar+n);
    FOR(i,0,n-1) c+=ar[i],sum[i]=c;

    rec(0,n-1);
    cout<<d<<NL;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define REV(i,j,k) for(int i=j;i>=k;i--)
#define FORR(i,j,k,l) for(int i=j;i<=k;i+=l)
#define inf         freopen("in.txt", "r", stdin)
#define outf        freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c)    scanf("%d %d %d", &a, &b, &c)
#define clean(mat,n)   FOR(i,0,n) mat[i].clear()
#define minn          (long long)-1000000000000000000
#define maxx          (long long) 1000000000000000000
#define mod          1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define LL           unsigned long long
#define NL '\n'
#define cnd tree[idx]
#define lnd (idx<<1)
#define rnd ((idx<<1)+1)
#define PB push_back
#define F first
#define S second
#define MAX 200010
#define SZ 100010
#define MP make_pair
#define valid(nx,ny)  ((nx >= 0) && (nx < row) && (ny >= 0) && (ny < col))
typedef pair<int,int> pr;

int ar[MAX];
LL val,c,sum[MAX];

LL rec(int st,int en)
{

    if(st>=en)return 0;

    int x,y,z;
    LL vv=0;

    val=sum[en];
    if(st!=0)val-=sum[st-1],vv=sum[st-1];
    c=val/3;
    if(en-st<=2)return val;

    x=upper_bound(sum+st,sum+en+1,vv+c)-sum;
    x--;
    x=max(x,st);

    vv=sum[x];
    y=upper_bound(sum+x+1,sum+en+1,vv+c)-sum;
    y--;
    y=max(x+1,y);
    y=min(y,en);
    if(y==en)
    {
        y=(y+en-1)/2;
    }

    return val+rec(st,x)+rec(x+1,y)+rec(y+1,en);

}
int main()
{
    ios::sync_with_stdio(false);
    //inf;
    //outf;
    int i,j,n,m;

    cin>>n;
    FOR(i,0,n-1) cin>>ar[i];
    sort(ar,ar+n,greater<int>());
    FOR(i,0,n-1) c+=ar[i],sum[i]=c;

    cout<<rec(0,n-1);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define REV(i,j,k) for(int i=j;i>=k;i--)
#define FORR(i,j,k,l) for(int i=j;i<=k;i+=l)
#define inf         freopen("in.txt", "r", stdin)
#define outf        freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c)    scanf("%d %d %d", &a, &b, &c)
#define clean(mat,n)   FOR(i,0,n) mat[i].clear()
#define minn          (long long)-1000000000000000000
#define maxx          (long long) 1000000000000000000
#define mod          1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define LL            long long
#define NL '\n'
#define cnd tree[idx]
#define lnd (idx<<1)
#define rnd ((idx<<1)+1)
#define PB push_back
#define F first
#define S second
#define MAX 200010
#define SZ 100010
#define MP make_pair
#define valid(nx,ny)  ((nx >= 0) && (nx < row) && (ny >= 0) && (ny < col))
typedef pair<int,int> pr;

int ar[MAX];
LL val,c,sum[MAX];

LL rec(int st,int en)
{

    if(st>=en)return 0;

    int x,y,z;
    LL vv=0;

    val=sum[en];
    if(st!=0)val-=sum[st-1],vv=sum[st-1];
    c=val/3;
    if(en-st<=2)return val;

    x=lower_bound(sum+st,sum+en+1,vv+c)-sum;
//cout<<vv<<' '<<c<<NL;
    vv=sum[x];
//cout<<vv<<NL;
    y=lower_bound(sum+x+1,sum+en+1,vv+c)-sum;
    y--;

    if(y==en)
    {
        y=(x+1+en)/2;
    }
    x=max(st,x);
    y=max(x+1,y);
    y=min(y,en-1);

    return val+rec(st,x)+rec(x+1,y)+rec(y+1,en);

}
int main()
{
    ios::sync_with_stdio(false);
    ///inf;
    //outf;
    int i,j,n,m;

    cin>>n;
    FOR(i,0,n-1) cin>>ar[i];
    sort(ar,ar+n,greater<int>());
    FOR(i,0,n-1) c+=ar[i],sum[i]=c;

    cout<<rec(0,n-1);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define REV(i,j,k) for(int i=j;i>=k;i--)
#define FORR(i,j,k,l) for(int i=j;i<=k;i+=l)
#define inf         freopen("in.txt", "r", stdin)
#define outf        freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c)    scanf("%d %d %d", &a, &b, &c)
#define clean(mat,n)   FOR(i,0,n) mat[i].clear()
#define minn          (long long)-1000000000000000000
#define maxx          (long long) 1000000000000000000
#define mod          1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define LL           long long
#define NL '\n'
#define cnd tree[idx]
#define lnd (idx<<1)
#define rnd ((idx<<1)+1)
#define PB push_back
#define F first
#define S second
#define MAX 100010
#define SZ 100010
#define MP make_pair
#define valid(nx,ny)  ((nx >= 0) && (nx < row) && (ny >= 0) && (ny < col))
typedef pair<int,int> pr;

int ar[MAX];
LL val,c;
int main()
{
    ios::sync_with_stdio(false);
    //inf;
    //outf;
    int i,j,n,m;

    cin>>n;
    FOR(i,0,n-1)
    {
        cin>>ar[i];
        c+=ar[i];
    }

    i=n-1;
    while(i>0)
    {
       val+=c;
       c-=ar[i];i--;
       if(i>=0)c-=ar[i];
       i--;
    }

    cout<<val<<NL;
    return 0;
}


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

int main()
{
    int i,j,inr=0,inc=0;
    map<int,int>row;
    map<int,int>col;
    char ch;
    int n,m;
    cin>>n>>m;
    char a[n+1][m+1];
    int sum=0;
    int maxx=-1;
    int gd=0;

    for(i=1;i<=n;i++)
    { sum=0;
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='*')
            {

                row[i]++;
                col[j]++;

                gd++;
            }

        }

    }

map<int,int>::iterator it=row.begin();
map<int,int>::iterator itt=col.begin();
for(it=row.begin();it!=row.end();it++)
{
    for(itt=col.begin();itt!=col.end();itt++)
    {
        if(it->second+itt->second-1==gd&&gd!=0)
        {
            inr=it->first;
            inc=itt->first;
            break;
        }
    }
}
if(!inr&&!inc)
{
    cout<<"NO";
}
else
{

cout<<"YES"<<endl<<inr<<" "<<inc;
}

    return 0;
}


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

int main()
{
    int i,j,inr=0,inc=0;
    map<int,int>row;
    map<int,int>col;
    char ch;
    int n,m;
    cin>>n>>m;
    char a[n+1][m+1];
    int sum=0;
    int maxx=-1;
    int gd=0;

    for(i=1;i<=n;i++)
    { sum=0;
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='*')
            {
                row[i]++;
                col[j]++;
                gd++;
            }

        }

    }

map<int,int>::iterator it=row.begin();
map<int,int>::iterator itt=col.begin();
for(it=row.begin();it!=row.end();it++)
{
    for(itt=col.begin();itt!=col.end();itt++)
    {
        if(it->second+itt->second>=gd)
        {
            inr=it->first;
            inc=itt->first;
            break;
        }
    }
}
if(!inr&&!inc)
{
    cout<<"NO";
}
else
{

cout<<"YES"<<endl<<inr<<" "<<inc;
}

    return 0;
}


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

int main()
{
    int i,j,inr=0,inc=0;
    map<int,int>row;
    map<int,int>col;
    char ch;
    int n,m;
    cin>>n>>m;
    char a[n+1][m+1];
    int sum=0;
    int maxx=-1;
    int gd=0;

    for(i=1;i<=n;i++)
    { sum=0;
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='*')
            {

                row[i]++;
                col[j]++;

                gd++;
            }

        }

    }
    int key=0;

map<int,int>::iterator it=row.begin();
map<int,int>::iterator itt=col.begin();
for(it=row.begin();it!=row.end();it++)
{
    key=0;
    for(itt=col.begin();itt!=col.end();itt++)
    {
        if(a[it->first][itt->first]=='*')
            key=1;
        else
            key=0;

        if(it->second+itt->second-key==gd)
        {
            inr=it->first;
            inc=itt->first;
        cout<<"YES"<<endl<<inr<<" "<<inc;
        return 0;

        }
    }
}
if(!inr&&!inc&&gd!=0)
{
    cout<<"NO";
}
else if(gd==0)
cout<<"YES"<<endl<<"1"<<" "<<"1";

    return 0;
}


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

int main()
{
    int i,j,inr=0,inc=0;
    map<int,int>row;
    map<int,int>col;
    char ch;
    int n,m;
    cin>>n>>m;
    char a[n+1][m+1];
    int sum=0;
    int maxx=-1;
    int gd=0;

    for(i=1;i<=n;i++)
    { sum=0;
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='*')
            {

                row[i]++;
                col[j]++;

                gd++;
            }

        }

    }
    int key=0;

map<int,int>::iterator it=row.begin();
map<int,int>::iterator itt=col.begin();
for(it=row.begin();it!=row.end();it++)
{
    key=0;
    for(itt=col.begin();itt!=col.end();itt++)
    {
        if(a[it->first][itt->first]=='*')
            key=1;
        else
            key=0;

        if(it->second+itt->second-key==gd)
        {
            inr=it->first;
            inc=itt->first;
            break;
        }
    }
}
if(!inr&&!inc&&gd!=0)
{
    cout<<"NO";
}
else if(gd==0)
cout<<"YES"<<endl<<"1"<<" "<<"1";
else
{

cout<<"YES"<<endl<<inr<<" "<<inc;
}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
char c[maxn][maxn];
int vis[maxn],vis1[maxn];
int main()
{
    int n,m,num=0,k=0,k2=0,s2=0,f=1;
    cin >>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >>c[i][j];
            if(c[i][j]=='*')
            {
                vis[k++]=j;
            }
        }
    }
    if(k==0)
        f=0;
    s2=vis[0];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(c[i][j]=='*'&&j!=s2)
            {
                 vis1[k2++]=i;
            }
        }
    }
    sort(vis1,vis1+k2);
    for(int i=0;i<k2-1;i++)
    {
        if(vis1[i]!=vis1[i+1])
        {
            f=0;
            break;
        }
        else
            f=1;
    }
    if(f)
        cout <<"YES"<<endl<<vis1[0]+1<<' '<<s2+1<<endl;
    else
        cout <<"NO"<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
char c[maxn][maxn];
int vis[maxn],vis1[maxn];
int main()
{
    int n,m,num=0,k=0,k2=0,s2=0,f=1;
    cin >>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin >>c[i][j];
            if(c[i][j]=='*')
            {
                vis[k++]=j;
            }
        }
    }
    s2=vis[0];
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(c[i][j]=='*'&&j!=s2)
            {
                 vis1[k2++]=i;
            }
        }
    }
    sort(vis1,vis1+k2);
    for(int i=0;i<k2-1;i++)
    {
        if(vis1[i]!=vis1[i+1])
        {
            f=0;
            break;
        }
        else
            f=1;
    }
    if(f)
        cout <<"YES"<<endl<<vis1[0]<<' '<<s2<<endl;
    else
        cout <<"NO"<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
char c[maxn][maxn];
int vis[maxn],vis1[maxn];
int main()
{
    int n,m,num=0,k=0,k2=0,s2=0,f=1;
    cin >>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >>c[i][j];
            if(c[i][j]=='*')
            {
                vis[k++]=j;
            }
        }
    }
    s2=vis[0];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(c[i][j]=='*'&&j!=s2)
            {
                 vis1[k2++]=i;
            }
        }
    }
    sort(vis1,vis1+k2);
    for(int i=0;i<k2-1;i++)
    {
        if(vis1[i]!=vis1[i+1])
        {
            f=0;
            break;
        }
        else
            f=1;
    }
    if(f)
        cout <<"YES"<<endl<<vis1[0]+1<<' '<<s2+1<<endl;
    else
        cout <<"NO"<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
char c[maxn][maxn];
int vis[maxn],vis1[maxn];
int main()
{
    int n,m,num=0,k=0,k2=0,s2=0,f=1;
    cin >>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >>c[i][j];
            if(c[i][j]=='*')
            {
                vis[k++]=j;
            }
        }
    }
    if(k==0&&n!=m)
        f=0;
    s2=vis[0];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(c[i][j]=='*'&&j!=s2)
            {
                 vis1[k2++]=i;
            }
        }
    }
    sort(vis1,vis1+k2);
    for(int i=0;i<k2-1;i++)
    {
        if(vis1[i]!=vis1[i+1])
        {
            f=0;
            break;
        }
        else
            f=1;
    }
    if(f)
        cout <<"YES"<<endl<<vis1[0]+1<<' '<<s2+1<<endl;
    else
        cout <<"NO"<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
char c[maxn][maxn];
int vis[maxn],vis1[maxn];
int main()
{
    int n,m,num=0,k=0,k2=0,s2=0,f=1;
    cin >>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >>c[i][j];
            if(c[i][j]=='*')
            {
                vis[k++]=j;
            }
        }
    }
    if(k==0&&n!=m&&n!=1&&m!=1)
        f=0;
    s2=vis[0];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(c[i][j]=='*'&&j!=s2)
            {
                 vis1[k2++]=i;
            }
        }
    }
    sort(vis1,vis1+k2);
    for(int i=0;i<k2-1;i++)
    {
        if(vis1[i]!=vis1[i+1])
        {
            f=0;
            break;
        }
        else
            f=1;
    }
    if(f)
        cout <<"YES"<<endl<<vis1[0]+1<<' '<<s2+1<<endl;
    else
        cout <<"NO"<<endl;
    return 0;
}