#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vec = vector<T>;
template <typename T>
using vvec = vector<vector<T>>;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (size_t i = 0; i < (n); i++) {
    cin >> (a)[i];
  };
  int maxh = 0, count = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < maxh) {
      count += (maxh - a[i]);
    } else {
      maxh = a[i];
    }
  }
  cout << count;
}
