#include <bits/stdc++.h>
using namespace std;
template <typename T>
void printvec(vector<T> &v, int n, int start = 0) {
  for (int i = start; i < n; i++) {
    cout << v[i] << " ";
  }
  cout << "done printing";
  cout << "\n";
}
const int mod = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &x : a) cin >> x;
  int prev = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < prev) {
      ans += prev - a[i];
      a[i] = prev;
    }
    prev = a[i];
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
