#include #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 200005 int main() { int n; cin >> n; ll a[n]; rep(i,n) cin >> a[i]; sort(a,a+n); ll dp[n+1] = {}; ll INF = 1001001001001001001; rep(i,n+1)dp[i] = INF; dp[0] = 0; srep(i,2,n+1){ dp[i] = dp[i-2] + a[i-1] - a[i-2]; if(i >= 3){ dp[i] = min(dp[i], dp[i-3]+a[i-1]-a[i-3]); } } cout << dp[n] << endl; return 0; }