#pragma once
#include <stdio.h>
#include <sstream>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <limits>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define bit(x,v) ((ll)x << v)

const ll INF = 1000000007;
const int MAX = 210000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
	for (size_t i = 0; i <= n; i++)
	{
		pr[i] = i;
	}
}

int parent(int x) {
	if (x == pr[x]) return x;
	return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
	int px = parent(x);
	int py = parent(y);

	if (px == py) return false;
	if (px < py) {
		pr[py] = px;
	}
	else {
		pr[px] = py;
	}
	return true;
}

// res[i][c] := i 文字目以降で最初に文字 c が登場する index (存在しないときは n)
vector<vector<int> > calcNext(const string& S) {
	int n = (int)S.size();
	vector<vector<int> > res(n + 1, vector<int>(26, n));
	for (int i = n - 1; i >= 0; --i) {
		for (int j = 0; j < 26; ++j) res[i][j] = res[i + 1][j];
		res[i][S[i] - 'a'] = i;
	}
	return res;
}
// mod 1000000007 の世界で a += b する関数
void add(long long& a, long long b) {
	a += b;
	if (a >= MOD) a -= MOD;
}
ll slide(vector<ll>& v, ll a, ll b) {
	ll azv = abs(v[0] - a);
	ll bzv = abs(v[0] - b);
	ll aov = abs(v[1] - a);
	ll bov = abs(v[1] - b);
	v[0] = a;
	v[1] = b;
	return max((azv + bov), (bzv + aov));
}
ll a[100010];
int n;
ll calc(bool startm) {
	
	priority_queue<ll> hq;
	priority_queue<ll,vector<ll>,greater<ll>> lq;
	for (size_t i = 0; i < n; i++)
	{
		hq.push(a[i]);
		lq.push(a[i]);
	}

	int c = 1;
	ll res = 0;
	vector<ll> side(2,0);
	bool gre = true;
	if (startm) {
		
		side[0] = hq.top();
		side[1] = side[0];
		hq.pop();
		gre = false;
	}
	else {
		side[0] = lq.top();
		side[1] = side[0];
		lq.pop();
	}
	while (c < n)
	{
		if (gre) {
			ll o = hq.top();
			hq.pop();
			if (c == n - 1) {
				if (abs(side[0] - o) < abs(side[1] - o)) {
					res += abs(side[1] - o);
				}
				else {
					res += abs(side[0] - o);
				}
			}
			else {
				ll t = hq.top();
				hq.pop();
				res += slide(side, o, t);

			}
		}
		else {
			ll o = lq.top();
			lq.pop();
			if (c == n - 1) {
				if (abs(side[0] - o) < abs(side[1] - o)) {
					res += abs(side[1] - o);
				}
				else {
					res += abs(side[0] - o);
				}
			}
			else {
				ll t = lq.top();
				lq.pop();
				res += slide(side, o, t);
			}
		}

		gre = !gre;
		c += 2;
	}
	return res;
}

void solv() {

	cin >> n;
	for (size_t i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	ll r1 = a[n - 1];
	
	for (size_t i = 1; i < n; i += 2)
	{
		ll o = a[i];
		if (i + 1 >= n) {

		}
		else {
			ll t = a[i + 1];

		}
	}
	cout << max(calc(true), calc(false)) << endl;
}




int main() {
	//COMinit();
	solv();

	return 0;
}


Measured memory_bytes: 3916