#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long MAX = 5100000; const long long INF = 1LL << 60; const long long mod = 1000000007LL; //const long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; ll N; ll a[200500]; ll sum[200500]; int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ scanf("%lld", &N); for (ll i = 0; i < N; i++) scanf("%lld", &a[i]); sort(a, a + N); if (N == 2) { cout << a[1] - a[0] << endl; return 0; } ll dp[250000]; for (ll i = 0; i < 250000; i++) dp[i] = INF; dp[2] = a[1] - a[0]; dp[3] = a[2] - a[0]; for (ll i = 4; i <= N; i++) { ll id = i - 1; chmin(dp[i], dp[i - 2] + a[id] - a[id - 1]); chmin(dp[i], dp[i - 3] + a[id] - a[id - 2]); } cout << dp[N] << endl; return 0; }