#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */


ll solve(){
    ll res = 0;
    ll N; cin >> N;
    vector<ll> A(N); for(auto& in:A) cin >> in;
    sort(A.begin(),A.end());
    { // 1
        deque<ll> deq(A.begin(),A.end());
        deque<ll> T;
        T.push_back(deq.back()); deq.pop_back();
        ll cnt = 0;
        while(deq.size()){
            ll L = T.front(),R = T.back();
            ll V;
            if(cnt%2==0){
                V = deq.front(); deq.pop_front();
            }else{
                V = deq.back(); deq.pop_back();
            }
            cnt++;
            if(abs(L-V) > abs(R-V)){
                T.push_front(V);
            }else{
                T.push_back(V);
            }
        }
        ll now = T.front(); T.pop_front();
        ll cost = 0;
        while(T.size()){
            cost += abs(T.front() - now);
            now = T.front(); T.pop_front();
        }
        res = max(res,cost);
    }
    { // 2
        deque<ll> deq(A.begin(),A.end());
        deque<ll> T;
        T.push_back(deq.back()); deq.pop_back();
        ll cnt = 0;
        while(deq.size()){
            ll L = T.front(),R = T.back();
            ll V;
            if(cnt%2==0){
                V = deq.front(); deq.pop_front();
            }else{
                V = deq.back(); deq.pop_back();
            }
            cnt++;
            if(abs(L-V) >= abs(R-V)){
                T.push_front(V);
            }else{
                T.push_back(V);
            }
        }
        ll now = T.front(); T.pop_front();
        ll cost = 0;
        while(T.size()){
            cost += abs(T.front() - now);
            now = T.front(); T.pop_front();
        }
        res = max(res,cost);
    }
    { // 3
        deque<ll> deq(A.begin(),A.end());
        deque<ll> T;
        T.push_back(deq.front()); deq.pop_front();
        ll cnt = 1;
        while(deq.size()){
            ll L = T.front(),R = T.back();
            ll V;
            if(cnt%2==0){
                V = deq.front(); deq.pop_front();
            }else{
                V = deq.back(); deq.pop_back();
            }
            cnt++;
            if(abs(L-V) > abs(R-V)){
                T.push_front(V);
            }else{
                T.push_back(V);
            }
        }
        ll now = T.front(); T.pop_front();
        ll cost = 0;
        while(T.size()){
            cost += abs(T.front() - now);
            now = T.front(); T.pop_front();
        }
        res = max(res,cost);
    }
    {  // 4
        deque<ll> deq(A.begin(),A.end());
        deque<ll> T;
        T.push_back(deq.front()); deq.pop_front();
        ll cnt = 1;
        while(deq.size()){
            ll L = T.front(),R = T.back();
            ll V;
            if(cnt%2==0){
                V = deq.front(); deq.pop_front();
            }else{
                V = deq.back(); deq.pop_back();
            }
            cnt++;
            if(abs(L-V) >= abs(R-V)){
                T.push_front(V);
            }else{
                T.push_back(V);
            }
        }
        ll now = T.front(); T.pop_front();
        ll cost = 0;
        while(T.size()){
            cost += abs(T.front() - now);
            now = T.front(); T.pop_front();
        }
        res = max(res,cost);
    }
    {  // 5
        deque<ll> deq(A.begin(),A.end());
        deque<ll> T;
        T.push_back(deq.back()); deq.pop_back();
        while(deq.size()){
            ll L = T.front(),R = T.back();
            ll V1 = deq.front();
            ll V2 = deq.back();
            
            ll V;
            if(max(abs(L-V1),abs(R-V1))>max(abs(L-V2),abs(R-V2))){
                V = V1; deq.pop_front();
            }else{
                V = V2; deq.pop_back();
            }
            if(abs(L-V) > abs(R-V)){
                T.push_front(V);
            }else{
                T.push_back(V);
            }
        }
        ll now = T.front(); T.pop_front();
        ll cost = 0;
        while(T.size()){
            cost += abs(T.front() - now);
            now = T.front(); T.pop_front();
        }
        res = max(res,cost);
    }
    
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}


Measured memory_bytes: 1960