#include <iostream>
#include <fstream>
#include <cmath>  
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <numeric>
#include <functional>
#include <string> 
#include <vector>
#include <bitset>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>

using namespace std;
using ll = long long;

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i, m, n) for(int i = int(m);i < int(n);i++)
#define ALL(obj) (obj).begin(),(obj).end()

const ll MOD = (ll)1e9 + 7;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;

vector<int> dy = { 0,0,1,-1 };
vector<int> dx = { 1,-1,0,0 };

template<class T> void corner(bool flg, T hoge) {
	if (flg) {
		cout << hoge << endl;
		exit(0);
	}
	else return;
}

template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {
	o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;
}

template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {
	o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;
}

template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {
	o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;
}

template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {
	o << "{" << obj.first << ", " << obj.second << "}"; return o;
}

template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {
	o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;
}

void print(void) {
	cout << endl;
}

template <class Head> void print(Head&& head) {
	cout << head;
	print();
}

template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {
	cout << head << " ";
	print(forward<Tail>(tail)...);
}

void YN(bool flg) {
	cout << ((flg) ? "YES" : "NO") << endl;
}

void Yn(bool flg) {
	cout << ((flg) ? "Yes" : "No") << endl;
}

void yn(bool flg) {
	cout << ((flg) ? "yes" : "no") << endl;
}

int main() {
	int N; cin >> N;
	V<ll> A(N); REP(i, N) cin >> A[i];
	sort(ALL(A));

	set<int> st1;
	deque<ll> d1;

	d1.push_back(A[0]);
	st1.insert(0);
	int l = 1, r = N - 1;
	for(int i = 0;;++i){
		if (i%2 == 0){
			if (st1.insert(r).second) d1.push_back(A[r]);
			else break;
			r--;
			if (st1.insert(r).second) d1.push_front(A[r]);
			else break;
			r--;
		}
		if (i % 2 == 1) {
			if (st1.insert(l).second) d1.push_back(A[l]);
			else break;
			l++;
			if (st1.insert(l).second) d1.push_front(A[l]);
			else break;
			l++;
		}
	}
	ll ans1 = 0;
	REP(i, N - 1) ans1 += abs(d1[i] - d1[i + 1]);
	

	set<int> st2;
	deque<ll> d2;

	d2.push_back(A[N-1]);
	st2.insert(N-1);
	l = 0, r = N - 2;
	for (int i = 0;; ++i) {
		if (i % 2 == 1) {
			if (st2.insert(r).second) d2.push_back(A[r]);
			else break;
			r--;
			if (st2.insert(r).second) d2.push_front(A[r]);
			else break;
			r--;
		}
		if (i % 2 == 0) {
			if (st2.insert(l).second) d2.push_back(A[l]);
			else break;
			l++;
			if (st2.insert(l).second) d2.push_front(A[l]);
			else break;
			l++;
		}
	}
	ll ans2 = 0;
	REP(i, N - 1) ans2 += abs(d2[i] - d2[i + 1]);

	cout << max(ans1, ans2) << endl;
	

	return 0;
}



Measured memory_bytes: 12032