/*+lmake
 * STD = c++14
 * DEFINE += WAAUTOMATON
 */
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#ifdef WAAUTOMATON
#define debug(args...)                                                                             \
    {                                                                                              \
        dbg, args;                                                                                 \
        cerr << endl;                                                                              \
    }
#define massert(x) assert(x)
#else
#define debug(args...) // Just strip off all debug tokens
#define massert(x)
#endif
struct debugger
{
    template <typename T>
    debugger &operator,(const T &v)
    {
        cerr << v << " ";
        return *this;
    }
} dbg;

void solve(long long n, vector<long long> a)
{
    srand(19260817);
    sort(a.begin(), a.end());
    if (n % 2 == 0) {
        LL ans1 = 0;
        LL l = a[n / 2], r = a[n / 2 - 1];
        for (int i = 3; i <= n; ++i) {
            LL now;
            if (i % 2 == 0) {
                now = a[n / 2 - i / 2];
            } else {
                now = a[n / 2 + i / 2];
            }
            if (abs(now - l) > abs(now - r) || (abs(now - l) == abs(now - r) && rand() % 2 == 0)) {
                ans1 += abs(now - l);
                l = now;
            } else {
                ans1 += abs(now - r);
                r = now;
            }
        }
		ans1+=abs(l-r);
        LL ans2 = 0;
        l = a[n / 2], r = a[n / 2 - 1];
        for (int i = 3; i <= n; ++i) {
            LL now;
            if (i % 2 == 1) {
                now = a[n / 2 - i / 2-1];
            } else {
                now = a[n / 2 + i / 2-1];
            }
            if (abs(now - l) > abs(now - r) || (abs(now - l) == abs(now - r) && rand() % 2 == 0)) {
                ans2 += abs(now - l);
                l = now;
            } else {
                ans2 += abs(now - r);
                r = now;
            }
        }
		ans2+=abs(l-r);
		cout<<max(ans1,ans2)<<endl;
        return;
    }
    /*for(auto i:a) {
            cout<<i<<' ';
    }
    cout<<endl;*/
    LL ans1 = 0;
    LL l = a[0], r = a[0];
    for (int i = 2; i <= n; ++i) {
        LL now;
        if (i % 2 == 0) {
            now = a[n - i / 2];
        } else {
            now = a[i / 2];
        }
        if (abs(now - l) > abs(now - r) || (abs(now - l) == abs(now - r) && rand() % 2 == 0)) {
            ans1 += abs(now - l);
            l = now;
        } else {
            ans1 += abs(now - r);
            r = now;
        }
    }
    LL ans2 = 0;
    l = a[n - 1];
    r = a[n - 1];
    deque<int> x;
    x.push_back(l);
    for (int i = 2; i <= n; ++i) {
        LL now;
        if (i % 2 == 0) {
            now = a[i / 2 - 1];
        } else {
            now = a[n - i / 2 - 1];
        }
        if (abs(now - l) > abs(now - r) || (abs(now - l) == abs(now - r) && rand() % 2 == 0)) {
            x.push_front(now);
            ans2 += abs(now - l);
            l = now;
        } else {
            ans2 += abs(now - r);
            x.push_back(now);
            r = now;
        }
    }
    /*for(auto i:x) {
            cout<<i<<' ';
    }
    cout<<endl;*/
    debug(ans1, ans2);
    cout << max(ans1, ans2) << endl;
}

int main()
{
#ifdef WAAUTOMATON
    freopen("in.txt", "r", stdin);
#endif
    long long N;
    scanf("%lld", &N);
    vector<long long> A(N - 1 + 1);
    for (int i = 0; i <= N - 1; i++) {
        scanf("%lld", &A[i]);
    }
    solve(N, A);
    return 0;
}


Measured memory_bytes: 2304