
/*
ID: C055
TASK: ots
LANG: C++
*/

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

int N, points[1048576], end[1048576];

struct line
{
    int len, iBegin, iEnd;
    
    inline bool operator < ( const line &obj ) const
    {
        return( len > obj.len );
    }   
    
} lines[1048576];

void input()
{
    scanf( "%d", &N );
    
    for( int i = 0; i < N; ++i ) 
        scanf( "%d", &points[i] );
}

void solve()
{
    /* GREEDY! */ 
     
    sort( points, points + N );
    
    for( int i = 1; i < N-1; ++i ) end[i] = 2;
    end[0] = end[N-1] = 1;
    
    
    for( int i = 0; i < N-1; ++i )
    {
        lines[i].iBegin = i;
        lines[i].iEnd = i+1;
        
        lines[i].len = points[i+1] - points[i];
    }
    
    sort( lines, lines + N-1 );
    
    int length = points[N-1] - points[0];
    
    for( int i = 0; i < N-1; ++i )
    {
        if( end[ lines[i].iBegin ] == 2 && end[ lines[i].iEnd ] == 2 )
        {
            length -= lines[i].len;
            end[ lines[i].iBegin ]--;
            end[ lines[i].iEnd ]--;
        }
    }
    
    printf( "%d\n", length );
}

int main()
{
    input();
    solve();
    
    
    return 0;
}
