unittest
{
	assert( [ "7", "3 1 4 1 5 9 2" ].parse.expand.solve == 4 );
	assert( [ "10", "0 1 2 3 4 5 6 7 8 9" ].parse.expand.solve == 3 );
	assert( [ "1", "99999" ].parse.expand.solve == 1 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
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.strip.to!( long );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	return tuple( as );
}

auto solve( long[] as )
{
	as.sort();
	as = -100 ~ as ~ -100;
	
	auto r = 0L;
	auto gs = as.group.array;
	foreach( i; 1 .. gs.length - 1 )
	{
		auto sa = gs[ i ][ 1 ];
		auto sb = ( gs[ i - 1 ][ 0 ] == gs[ i ][ 0 ] - 1 ) ? gs[ i - 1 ][ 1 ] : 0L;
		auto sc = ( gs[ i + 1 ][ 0 ] == gs[ i ][ 0 ] + 1 ) ? gs[ i + 1 ][ 1 ] : 0L;
		auto s = sa + sb + sc;
		if( r < s ) r = s;
	}
	
	return r;
}
