#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <vector>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef double f2;
typedef long double f3;
#define N 100050
#define OUTSIZE 1050
#define rep(n) for(i=1;i<=n;i++)
#define per(n) for(i=n;i;i--)
namespace SA {
	int n,ws[OUTSIZE],wa[OUTSIZE],wb[OUTSIZE],rk[OUTSIZE],height[OUTSIZE],sa[OUTSIZE];
	char w[OUTSIZE];
	void build(int m) {
		int i,j,p,*x=wa,*y=wb;
		rep(m)ws[i]=0;
		rep(n)ws[x[i]=w[i]]++;
		rep(m)ws[i]+=ws[i-1];
		per(n)sa[ws[x[i]]--]=i;
		for(j=1;j<n;j<<=1) {
			for(p=0,i=n;i>n-j;i--) y[++p]=i;
			rep(n)if(sa[i]>j) y[++p]=sa[i]-j;
			rep(m)ws[i]=0;
			rep(n)ws[x[i]]++;
			rep(m)ws[i]+=ws[i-1];
			per(n)sa[ws[x[y[i]]]--]=y[i];
			swap(x,y);
			m=1;
			rep(n)x[sa[i]]=y[sa[i]+j]==y[sa[i+1]+j]&&y[sa[i]]==y[sa[i+1]]?m:m++;
			if(m>n) break;
		}
		rep(n)rk[sa[i]]=i; 
		p=0;
		rep(n)if(rk[i]!=n) {
			j=rk[i]+1;
			for(;w[i+p]==w[sa[j]+p];p++) ;
			height[rk[i]]=p;
			if(p) p--;
		}
	}
	void init() {
		n=6; w[1]='a'; w[2]='b'; w[3]='a'; w[4]='b'; w[5]='b'; w[6]='a';
		build(128);
	}
}
namespace Scape_Goat_Tree {
	#define ls ch[p][0]
	#define rs ch[p][0]
	int ch[OUTSIZE][2],val[OUTSIZE],siz[OUTSIZE],tot[OUTSIZE],ex[OUTSIZE],S[OUTSIZE],V[OUTSIZE],tp,koishi;
	int root,f[OUTSIZE],cnt;
	f2 AL=0.75;
	void pushup(int p) {
		siz[p]=siz[ls]+siz[rs]+ex[p];
		tot[p]=tot[ls]+tot[rs]+1;
	}
	int GG(int p) {
		return tot[p]*AL<max(tot[ls],tot[rs]);
	}
	int newnode(int v) {int p=++cnt; siz[p]=tot[p]=ex[p]=1; val[p]=v; return p;}
	void insert(int &p,int v,int fa) {
    	if(!p) {p=newnode(v); f[p]=fa; return ;}
    	insert(ch[p][v>val[p]],v,p);
    	pushup(p); if(GG(p)) koishi=p;
	}
	void init() {
		insert(root,1,0); insert(root,2,0); insert(root,3,0);
		insert(root,4,0); insert(root,5,0);
	}
	#undef ls
	#undef rs
}
#undef N
#define N 300050
int n,a[N],b[N],Q[N],l,r;
ll ans;
int main() {

	SA::init();Scape_Goat_Tree::init();
	
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	l=r=150000;
	Q[r++]=a[1];
	int h=2,t=n;
	int flg=1;
	while(h<=t) {
		if(flg) {
			if(h==t) {
				if(abs(a[t]-Q[l])>abs(a[t]-Q[r-1])) Q[--l]=a[t]; 
				else Q[r++]=a[t];
				break;
			}
			Q[r++]=a[t];
			t--;
			Q[--l]=a[t];
			t--;
		}else {
			if(h==t) {
				if(abs(a[h]-Q[r-1])>abs(a[h]-Q[l])) Q[r++]=a[h];
				else Q[--l]=a[h];
				break;
			}
			Q[--l]=a[h];
			h++;
			Q[r++]=a[h];
			h++;
		}
		flg^=1;
	}
	for(i=1;i<=n;i++) b[i]=Q[l++];
	//for(i=1;i<=n;i++) printf("%d\n",b[i]);
	for(i=2;i<=n;i++) ans+=abs(b[i]-b[i-1]);

	reverse(a+1,a+n+1);
	l=r=150000;
	Q[r++]=a[1];
	h=2,t=n;
	flg=1;
	while(h<=t) {
		if(flg) {
			if(h==t) {
				if(abs(a[t]-Q[l])>abs(a[t]-Q[r-1])) Q[--l]=a[t]; 
				else Q[r++]=a[t];
				break;
			}
			Q[r++]=a[t];
			t--;
			Q[--l]=a[t];
			t--;
		}else {
			if(h==t) {
				if(abs(a[h]-Q[r-1])>abs(a[h]-Q[l])) Q[r++]=a[h];
				else Q[--l]=a[h];
				break;
			}
			Q[--l]=a[h];
			h++;
			Q[r++]=a[h];
			h++;
		}
		flg^=1;
	}
	ll now=0;
	for(i=1;i<=n;i++) b[i]=Q[l++];
	for(i=2;i<=n;i++) now+=abs(b[i]-b[i-1]);
	ans=max(ans,now);
	printf("%lld\n",ans);
}

Measured memory_bytes: 1408