unittest
{
	assert( [ "5", "6", "8", "1", "2", "3" ].parse.expand.solve == 21 );
	assert( [ "6", "3", "1", "4", "1", "5", "9" ].parse.expand.solve == 25 );
	assert( [ "3", "5", "5", "1" ].parse.expand.solve == 8 );
	
	assert( [ "5", "6", "8", "1", "2", "5" ].parse.expand.solve == 21 );		// 追加ケース
}



import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = input.front.to!( long );
	auto as = new long[ n ];
	foreach( ref a; as )
	{
		input.popFront;
		a = input.front.to!( long );
	}
	return tuple( as );
}

auto solve( long[] as )
{
	as.sort();
	
	// 真ん中に1番小さいやつ、その外側に1,2番目に大きいやつ、その外側に2,3番目に小さいやつ…と並べたパターンを計算
	auto ans1 = solve2( as, true );
	
	// 真ん中に1番大きいやつ、その外側に1,2番目に小さいやつ、その外側に2,3番目に大きいやつ…と並べたパターンを計算
	auto ans2 = solve2( as, false );
	
	return max( ans1, ans2 );
}

auto solve2( long[] as, bool f )
{
	// 並べ替える
	auto bs = new long[ as.length ];
	auto ci = bs.length.signed / 2;
	if( f )
	{
		bs[ ci ] = as.front;
		as.popFront;
	}
	else
	{
		bs[ ci ] = as.back;
		as.popBack;
	}
	for( auto i = 1L;; i++ )
	{
		f = !f;
		if( f )
		{
			bs[ ci - i ] = as.front;
			as.popFront;
			if( as.empty ) break;
			bs[ ci + i ] = as.front;
			as.popFront;
			if( as.empty ) break;
		}
		else
		{
			bs[ ci - i ] = as.back;
			as.popBack;
			if( as.empty ) break;
			bs[ ci + i ] = as.back;
			as.popBack;
			if( as.empty ) break;
		}
	}
	
	// 差の合計を計算
	auto dt = 0L;
	auto p = bs.front;
	foreach( b; bs )
	{
		dt += ( p - b ).abs;
		p = b;
	}
	return dt;
}
