#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

#define endl "\n"
#define int long long
int64_t lol;

using namespace std;

int dp[2005][2005];
vector<int> s;

int sub(int l, int r) {
        if (dp[l][r] != -1) return dp[l][r];
        if (l == r) return 0;
        return dp[l][r] = (s[r] - s[l] + min(sub(l, r - 1), sub(l + 1, r)));
}

void Seeyouagain() {
        int n;
        cin >> n;
        s.resize(n);
        for (int i = 0; i < n; ++i) cin >> s[i];
        sort(s.begin(), s.end());
        memset(dp, -1, sizeof dp);
        cout << sub(0, n - 1);
}

signed main() {
        ios_base::sync_with_stdio(!cin.tie(nullptr));
        int64_t testc = 1;
        // cin >> testc;
        while (testc--) {
                Seeyouagain();
                cout << endl;
        }
        return 0;
}