#include #define FI first #define SE second #define PB push_back #define ll long long #define FOR(i,a,b) for(int i=a;i=a;--i) #define YES(i) cout<<(i?"YES":"NO")< #define vint vector using namespace std; const int INF=1e9+7; const ll LINF=1e18; const int MOD=1e9+7; //const int MOD=998244353; ll y[100000],dp[100000]; int main(){ int n; cin>>n; assert(n>=2 && n<=100000); FOR(i,0,n){ cin>>y[i]; assert(-1e9<=y[i] && y[i]<=1e9); } sort(y,y+n); dp[0]=LINF; dp[1]=y[1]-y[0]; dp[2]=y[2]-y[0]; FOR(i,3,n){ dp[i]=min(dp[i-3]+y[i]-y[i-2],dp[i-2]+y[i]-y[i-1]); } co(dp[n-1]); }