#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, a[200100];
map<ll, int> m;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (m.find(a[i]) == m.end()) {
      if (i == 0) {
        ans += a[i + 1] - a[i];
        m[a[i + 1]]++;
      } else if (i == n - 1) {
        int x = a[n - 2];
        if (m[x] == 2)
          ans += 2 * (a[n - 1] - x);
        else
          ans += a[n - 1] - x;
      } else {
        int x = m[a[i - 1]];
        if (x * a[i] - x * a[i - 1] < a[i + 1] - a[i]) {
          ans += a[i] - x * a[i - 1];
          m[a[i]] = 1 + x;
        } else {
          ans += a[i] - a[i - 1];
          m[a[i]]++;
        }
      }
    } else
      m[a[i]]++;
  }
  cout << ans;
  return 0;
}
