unittest
{
	assert( [ "2", "4 8" ].parse.expand.solve == 8 );
	assert( [ "3", "1 1 3" ].parse.expand.solve == 3 );
	assert( [ "3", "4 2 5" ].parse.expand.solve == 5 );
	assert( [ "4", "-100 -100 -100 -100" ].parse.expand.solve == 0 );
}



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

auto solve( long[] as )
{
	auto ans = 0L;
	auto ave = roundTo!long( cast( real ) as.sum / as.length );
	foreach( a; as )
	{
		ans += ( ave - a ) ^^ 2;
	}
	return ans;
}
