#include <bits/stdc++.h>
using namespace std;
vector<long long> par, size;
void make_set(long long v) {
  par[v] = v;
  size[v] = 1;
}
long long find_set(long long u) {
  if (par[u] == u) return u;
  return par[u] = find_set(par[u]);
}
void union_sets(long long a, long long b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (size[a] < size[b]) swap(a, b);
    par[b] = a;
    size[a] += size[b];
  }
}
void init() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long &i : a) cin >> i;
  long long res = 0;
  for (long long i = 1; i < n; i++) {
    if (a[i - 1] > a[i]) {
      res += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << res << "\n";
}
int32_t main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  init();
  return 0;
}
