#include #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) using ll = long long; using namespace std; template inline void chmin(T & a, U const & b) { a = min(a, b); } ll solve(int n, vector & a) { map f; for (ll a_i : a) { ++ f[a_i]; } vector dp(f.size() + 1, LLONG_MAX / 10); dp[0] = 0; int i = 0; for (auto it = f.begin(); it != f.end(); ++ it, ++ i) { if (i >= 1) { chmin(dp[i + 1], dp[i - 1] + (it->first - prev(it)->first) * it->second); chmin(dp[i + 1], dp[i - 1] + (it->first - prev(it)->first) * prev(it)->second); } if (i >= 2) { chmin(dp[i + 1], dp[i - 2] + (it->first - prev(it)->first) * prev(it)->second + (it->first - prev(prev(it))->first) * prev(prev(it))->second); chmin(dp[i + 1], dp[i - 2] + (it->first - prev(it)->first) * it->second + (prev(it)->first - prev(prev(it))->first) * prev(prev(it))->second); chmin(dp[i + 1], dp[i - 2] + (it->first - prev(prev(it))->first) * it->second + (prev(it)->first - prev(prev(it))->first) * prev(it)->second); } } return dp[f.size()]; } int main() { int n; cin >> n; vector a(n); REP (i, n) { cin >> a[i]; } cout << solve(n, a) << endl; return 0; }