#include <bits/stdc++.h>
using namespace std;
const long long LINF = 1LL << 60;
const long long LMINF = 1LL << 63;
const int INF = 1 << 30;
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() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  int ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (a[i] >= a[i + 1]) {
      ans += a[i] - a[i + 1];
      a[i + 1] = a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
