#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B>
inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}
int dy[] = {1, 0};
int dx[] = {0, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, ans = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i != 0 && a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << '\n';
  return 0;
}
