#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

int n;
int y[100000];

long long calc(int m) {
  long long ret = 0;
  for (int i = 0; i < m; i++)
    ret += abs(y[i] - y[m/2]);
  for (int i = m; i < n; i++)
    ret += abs(y[i] - y[n-1-(n-m)/2]);
  return ret;
}

long long solve() {
  sort(y, y+n);

  set<int> st;
  REP (i, n) st.insert(y[i]);
  if (st.size() == 2) return 0;
  if (st.size() == 1) return 1;

  int lo = 1;
  int hi = n;
  while (hi - lo > 10) {
    int m1 = (2 * lo + hi) / 3;
    int m2 = (lo + 2 * hi) / 3;
    if (calc(m1) < calc(m2))
      hi = m2;
    else
      lo = m1;
  }

  long long ret = calc(lo);
  for (int i = lo+1; i < hi; i++)
    ret = min(ret, calc(i));
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  REP (i, n) cin >> y[i];
  cout << solve() << endl;

  return 0;
}