/*
TASK:ots
LANG:C++
*/
#include<iostream>
using namespace std;
int main()
{
    int n,lenght=0,max=0,x=0,del;
    cin>>n;
    long points[n];bool there[n];
    for(int i=0;i<n;i++){there[i]=false;}
    for(int i=0;i<n;i++){cin>>points[i];}
    sort(points,points+n);
    if(n==1){cout<<"0";}
    if(n==2){cout<<points[1]-points[0];}
    if(n==3){cout<<(points[1]-points[0])+(points[2]-points[1]);}
    else
    {
    for(int z=1;z<n;z++){lenght=lenght+(points[z]-points[z-1]);}
    if(n%2==0){del=(n+1)/3;}
    else if(n%2!=0){del=n/3;}
    while(x<del)
    {max=0;
    for(int i=2;i<n-1;i++){if(points[i]-points[i-1]>max&&(there[i]==false||there[i-1]==false)){max=points[i]-points[i-1];there[i]=true;there[i-1]=true;};}
    lenght=lenght-max;
    x++;}cout<<lenght;};
    cout<<endl;
}
    
