#include <bits/stdc++.h>
using namespace std;
template <class T = long long>
using V = vector<T>;
template <class T = long long>
using VV = V<V<T>>;
using P = pair<long long, long long>;
template <class T>
void scan(T a) {
  cin >> a;
}
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(V<T> &);
template <class T, class L>
void scan(pair<T, L> &);
template <class T>
void scan(V<T> &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>
void scan(T &a) {
  cin >> a;
}
void IN() {}
template <class Head, class... Tail>
void IN(Head &head, Tail &...tail) {
  scan(head);
  IN(tail...);
}
template <class T>
inline void print(T x) {
  cout << x << '\n';
}
struct inputoutputfaster {
  inputoutputfaster() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
  }
} inputoutputfaster_;
template <class T>
V<T> press(V<T> &x) {
  V<T> res = x;
  sort((res).begin(), (res).end());
  res.erase(unique((res).begin(), (res).end()), res.end());
  for (int i = (0); i < (((int)(x).size())); ++i) {
    x[i] = lower_bound((res).begin(), (res).end(), x[i]) - res.begin();
  }
  return res;
}
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;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\n';
    exit(0);
  }
}
template <class T>
inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\n';
    exit(0);
  }
}
template <class T>
T divup(T x, T y) {
  return (x + (y - 1)) / y;
}
template <typename T>
T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1) ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
const int inf = 1e9;
const long long INF = 1e18;
template <typename T>
void view(const V<T> &v) {
  cerr << "{ ";
  for (const auto &e : v) {
    cerr << e << ", ";
  }
  cerr << "\b\b }";
}
template <typename T>
void view(const VV<T> &vv) {
  cerr << "{\n";
  for (const auto &v : vv) {
    cerr << "\t";
    view(v);
    cerr << "\n";
  }
  cerr << "}";
}
template <typename T, typename U>
void view(const V<pair<T, U>> &v) {
  cerr << "{\n";
  for (const auto &c : v) {
    cerr << "\t(" << c.first << ", " << c.second << ")\n";
  }
  cerr << "}";
}
template <typename T, typename U>
void view(const map<T, U> &m) {
  cerr << "{\n";
  for (auto &t : m) {
    cerr << "\t[" << t.first << "] : " << t.second << "\n";
  }
  cerr << "}";
}
template <typename T, typename U>
void view(const pair<T, U> &p) {
  cerr << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
void view(const set<T> &s) {
  cerr << "{ ";
  for (auto &t : s) {
    view(t);
    cerr << ", ";
  }
  cerr << "\b\b }";
}
template <typename T>
void view(T e) {
  cerr << e;
}
int main() {
  int n;
  IN(n);
  V<int> a(n);
  IN(a);
  for (int i = (0); i < (n); ++i) (void(0));
  int now = 0, res = 0;
  for (int i = (0); i < (n); ++i)
    if (!chmax(now, a[i])) res += now - a[i];
  print(res);
}
