#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef vector< vvi > vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
typedef complex<double> base;

#define mmin(a, b) a = min(a, (b))
#define mmax(a, b) a = max(a, (b))
#define mp(a,b) make_pair(a,b)
#define modsum(a, b) ((a) + (b) >= M ? (a) + (b) - M : (a) + (b))
#define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b))
#define modmultiply(a, b) (((a) * (b)) % M)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for(int i = 0; i < int(n); ++i)
#define cint const int &
#define SZ(a) ((int)(a.size()))
#define watch(x) cerr << (#x) << " = " << x << endl;

const double PI = 2 * acos(0.0);

const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";


template<class T>
ostream & operator << (ostream &out, vector<T> &a) {
    out << "{";
    for (auto x : a) out << x << " ";
    return out << "}";
}

template<class T>
ostream & operator << (ostream &out, pair<T, T> &a) {
    out << "{";
    cout << a.first << ", " << a.second;
    return out << "}";
}

void smain();

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

#ifdef MAX_HOME
    freopen("input.txt", "r", stdin);
#endif

    cout << setprecision(12) << fixed;

    smain();
}

//
//const int N = 309;
//
//ll solve(vvi mat, int dist) {
//    int n = mat.size(), m = mat[0].size();
//    vvl cnt(n, vl(m, 0));
//    vvl a = calc(mat, dist);
//    fori (i, n) fori (j, m) cnt[i][j] += a[i][j];
//
//    vvl tmp = inverse_i(mat);
//    a = inverse_i(calc(tmp, dist));
//    fori (i, n) fori (j, m) cnt[i][j] += a[i][j];
//
//    tmp = inverse_j(mat);
//    a = inverse_j(calc(tmp, dist));
//    fori (i, n) fori (j, m) cnt[i][j] += a[i][j];
//
//    tmp = inverse_i(inverse_j(mat));
//    a = inverse_i(inverse_j(calc(tmp, dist)));
//    fori (i, n) fori (j, m) cnt[i][j] += a[i][j];
//    ll ret = 0;
//    fori (i, n) fori (j, n) {
//        ll cur = a[i][j];
//        ret = cur * (cur - 1) * (cur - 2) / (2LL * 3LL);
//    }
//    return ret;
//}

ll solve1(int n, vi a) {
    int h = n >> 1;
    vi big, small;
    sort(RALL(a));
    fori (i, n)
        if (i < h) big.push_back(a[i]);
        else small.push_back(a[i]);

    int i = 0, j = 2;
    vi res(n);
    res[0] = small[0];
    if (small.size() > 1) res[n - 1] = small[1];
    for (int k = 1; k < n - 1; ++k)
        if (k & 1)
            res[k] = big[i++];
        else
            res[k] = small[j++];
    ll ret = 0;
    for (int i = 1; i < n; ++i) ret += abs(res[i] - res[i - 1]);
    return ret;
}

ll solve2(int n, vi a) {
    int h = n >> 1;
    vi big, small;
    sort(ALL(a));
    fori (i, n)
        if (i < h) small.push_back(a[i]);
        else big.push_back(a[i]);

    int i = 0, j = 2;
    vi res(n);
    res[0] = big[0];
    if (big.size() > 1) res[n - 1] = big[1];
    big.pop_back();
    for (int k = 1; k < n - 1; ++k)
        if (k & 1)
            res[k] = small[i++];
        else
            res[k] = big[j++];
    ll ret = 0;
    for (int i = 1; i < n; ++i) ret += abs(res[i] - res[i - 1]);
    return ret;
}

ll solve3(int n, vi a) {
    int h = n >> 1;
    vi big, small;
    sort(ALL(a));
    fori (i, n)
        if (i < h) small.push_back(a[i]);
        else big.push_back(a[i]);

    int i = 0, j = 1;
    vi res(n);
    res[0] = big[0];
    if (small.size()) res[n - 1] = small.back();
    for (int k = 1; k < n - 1; ++k)
        if (k & 1)
            res[k] = small[i++];
        else
            res[k] = big[j++];
    ll ret = 0;
    watch(res);
    for (int i = 1; i < n; ++i) ret += abs(res[i] - res[i - 1]);
    return ret;
}

void smain() {
    int n;
    cin >> n;
    vi a(n);
    fori (i, n) cin >> a[i];
    if (n & 1)
        cout << max(solve1(n, a), solve2(n, a));
    else
        cout << solve3(n, a);
}
//
//void smain2() {
//    int n, m;
//    cin >> n >> m;
//    vvi mat(n, vi(m, 0));
//
//    fori (i, n) {
//        string s;
//        cin >> s;
//        fori (j, m) mat[i][j] = s[j] == '#';
//    }
//
//    ll ans = 0;
//
//    for (int i = 1; i <= n; ++i)  ans += solve(mat, i);
//    cout << ans;
//}
//
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

Measured memory_bytes: 4696