#include<bits/stdc++.h>
using namespace std;
#define Go_ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long int
#define llu long long unsigned int
#define pii pair<int ,int>
#define PII pair<ll ,ll>

const int Max=1e9+100;
const ll  MAX=1e14+500;
const ll sz=2e5+100;
ll n;
vector<ll> test;

ll dp[2008][2008];

ll cholo(ll l, ll r)
{
    //cout<<l<<" "<<r<<endl;
    if(l<=0 && r>n) return 0ll;

    ll mx=MAX;

    ll &ret=dp[l][r];
    if(~ret) return ret;
    
    if(l<=0) mx=(test[r]-test[1]) + cholo(l,r+1);
    else if(r>n) mx=(test[n]-test[l]) + cholo(l-1,r);
    else{
        mx=(test[r-1]-test[l]) + cholo(l-1,r);
        mx=min(mx, (test[r]-test[l+1]) + cholo(l,r+1));
    }

    return (ret = mx);
}

int main()
{
    Go_
    ll a,b,c,i,j,k,q,p,x,y,ct,ct1,m,l,r,x1,y1,mn,h,sum1,in,z,mid,mx;
    char ch;
    double d;
    string str1,str2,str;
    bool bl,bl1;
    int t,cs=1;
    
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;
        test.push_back(x);
    }
    
    test.push_back(0);
    sort(test.begin(), test.end());
    
    ll ans=MAX;
    memset(dp, -1, sizeof dp);
    
    for(int i=1;i<=n;i++){
        ans=min(ans, cholo(i-1,i+1));
    }

    cout<<ans<<'\n';
    

   
}
