#include <bits/stdc++.h>
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool change(T& a, T b) {
  if (a != b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (long long i = 0; i < (n); ++i) cin >> a[i];
  int m = 0, ans = 0;
  for (long long i = 0; i < (n); ++i) {
    if (m <= a[i]) {
      m = a[i];
    } else {
      ans += m - a[i];
    }
  }
  cout << ans << endl;
}
