#include using namespace std; using ll = long long; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b> N; vector A(N); FOR(i, 0, N){ cin >> A.at(i); } sort(ALL(A)); dp[0] = inf; dp[1] = A[1]-A[0]; dp[2] = A[2]-A[0]; FOR(i, 3, N){ dp[i] = inf; // k = 2 dp[i] = dp[i-2] + A[i]-A[i-1]; // k = 3 ll a = inf; if(i>3){ a = dp[i-3] + A[i] - A[i-2]; } chmin(dp[i], a); } p(dp[N-1]); return 0; }