#include <bits/stdc++.h>
using namespace std;
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() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  for (long long i = 0; i < N; ++i) {
    cin >> A[i];
  }
  long long ans = 0;
  for (int i = 1; i < N; ++i) {
    int tmp = max(0, A[i] - A[i - 1]);
    ans += tmp;
    A[i] = A[i] + tmp;
  }
  cout << ans << endl;
}
