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

void chmax(T)(ref T a, in T b) {
	if (a < b) {
		a = b;
	}
}

void main() {
	auto n = readln.chomp.to!int;
	auto a = readln.chomp.split(' ').map!(to!int).array;
	long s = 0;

	foreach (i;1..n) {
		if (a[i] < a[i - 1]) {
			auto d = a[i - 1] - a[i];
			a[i] += d;
			s += d;
		}
	}

	writeln(s);
}
