#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i;
  i = 0;
  cin >> n;
  long long a[n];
  while (i < n) {
    cin >> a[i];
    i++;
  }
  a[i + 1] = '\0';
  long long height;
  height = 0;
  i = 0;
  while (n > 0) {
    if (a[i] > a[i + 1]) {
      height += a[i] - a[i + 1];
      a[i + 1] = a[i];
    }
    i++;
    n--;
  }
  cout << height;
  return 0;
}
