#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;

    cin >> n;
    V a(n);
    REP(i, n)
    {
        cin >> a[i];
    }

    SORT(a);
    V a_(n);
    REP(i, n)
    a_[i] = a[i];
    V v;

    v.push_back(a[0]);
    a.erase(a.begin());

    bool b = true; //true:max

    while (a.size())
    {
        if (a.size() == 1)
        {
            int x = a[0];
            int q = v[0];
            int w = v[v.size() - 1];

            if (abs(x - q) > abs(x - w))
            {
                v.insert(v.begin(), x);
            }
            else
            {
                v.push_back(x);
            }

            a.erase(a.begin());
        }
        else
        {
            int x = a[a.size() - 1];
            int y = a[a.size() - 2];

            if (!b)
            {
                x = a[0];
                y = a[1];
            }

            int q = v[0];
            int w = v[v.size() - 1];

            if (abs(x - q) + abs(y - w) > abs(x - w) + abs(y - q))
            {
                v.insert(v.begin(), x);
                v.push_back(y);
            }
            else
            {
                v.insert(v.begin(), y);
                v.push_back(x);
            }

            if (b)
            {
                a.erase(a.end() - 1);
                a.erase(a.end() - 1);
            }
            else
            {
                a.erase(a.begin());
                a.erase(a.begin());
            }
        }

        b = !b;
    }

    ll res = 0;

    REP(i, n - 1)
    {
        res += abs(v[i + 1] - v[i]);
    }

    REP(i, n)
    a.push_back(a_[i]);

    v.clear();

    v.push_back(a[a.size() - 1]);
    a.erase(a.end() - 1);

    b = false; //true:max

    while (a.size())
    {
        if (a.size() == 1)
        {
            int x = a[0];
            int q = v[0];
            int w = v[v.size() - 1];

            if (abs(x - q) > abs(x - w))
            {
                v.insert(v.begin(), x);
            }
            else
            {
                v.push_back(x);
            }

            a.erase(a.begin());
        }
        else
        {
            int x = a[a.size() - 1];
            int y = a[a.size() - 2];

            if (!b)
            {
                x = a[0];
                y = a[1];
            }

            int q = v[0];
            int w = v[v.size() - 1];

            if (abs(x - q) + abs(y - w) > abs(x - w) + abs(y - q))
            {
                v.insert(v.begin(), x);
                v.push_back(y);
            }
            else
            {
                v.insert(v.begin(), y);
                v.push_back(x);
            }

            if (b)
            {
                a.erase(a.end() - 1);
                a.erase(a.end() - 1);
            }
            else
            {
                a.erase(a.begin());
                a.erase(a.begin());
            }
        }

        b = !b;
    }

    ll res_ = 0;

    REP(i, n - 1)
    {
        res_ += abs(v[i + 1] - v[i]);
    }

    cout << max(res, res_) << endl;

    return 0;
}

Measured memory_bytes: 1660