#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <unordered_map>
 
using namespace std;
typedef long long ll;
 
int main()
{
    ll N;
    cin >> N;
    ll* A;
    A = new ll[N];
    for(ll i = 0; i < N; i ++){
        cin >> A[i];
    }
    sort(A, A + N);

    ll result = 0;
    ll start = 0;
    ll end = N - 1;

    ll right = 0;
    ll left = 0;
    ll center = A[0];
    start = 1;
    right = A[N - 1];
    left = A[N - 2];
    end -= 2;
    result += right - center;
    result += left - center;

    bool isMax = false;
    while(start <= end){
        // cout << "left" << left << endl;
        // cout << "right" << right << endl;
        // cout << "start" << start << endl;
        // cout << "result" << result << endl;
        // cout << "end" << end << endl;
        if(isMax){
            if(end - start >= 1){
                if(A[end] - left + A[end - 1] - right > A[end - 1] - right + A[end] - left){
                    
                    result += A[end] - left + A[end - 1] - right;
                    left = A[end];
                    right = A[end - 1];
        //             cout << "left" << left << endl;
        // cout << "right" << right << endl;
        //             cout << "1:" << result << endl;
                }
                else{
                    
                    result += A[end - 1] - right + A[end] - left;
                    right = A[end];
                    left = A[end - 1];
        //             cout << "left" << left << endl;
        // cout << "right" << right << endl;
        //             cout << "2:" << result << endl;
                }
                end -= 2;
            }
            else{
                result += max(A[end] - left, A[end] - right);
                break;
            }
            isMax = !isMax;
        }
        else{
            if(end - start >= 1){
                if(left - A[start] + right - A[start + 1] > left - A[start + 1] + right - A[start]){
                    
                    result += left - A[start] + right - A[start + 1];
                    left = A[start];
                    right = A[start + 1];
                    // cout << "left" << left << endl;
                    // cout << "right" << right << endl;
                    // cout << "3:" << result << endl;
                }
                else{
                    
                    result += left - A[start + 1] + right - A[start];
                    right = A[start];
                    left = A[start + 1];
                    // cout << "left" << left << endl;
                    // cout << "right" << right << endl;
                    // cout << "4:" << result << endl;
                }
                start += 2;
            }
            else{
                result += max(left - A[start], right - A[start]);
                break;
            }
            isMax = !isMax;
        }
    }
    ll result1 = result;
    //cout << "result1:" << result << endl;

    result = 0;
    start = 0;
    end = N - 1;

    right = 0;
    left = 0;
    center = A[N - 1];
    start = 2;
    right = A[0];
    left = A[1];
    end -= 1;
    result += center - right;
    result += center - left;

    isMax = true;
    while(start <= end){
        // cout << "left" << left << endl;
        // cout << "right" << right << endl;
        // cout << "start" << start << endl;
        // cout << "result" << result << endl;
        // cout << "end" << end << endl;
        if(isMax){
            if(end - start >= 1){
                if(A[end] - left + A[end - 1] - right > A[end - 1] - right + A[end] - left){
                    
                    result += A[end] - left + A[end - 1] - right;
                    left = A[end];
                    right = A[end - 1];
                    //cout << "1:" << result << endl;
                }
                else{
                    
                    result += A[end - 1] - right + A[end] - left;
                    right = A[end];
                    left = A[end - 1];
                    //cout << "2:" << result << endl;
                }
                end -= 2;
            }
            else{
                result += max(A[end] - left, A[end] - right);
                break;
            }
            isMax = !isMax;
        }
        else{
            if(end - start >= 1){
                if(left - A[start] + right - A[start + 1] > left - A[start + 1] + right - A[start]){
                    
                    result += left - A[start] + right - A[start + 1];
                    left = A[start];
                    right = A[start + 1];
                    //cout << "3:" << result << endl;
                }
                else{
                    
                    result += left - A[start + 1] + right - A[start];
                    right = A[start];
                    left = A[start + 1];
                    //cout << "4:" << result << endl;
                }
                start += 2;
            }
            else{
                result += max(left - A[start], right - A[start]);
                break;
            }
            isMax = !isMax;
        }
    }
    if(result > result1){
        cout << result << endl;
    }
    else{
        cout << result1 << endl;
    }

    return 0;
}

Measured memory_bytes: 1152