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



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[] );
	
	auto ans = 0L;
	auto m = 0L;
	foreach( a; as )
	{
		if( a < m ) ans += m - a;
		else        m = a;
	}
	return ans;
}
