#include <stdio.h>

int main()
{
	long long a = 0;
	int n, l = 0;
	scanf ("%d", &n);
	for (int i = 0; i < n; i++){
		int x; scanf ("%d", &x);
		if (l <= x) l = x;
		else a += l - x;
	}
	printf ("%lld\n", a);
	return 0;
}