const
	MAXN = 200000;

var
	a, b, c: array [0..MAXN] of longint;
	n, m, k, l, r, i, j: longint;
	result: int64;

procedure swap(var x, y: longint);
var
	t: longint;
begin
	t := x; x := y; y := t;
end;

procedure sort(var a: array of longint; l, r: longint);
var
	i, j, x: longint;
begin
	i := l; j := r;
	x := a[l + random(r - l + 1)];
	repeat
		while a[i] > x do inc(i);
		while a[j] < x do dec(j);
		if i <= j then
		begin
			swap(a[i], a[j]);
			inc(i); dec(j);
		end;
	until i > j;
	if l < j then sort(a, l, j);
	if i < r then sort(a, i, r);
end;

begin
//	assign(input, 'D.in'); reset(input);
//	assign(output, 'D.out'); rewrite(output);
	
	for i := 0 to MAXN do c[i] := 0;

	readln(n);
	m := 0;
	for i := 0 to n - 1 do
	begin
		read(a[i]);
		inc(c[a[i]]);
		if a[i] > m then m := a[i];
	end;

	k := -1;
	for i := 0 to MAXN do
	begin
		if (c[i] > (n shr 1) + (n and 1)) then
		begin
			k := i;
			break;
		end;
	end;
	if k <> -1 then
	begin
		l := 0;
		if k <> m then
		begin
			for i := MAXN downto k + 1 do
			begin
				while c[i] > 0 do
				begin
					b[l] := k; inc(l);
					dec(c[i]);
					b[l] := i; inc(l);
					dec(c[k]);
				end;
			end;
		end;
		r := n - 1;
		for i := 0 to k do
		begin
			while c[i] > 0 do
			begin
				if r < l then break;
				b[r] := k; dec(r);
				dec(c[k]);
				if r < l then break;
				b[r] := i; dec(r);
				dec(c[i]);
			end;
		end;
	end;
	if k = -1 then
	begin
		sort(a, 0, n - 1);
		k := 0;
		i := 1;
		while i < n do
		begin
			b[i] := a[k]; inc(k);
			inc(i, 2);
		end;
		i := 0;
		while i < n do
		begin
			b[i] := a[k]; inc(k);
			inc(i, 2);
		end;
		
		if n and 1 = 1 then
		begin
			swap(b[n - 1], b[n - 2]);
			swap(b[n - 2], b[n - 3]);
		end;
	end;
	
	result := 0;
	i := 0;
	while i < n do
	begin
		j := i;
		while (j < n) and (b[j] <= b[j + 1]) do
		begin
			inc(j);
		end;
		inc(result, b[j]);
		i := j;
		inc(i);
	end;
	
	writeln(result);
	
	close(output);
end.
