/*
TASK:ots
LANG:C++
*/
#include<iostream>
#include<stdio.h>
using namespace std;
  int cmp( const void *a, const void *b)
  {
     if(*(int *)a>*(int *)b) return 1;
  return 0;
  }
int main()
{
    long long n,a,b,c,x,y,z,t=0,br=0,i,q=0;
    long s[99999];
//    scanf("%lld",&n);
    //cin>>n;
    //cout<<n;
      cin>>n;
    i=0;
    //for(i=0;i<5;i++) cout<<i;
    for (i=0;i<n;i++)
        scanf("%ld",&s[i]);

    qsort(s,n,sizeof(s[0]),cmp);
    if(n==0||n==1){cout<<"0"<<endl; return 0;}
    if(n==2){x=s[q];q++;y=s[q];q++; cout<<y-x<<endl;return 0;}
    if(n==3){x=s[q];q++;y=s[q];q++;z=s[q];q++; cout<<z-x<<endl;return 0;}
    
    x=s[q];q++;y=s[q];q++;
//   scanf("%lld %lld",&x,&y);
    br+=y-x;
    x=y;
    t=1;
    y=s[q];q++;
//    scanf("%lld",&y);
    a=y-x;
    x=y;
    
    for (i=4;i<n;i++)
        {
        y=s[q];q++;
//        scanf("%lld",&y);
        b=y-x;
        if (a<b){if(t!=2){br+=a;t=1;}}
           else {br+=b;t=2;}
        x=y;
        a=b;
        }
      y=s[q];q++;
//    scanf("%lld",&y);
        b=y-x;
    br+=b;
    cout<<br<<endl;
    return 0;
}
