#include <iostream>
using namespace std;

int main() {
  long n, max = 0, sum = 0;
  cin >> n;
  long a[n];
  for (long i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > max) max = a[i];
    else sum += max - a[i];
  }
  
  cout << sum;
  
  return 0;
}