#include<bits/stdc++.h>

#define fi first
#define se second
#define show(x) cerr << #x << "=" << "\n"
typedef long long ll;
typedef unsigned long long ull;

using namespace std;

int n;
int a[100001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  FILE *stream2;
  stream1 = freopen("in.txt", "r", stdin);
  stream2 = freopen("out.txt", "w", stdout);
  if (stream1 == NULL) return 0;
  if (stream2 == NULL) return 0;
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  deque<int> s;
  s.push_front(a[0]);
  s.push_back(a[n - 1]);
  if (n > 2) s.push_front(a[n - 2]);
  int start = 1, end = n - 3;
  bool big = 0;
  while (start <= end) {
    //cout << s << endl;
    int f = s.front();
    int l = s.back();
    if (end - start > 0) {
      if (big) {
        if (abs(f - a[end]) + abs(l - a[end - 1]) >= abs(f - a[end - 1]) + abs(l - a[end])) {
          s.push_back(a[end - 1]);
          s.push_front(a[end]);
          end -= 2;
        } else {
          s.push_back(a[end]);
          s.push_front(a[end - 1]);
          end -= 2;
        }
        big = 0;
      } else {
        //cout << a[start] << " " << a[start + 1] << endl;
        if (abs(f - a[start]) + abs(l - a[start + 1]) >= abs(f - a[start + 1]) + abs(l - a[start])) {
          s.push_back(a[start + 1]);
          s.push_front(a[start]);
        } else {
          s.push_back(a[start]);
          s.push_front(a[start + 1]);
        }
        start += 2;
        big = 1;
      }
      //cout << s << endl;
    } else {
      if (abs(f - a[start]) >= abs(l - a[start])) {
        s.push_front(a[start]);
      } else {
        s.push_back(a[start]);
      }
      start++;
    }
  }
  deque<int> t;
  t.push_front(a[n - 1]);
  t.push_back(a[0]);
  if (n > 2) t.push_front(a[1]);
  start = 2, end = n - 2;
  big = 1;
  while (start <= end) {
    int f = t.front();
    int l = t.back();
    if (end - start > 0) {
      if (big) {
        if (abs(f - a[end]) + abs(l - a[end - 1]) >= abs(f - a[end - 1]) + abs(l - a[end])) {
          t.push_back(a[end - 1]);
          t.push_front(a[end]);
          end -= 2;
        } else {
          t.push_back(a[end]);
          t.push_front(a[end - 1]);
          end -= 2;
        }
        big = 0;
      } else {
        if (abs(f - a[start]) + abs(l - a[start + 1]) >= abs(f - a[start + 1]) + abs(l - a[start])) {
          t.push_back(a[start + 1]);
          t.push_front(a[start]);
        } else {
          t.push_back(a[start]);
          t.push_front(a[start + 1]);
        }
        start += 2;
        big = 1;
      }
    } else {
      if (abs(f - a[start]) >= abs(l - a[start])) {
        t.push_front(a[start]);
      } else {
        t.push_back(a[start]);
      }
      start++;
    }
  }
  //cout << s << endl;
  //cout << t << endl;
  ll ans = 0;
  for (int i = 1; i < (int)s.size(); i++) {
    ans += abs(s[i] - s[i - 1]);
  }
  ll ans2 = 0;
  for (int i = 1; i < (int)s.size(); i++) {
    ans2 += abs(t[i] - t[i - 1]);
  }
  cout << max(ans, ans2) << endl;
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
  fclose(stream1);
  fclose(stream2);
#endif
  return 0;
}

Measured memory_bytes: 1536