#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1000000000;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) cin >> A[i];
  int prev = -1;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (prev > A[i]) {
      ans += prev - A[i];
      A[i] = prev;
    }
    prev = A[i];
  }
  cout << ans << endl;
}
