﻿#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <iomanip>
#include <math.h>
#include <fstream>
#include <utility>

using namespace std;

#define ffor(i,j,n) for(long i=(j);i<((long)n);++i)
#define fforr(k,a,b) for(long k=b-1; k>=a;k--)
#define all(v) v.begin(), v.end()
#define ll long long int
#define MOD 1000000007
#define PRECMB  for (int i = 1; i <= 1000000; i++) {fac[i] = fac[i - 1] * i % MOD; inv[i] = mp(fac[i], MOD - 2);}

ll fac[1001] = { 1 }, inv[1001] = { 1 };
//ll fac[1000001] = { 1 }, inv[1000001] = { 1 };
//ll fac[200001] = { 1 }, inv[1] = { 1 };
ll mcd(ll a, ll b) { if (b == 0) return a; return mcd(b, a % b); }
ll mcm(ll a, ll b) { return (a / mcd(a, b)) * b; }
ll mp(ll a, ll b) { ll ret = 1; while (b) { if (b & 1)ret = ret * a % MOD; a = a * a % MOD; b >>= 1; }return ret; }
ll cmb(ll r, ll c) { return (c > r || c < 0) ? 0 : fac[r] * inv[c] % MOD * inv[r - c] % MOD; } // Num combinatori

ll mcde(ll a, ll b, ll* x, ll* y)
{
    if (a == 0) { *x = 0; *y = 1; return b; }
    ll x1, y1; ll mcd = mcde(b % a, a, &x1, &y1);
    *x = y1 - (b / a) * x1; *y = x1;
    return mcd;
}
ll modI(ll a, ll m) // obtenir resta de 1/a mod m
{
    ll m0 = m, y = 0, x = 1;
    if (m == 1) return 0;
    while (a > 1) {
        ll q = a / m, t = m;
        m = a % m; a = t; t = y; y = x - q * y; x = t;
    }
    if (x < 0) x += m0;
    return x;
}

typedef std::vector<ll> ints;

#define itt vector<ll>::iterator
#define insertaO(v,val) v.insert(lower_bound(all(v), val), val)
#define trobatO(v, val) binary_search(all(v), val)
#define trobaO(it, v, val, trobat) itt it = lower_bound(all(v), val); bool trobat=it != v.end() && *it == val

// troba l'últim element menor o igual a val en v, un vector ordenat. Presuposa que aquest element existeix...
#define trobaMO(it, v, val) itt it = lower_bound(all(v), val); if (it == v.end()) it--; else if (*it > val) it--;

// funciona?
#define trobamO(it, v, val) itt it = upper_bound(all(v), val, less_equal<ll>()); bool trobat=it != v.end() && *it <=val

bool sortbysec(const pair<int, int>& a, const pair<int, int>& b) { return (a.second < b.second); }

struct Cell {
    ll x0, x1, y0, y1, val;
    bool marcat;
};
bool cmpt(Cell i1, Cell i2)
{
    return (i1.val < i2.val);
}

struct Tipus {
    ll r, c;
    ll b;
    //    Tipus& operator<(const Tipus& obj) { return b < obj.b; }
};
bool operator<(const Tipus& s1, const Tipus& s2) {
    return s1.b < s2.b;
}

vector <ll> primers;
void trobaprimers() {
    ffor(i, 2, 500) {
        bool primer = true;
        ffor(j, 0, primers.size()) {
            if (i % primers[j] == 0) primer = false;
        }
        if (primer) primers.push_back(i);
    }
}
int numdigits(ll num) {
    ll cmpt = 0;
    while (num > 0) {
        cmpt++;
        num /= 10;
    }
    return cmpt;
}



//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void fesho(int T) {
    ll n, ai;
    
    vector <ll> vp, vv;

    vector <pair <int, int>> VV;
    

    cin >> n;
    vector <vector<ll>> dp(n + 2, vector<ll>(n + 2));

    ffor(i, 0, n) {
        cin >> ai;
        vp.push_back(ai);
    }

    sort(all(vp));

    ffor(i, 1, n) {
        ffor(j, 0, n-i) {
            dp[j][j + i] = vp[j+i] - vp[j] + min(dp[j + 1][j + i], dp[j][ j + i - 1]);
        }
    }

    cout << dp[0][n-1] << endl;


    return;
}


int main() {
    long int T;


    T = 1; // cin >> T;

    //trobaprimers();
    for (int i = 0; i < T; i++) {
        fesho(i + 1);
    }

    return 0;
}
