#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int scan() { return getchar(); }
void scan(int& a) { cin >> a; }
void scan(long long& a) { cin >> a; }
void scan(char& a) { cin >> a; }
void scan(double& a) { cin >> a; }
void scan(long double& a) { cin >> a; }
void scan(char a[]) { scanf("%s", a); }
void scan(string& a) { cin >> a; }
template <class T>
void scan(vector<T>&);
template <class T, size_t size>
void scan(array<T, size>&);
template <class T, class L>
void scan(pair<T, L>&);
template <class T, size_t size>
void scan(T (&)[size]);
template <class T>
void scan(vector<T>& a) {
  for (auto& i : a) scan(i);
}
template <class T>
void scan(deque<T>& a) {
  for (auto& i : a) scan(i);
}
template <class T, size_t size>
void scan(array<T, size>& a) {
  for (auto& i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L>& p) {
  scan(p.first);
  scan(p.second);
}
template <class T, size_t size>
void scan(T (&a)[size]) {
  for (auto& i : a) scan(i);
}
template <class T>
void scan(T& a) {
  cin >> a;
}
void IN() {}
template <class Head, class... Tail>
void IN(Head& head, Tail&... tail) {
  scan(head);
  IN(tail...);
}
int in() {
  int x;
  cin >> x;
  return x;
}
long long llin() {
  unsigned long long x;
  cin >> x;
  return x;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  cin.tie(NULL), ios_base::sync_with_stdio(false);
  cout << fixed << setprecision((15));
  int n;
  scan(n);
  vector<int> a(n);
  for (long long i = 0; i < (n); ++i) {
    int x;
    cin >> x;
    a[i] = x;
  }
  unsigned s = 0;
  for (long long i = 1; i < (n); ++i) {
    if (a[i - 1] > a[i]) {
      int temp = (a[i - 1] - a[i]);
      s += temp;
      a[i] += temp;
    }
  }
  cout << s << '\n';
  return 0;
}
