#include <bits/stdc++.h>
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;
using Field = vector<vector<char>>;
using P = pair<int, int>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<ll> 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;
}
