#include <bits/stdc++.h>
using namespace std;
const long long INF64 = LLONG_MAX;
const int INF = INT_MAX;
const long double PI = acos(-1);
const long double E = 2.71828183;
const int MOD = 1e9 + 7;
template <typename T>
struct segment_tree {
  vector<T> tree;
  int size;
  T DEF;
  void init(int n) {
    size = 1;
    DEF = INF;
    while (size < n) size *= 2;
    tree = vector<T>(2 * size, 0);
  }
  T merge(T a, T b) { return a + b; }
  void set(int i, int x, T v, int lx, int rx) {
    if (rx - lx == 1) {
      tree[x] = v;
      return;
    }
    int m = (rx + lx) / 2;
    if (i < m) {
      set(i, 2 * x + 1, v, lx, m);
    } else {
      set(i, 2 * x + 2, v, m, rx);
    }
    tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
  }
  void set(int i, T v) { set(i, 0, v, 0, size); }
  T sum(int l, int r, int x, int lx, int rx) {
    if (r <= lx || rx <= l)
      return 0;
    else if (l <= lx && rx <= r)
      return tree[x];
    int m = (lx + rx) / 2;
    return merge(sum(l, r, 2 * x + 1, lx, m), sum(l, r, 2 * x + 2, m, rx));
  }
  T sum(int l, int r) { return sum(l, r + 1, 0, 0, size); }
  void print_tree() {
    for (int i = 0; i < tree.size(); i++) cout << to_string(tree[i]) << " ";
    cout << "\n";
  }
};
vector<int> bfs(vector<vector<int> > adj, int src) {
  queue<int> q;
  vector<int> d(adj.size(), INF);
  q.push(src);
  d[src] = 0;
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    for (int nbr : adj[c]) {
      if (d[nbr] == INF) q.push(nbr);
      d[nbr] = min(d[nbr], d[c] + 1);
    }
  }
  return d;
}
void read(vector<int>& v) {
  int n = v.size();
  for (int i = 0; i < n; i++) cin >> v[i];
}
long double log(long double a, long double b) { return log(b) / log(a); }
long long power(long long base, long long exp, long long M = LLONG_MAX) {
  long long res = 1;
  while (exp) {
    if (exp % 2 == 1) res = ((res % M) * (base % M)) % M;
    base = ((base % M) * (base % M)) % M;
    exp /= 2;
  }
  return res;
}
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) solve();
}
void solve() {
  int n;
  cin >> n;
  int mx = -1;
  int ans = 0;
  while (n--) {
    int x;
    cin >> x;
    if (x < mx) ans += mx - x;
    mx = max(x, mx);
  }
  {
    cout << ans << "\n";
    return;
  }
}
