import std.stdio;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;

string my_readln() { return chomp(readln()); }

void main()
{
	auto tokens = split(my_readln());
	auto N = to!uint(tokens[0]);

	long[] a;
	foreach (i; 0..N)
	{
		a ~= to!long(my_readln());
	}
	a.sort();
	stderr.writeln(a);
	ulong result;
	long last_b = a[0], last_e = a[0];
	ulong begin = 1, end = a.length - 1;
	ulong mod = 0;
	if (a.length % 2 == 1)
	{
		auto mid = a.length / 2;
		auto left = a[mid] - a[mid-1];
		auto right = a[mid+1] - a[mid];
		if ((mid % 2 == 0 && left > right) || (mid % 2 == 1 && left < right))
		{
			last_b = a[$-1];
			last_e = a[$-1];
			begin = 0;
			end = a.length - 2;
			mod = 1;
		}
	}
	
	foreach (i; 1..a.length)
	{
		ulong idx;
		if ((i - 1) / 2 % 2 == mod)
			idx = end--;
		else
			idx = begin++;
		if ((i - 1) % 2 == 0)
		{
			result += abs(last_b - a[cast(uint)idx]);
			last_b = a[cast(uint)idx];
		}
		else
		{
			result += abs(last_e - a[cast(uint)idx]);
			last_e = a[cast(uint)idx];
		}
		stderr.writeln(idx);
		
	}
	writeln(result);
	stdout.flush();
}