#include #include #include using lint = long long; constexpr lint INF = 1LL << 60; void solve() { int n; std::cin >> n; std::vector xs(n); for (auto& x : xs) std::cin >> x; std::sort(xs.begin(), xs.end()); std::vector dp(n + 1, INF); dp[0] = 0; for (int i = 1; i < n; ++i) { for (int k = 1; k <= 2 && i - k >= 0; ++k) { lint cost = xs[i] - xs[i - k]; dp[i + 1] = std::min(dp[i + 1], dp[i - k] + cost); } } std::cout << dp[n] << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }