#include <bits/stdc++.h>
using namespace std;
template <class T>
bool INRANGE(T x, T a, T b) {
  return a <= x && x <= b;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int now = -1;
  int acc = 0;
  for (int i = 0; i < (n); ++i) {
    int a;
    cin >> a;
    if (now == -1) now = a;
    if (now > a) {
      acc += now - a;
    }
    if (now < a) {
      now = a;
    }
  }
  cout << acc << endl;
  return 0;
}
