#include<stdio.h>

#define MAX 1000
#define MAXWEIGHTIJ 20000

struct MCS {
	int cost;
	int sorder;
	int order;
};


void exchange( struct MCS *pa, struct MCS *pb )
{
   	struct MCS t;
     
	t=*pa;
	*pa=*pb;
	*pb=t;
	 
	 return;
}
 
int Partition( struct MCS A[], int p, int r )
{
    struct MCS x;
    int i,j;
 
    x=A[r];
    i=p-1;
    for( j=p; j<r; j++ ) {
        if( A[j].cost < x.cost ) {
            i++;
            exchange( &A[i], &A[j] );
         }
    }
    exchange( &A[i+1], &A[r] );

    return i+1;
}
void QuickSort( struct MCS A[], int p, int r )
{
    int q;
     
    if( p < r ) {
        q=Partition( A, p, r );
        QuickSort( A, p, q-1 );
        QuickSort( A, q+1, r );
    }
    return;
}

int tryChange( int n, struct MCS W[] )
{
	int cost,miniCost;
	int i;
	int maxi,mini;
	int max;
	int min,mostmin;
	int minorder,maxorder,mostminorder;
	int limitN;
	
	maxi=mini=0;
	miniCost=cost=MAXWEIGHTIJ;
	limitN=n;	
	maxorder=max=-1;
	mostmin=min=mostminorder=minorder=n;
    while( 1 ) {
		for( i=0; i<limitN; i++ ) {
			if( i != W[i].order ) {
				if( max < W[i].sorder ) {
    				max=W[i].sorder;
    				maxorder=i;
    			}
				if( W[i].sorder < min ) {
					min=W[i].sorder;
					minorder=i;	
				}
    			
			}
			if( W[i].sorder < mostmin ) {
				mostmin=W[i].sorder;
				mostminorder=i;	
			}
		}
		if( -1 == maxorder ) { break; }
		
		if( W[maxorder].order != minorder ) {
			if( 2*(W[minorder].cost+W[mostminorder].cost)  < (W[maxorder].cost+W[minorder].cost)  ) {
				minorder=mostminorder;
			}
		}
   		if( W[maxorder].order == minorder ) { 
			cost=( W[minorder].cost+W[maxorder].cost );
			miniCost=cost;
			mini=minorder;
			maxi=maxorder;
			break;
		}
		limitN--;
		max=-1;
		if( -1 == limitN ) {
			if( MAXWEIGHTIJ == miniCost ) {
				cost=( W[minorder].cost+W[maxorder].cost );
				miniCost=cost;
				mini=minorder;
				maxi=maxorder;
			}
			break;
		}
    }



    if( MAXWEIGHTIJ != miniCost ) {
		exchange( &W[mini], &W[maxi] );
	} else {
    
    	miniCost=0;
    }

	return miniCost;
}

int main( void ) 
{   
    int n,i;
    struct MCS W[MAX+1];
    int cost;
        
    scanf( "%d", &n );
    for( i=0; i<n; i++ ) {
        scanf( "%d", &W[i].cost );
        W[i].order=i;
    }

    QuickSort( W, 0, n-1 ); 
     for( i=0; i<n; i++ ) {
        W[i].sorder=i;
    }
   cost=i=0;
    while( (i = tryChange( n, W ) ) ) {
   		cost+=i;
    }
   	printf( "%d\n", cost );

    return 0;
}