open Printf open Scanf
module Lib = struct
	let i32,i64,ist = Int64.(to_int,of_int,to_string)
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
	let labs = Int64.abs
	let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
	let (+=) r v = r := !r + v
	let (-=) r v = r := !r - v
	let ( *= ) r v = r := !r * v
	let (/=) r v = r := !r / v
	let (%=) r v = r := !r % v
	(* math *)
	let ceildiv p q = (p+q-1L) / q
	let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
	let lcm m n = m / gcd m n * n 
	module I64_infix = struct
		let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
			let open Int64 in
			(logand),(logor),(logxor),lognot,
			(fun u v -> shift_left u (i32 v)),
			(fun u v -> shift_right u (i32 v)),
			(fun u v -> shift_right_logical u (i32 v))
	end
	(* input *)
	let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
	let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
	let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
	let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
	let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
	let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
	let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
	(* utils *)
	external id : 'a -> 'a = "%identity"
	let alen a = i64 @@ Array.length a
	let llen l = i64 @@  List.length l
	let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
	let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
	let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
	let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
	let fail _ = failwith "fail"
	let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
	let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
	let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
	let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
	let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
		let rec f0 ng ok =
			if labs (ok - ng) <= 1L then ok
			else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
		in f0 (ng-1L*d) (ok+1L*d)
	(* imperative *)
	let rep f t fbod = let i = ref f in while !i <= t do fbod !i; i := !i + 1L done
	let repb f t fbod = let i,c = ref f,ref true in while !i <= t && !c do
		match fbod !i with | `Break -> c := false | `Ok -> i := !i + 1L done
	let repm f t m0 fbod =
		let i,c,m = ref f,ref true,ref m0 in
		while !i<=t && !c do match fbod !m !i with
		| `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + 1L done; !m
	let repi f t fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ 1 done
	let repib f t fbod = let i,c = ref f,ref true in while !i <= t && !c do
		match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ 1 done
	let repim f t m0 fbod =
		let i,c,m = ref f,ref true,ref m0 in
		while !i<=t && !c do match fbod !m !i with
		| `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ 1 done; !m
	let rep_rev f t fbod = rep t f @@ fun v -> fbod @@ f + t - v
	let repb_rev f t fbod = repb t f @@ fun v -> fbod @@ f + t - v
	let repm_rev f t m0 fbod = repm f t m0 @@ fun u v -> fbod u @@ f + t - v
	let repi_rev f t fbod = repi t f @@ fun v -> fbod @@ f +$ t -$ v
	let repib_rev f t fbod = repib t f @@ fun v -> fbod @@ f +$ t -$ v
	let repim_rev f t m0 fbod = repim f t m0 @@ fun u v -> fbod u @@ f +$ t -$ v
	(* output *)
	let print_list_mle f ls = string_of_list f ls |> print_endline
	let print_array_mle f a = string_of_array f a |> print_endline
	let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
	let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
	(* debug *)
	let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
	let dump_i64_list ls = print_list ist ls
	let dump_i64_array a = print_array ist a
end open Lib

module Deque = struct
	(* let (+$),(-$) = Pervasives.((+),(-)) *)
	type 'a u = {
		mutable prev: 'a cell;
		value: 'a;
		mutable next: 'a cell; }
	and 'a cell = Nil | Cons of 'a u
	type 'a t = {
		mutable length: int;
		mutable head: 'a cell;
		mutable tail: 'a cell; }
	let empty () = {length=0;head=Nil;tail=Nil}
	let is_empty {length;_} = length=0
	let length {length;_} = length
	let clear q =
		q.length <- 0; q.head <- Nil; q.tail <- Nil
	let push_rear value q = match q.tail with
	| Nil ->
		let n = Cons {
			prev=Nil; value; next=Nil; } in
		q.length <- 1; q.head <- n; q.tail <- n
	| Cons tail ->
		let n = Cons {
			prev=q.tail; value; next=Nil; } in
		q.length <- q.length+$1; tail.next <- n; q.tail <- n
	let pop_front q = match q.head with
	| Nil -> raise Queue.Empty
	| Cons {value;next=Nil;_} -> clear q; value
	| Cons {value;next=Cons c as next;_} ->
		q.length <- q.length-$1; c.prev <- Nil; q.head <- next; value
	let push_front value q = match q.head with
	| Nil ->
		let n = Cons {
			prev=Nil; value; next=Nil; } in
		q.length <- 1; q.head <- n; q.tail <- n
	| Cons head ->
		let n = Cons {
			prev=Nil; value; next=q.head; } in
		q.length <- q.length+$1; head.prev <- n; q.head <- n
	let pop_rear q = match q.tail with
	| Nil -> raise Queue.Empty
	| Cons {value;prev=Nil;_} -> clear q; value
	| Cons {value;prev=Cons c as prev;_} ->
		q.length <- q.length-$1; c.next <- Nil; q.tail <- prev; value
	let iter f {head;_} = 
		let rec f0 = function
		| Nil -> () | Cons {value;next;_} -> f value; f0 next
		in f0 head
	let iter_rev f {tail;_} = 
		let rec f0 = function
		| Nil -> () | Cons {value;prev;_} -> f value; f0 prev
		in f0 tail
	let fold f u0 {head;_} =
		let rec f0 u = function
		| Nil -> u | Cons {value;next;_} -> f0 (f u value) next
		in f0 u0 head
	let of_list ls =
		let q = empty () in List.iter (fun v -> push_rear v q) ls; q
	let to_list q =
		fold (fun u v -> v::u) [] q |> List.rev
end

let n = get_i64 0
let a = input_i64_array n
let doo a =
	let res = Deque.empty() in
	Array.sort compare a;
	Deque.push_front a.(0) res;
	Deque.push_rear a.(i32 n-$1) res;
	rep 1L (n-2L) (fun i ->
		let i = (if i mod 2L = 1L then (i/2L)+1L else (n-1L-(i/2L)))
		in
		let i = i32 i in let v = a.(i) in
		let p,q = Deque.pop_front res, Deque.pop_rear res in
		Deque.push_front p res;
		Deque.push_rear q res;
		let r,s = labs (p-v), labs(q-v) in
		if r<s then (
			Deque.push_rear v res;
		) else (
			Deque.push_front v res;
		)
	);
	(* Deque.to_list res |> print_list ist; *)
	let c = Deque.to_list res |> Array.of_list in
	let (_,r) = c |> Array.fold_left(fun (prev,sum) v->
		(v,sum+labs(v-prev))) (c.(0),0L)
	(* let (_,r) = Deque.fold (fun (prev,sum) v -> (v,sum + labs(prev-v))) 0L res
	*)
	in r


let () =
	let r1 = doo a in
	let a = Array.to_list a |> List.rev |> Array.of_list in
	let r2 = doo a in
	printf "%Ld\n" (max r1 r2)


