#include <bits/stdc++.h>
using namespace std;
const long long fix = (long long)1e9 + 7;
long long po2(long long a) {
  double temp = a;
  if (log2(temp) == (long long)log2(temp)) return log2(temp);
  return -1;
}
void tc(long long i) { cout << "Case #" << i + 1 << ": "; }
void yes() { cout << "YES" << '\n'; }
void no() { cout << "NO" << '\n'; }
void impb() { cout << "IMPOSSIBLE" << '\n'; }
long long ct() {
  long long t;
  scanf("%lld", &t);
  return t;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long pwr(long long a, long long b) {
  long long x = 1, y = a;
  while (b > 0) {
    if (b % 2) x = (x * y) % fix;
    y = (y * y) % fix;
    b /= 2;
  }
  return x;
}
long long pmodinv(long long a) { return pwr(a, fix - 2); }
vector<vector<long long> > adj;
const long long N = 1e5 + 5;
bool visited[N] = {};
queue<long long> q;
long long comanc(long long u, long long v, long long parent[]) {
  while (u ^ v) {
    if (u == 1 || v == 1) return 1;
    u = parent[u];
    v = parent[v];
  }
  return u;
}
long long x;
long long max(long long a, long long b) {
  if (a > b) return a;
  return b;
}
vector<long long> factorial((long long)4002);
long long nCr(long long n, long long k) {
  return (factorial[n] *
          ((pmodinv(factorial[k]) * pmodinv(factorial[n - k])) % fix)) %
         fix;
}
long long dsu_par[N];
long long sze[N];
void make_set(long long n) {
  for (long long j = (1); j < (n + 1); ++j) {
    dsu_par[j] = j;
    sze[j] = 1;
  }
}
long long find_set(long long a) {
  if (dsu_par[a] == a) return a;
  return dsu_par[a] = find_set(dsu_par[a]);
}
void union_set(long long a, long long b) {
  a = find_set(a);
  b = find_set(b);
  if (a ^ b) {
    if (sze[a] < sze[b]) swap(a, b);
    dsu_par[b] = a;
    sze[a] += sze[b];
  }
}
map<pair<long long, long long>, long long> mp;
vector<vector<long long> > up;
vector<long long> tin, tout;
long long dfs_timer = 0;
long long lim;
long long dist[N] = {};
void _dfs(long long v, long long par) {
  tin[v] = ++dfs_timer;
  up[v][0] = par;
  for (long long j = (1); j < (lim + 1); ++j)
    up[v][j] = up[up[v][j - 1]][j - 1];
  for (long long u : adj[v]) {
    if (u ^ par) {
      dist[u] = dist[v] + mp.find(pair<long long, long long>(u, v))->second;
      _dfs(u, v);
    }
  }
  tout[v] = ++dfs_timer;
}
bool is_anc(long long u, long long v) {
  return tin[u] <= tin[v] && tout[u] >= tout[v];
}
long long lca(long long u, long long v) {
  if (is_anc(u, v)) return u;
  if (is_anc(v, u)) return v;
  for (long long i = lim; i >= 0; i--)
    if (is_anc(up[u][i], v) ^ 1) u = up[u][i];
  return up[u][0];
}
long long min(long long a, long long b) {
  if (a < b) return a;
  return b;
}
long long nn;
long long bisearch(long long w, long long h, long long limit) {
  long long lo = 1, hi = limit;
  while (lo < hi) {
    long long mid = lo + (hi - lo) / 2;
    if ((mid) / max(w, h) >= nn ||
        log2((long double)(mid / h)) + log2((long double)(mid / w)) >=
            log2((long double)(nn)))
      hi = mid;
    else
      lo = mid + 1;
  }
  return lo;
}
long long p, f;
long long S, W;
long long s, w;
long long ans(long long para) {}
int32_t main() {
  long long t = 1;
  for (long long ii = (0); ii < (t); ++ii) {
    long long ans, ma;
    long long n;
    cin >> n;
    cin >> ma;
    long long rec;
    for (long long j = (1); j < (n); ++j) {
      cin >> rec;
      if (rec < ma) {
        ans += ma - rec;
      } else {
        ma = rec;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
