#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const long long LINF = 1001002003004005006ll;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int n;
  cin >> n;
  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]) continue;
    ans += a[i] - a[i + 1];
    a[i + 1] = a[i];
  }
  cout << ans << endl;
}
