#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using i64 = int64_t; using i32 = int32_t; template void init_n(vector& v, size_t n, U x) { v = vector(n, x); } template void init_n(vector& v, size_t n) { init_n(v, n, T()); } template void read_n(vector& v, size_t n, size_t o = 0) { v = vector(n+o); for (size_t i=o; i> v[i]; } template void read_n(T a[], size_t n, size_t o = 0) { for (size_t i=o; i> a[i]; } template T gabs(const T& x) { return max(x, -x); } #define abs gabs i64 n; vector y, dp[3]; int main() { cin >> n; read_n(y, n); sort(begin(y), end(y)); y.insert(begin(y), 0); for (i64 i = 0; i < 3; ++i) init_n(dp[i], n + 1); dp[1][1] = dp[2][1] = 1ll << 50; for (i64 i = 1; i <= n; ++i) { if (i < n) { dp[0][i] = min(dp[1][i - 1], dp[2][i - 1]) + y[i + 1] - y[i]; // to succ } if (i > 1) { dp[1][i] = dp[0][i - 1]; // stay dp[2][i] = dp[1][i - 1] + y[i] - y[i - 1]; // to prev } } cout << min(dp[1][n], dp[2][n]) << '\n'; return 0; }