unittest
{
	assert( [ "4", "2 2 1 3" ].solve == 7 );
	assert( [ "7", "1 1 1 1 1 1 1" ].solve == 6 );
}



import std.algorithm;
import std.container;
import std.conv;
import std.range;
import std.stdio;

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

auto solve( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = input.front.to!( long );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	as.sort!( ( x, y ) => y < x );
	
	if( as.length == 1 ) return 0;
	if( as.length == 2 ) return as[ 0 ];
	
	auto ans = 0L;
	auto rbt = redBlackTree!( ( x, y ) => y < x, true )( as[ 0 ] );
	foreach( a; as[ 1 .. $ ] )
	{
		auto m = rbt.front;
		rbt.removeFront;
		
		rbt.insert( a );
		rbt.insert( a );
		ans += m;
	}
	return ans;
}
