#include <iostream>
using namespace std;
int main()
{
 	int n;
  	cin >> n;
  	int x;
  	int max = 0
  	long long int answer = 0;
  	for (int i = 0; i < n; i++)
    {
     	cin >> x;
      	if (x > max)
          max = x;
      	else answer += (max - x);
    }
  	cout << answer;
}