#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(a);i>(b);i--) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; typedef pair P; 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; } templatevoid Fill(A(&array)[N],const T &val){fill((T*)array, (T*)(array+N), val);} const int inf = INT_MAX / 2; const ll INF = LLONG_MAX / 2; //template end int main(){ int n; scanf("%d",&n); vector a(n); rep(i,0,n)scanf("%d",&a[i]); sort(ALL(a)); if(n<=3){ if(n==2)printf("%d\n",a[1]-a[0]); if(n==3)printf("%d\n",a[2]-a[0]); return 0; } int dp[100010]; Fill(dp,inf); dp[1]=a[1]-a[0],dp[2]=a[2]-a[0]; rep(i,3,n)rep(k,2,4){ chmin(dp[i],dp[i-k]+a[i]-a[i-k+1]); } printf("%d\n",dp[n-1]); return 0; }