#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
template <class T = long long>
using V = vector<T>;
template <class T = long long>
using PQ = priority_queue<T>;
template <class T = long long>
using PQG = priority_queue<T, V<T>, greater<T>>;
const long long MOD = 1000000007LL;
void in() {}
template <class Head, class... Tail>
void in(Head &&head, Tail &&...tail) {
  cin >> head;
  in(move(tail)...);
}
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
string join(vector<T> &v, string delim = "") {
  ostringstream os;
  for (int i = 0, i_len = (v.size()); i < i_len; ++i)
    i ? os << delim << v[i] : os << v[0];
  return os.str();
}
void solve() {
  long long n;
  in(n);
  V<long long> a(n);
  for (auto &_elem : a) cin >> _elem;
  long long res = 0;
  for (int i = (1), i_len = (n); i < i_len; ++i) {
    long long v = max(a[i] - a[i - 1], 0LL);
    res += v;
    a[i] += v;
  }
  cout << res << endl;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}
