#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
long long logb(long long base, long long x) { return (log(x) / log(base)); }
long long poww(const long long &a, long long b, const long long &m = mod) {
  if (b == 0) return 1;
  long long x = poww(a, b / 2, m);
  x = x * x % m;
  if (b & 1) x = x * a % m;
  return x;
}
void TAK(vector<long long> &v) {
  for (long long i = 0; i < (long long)(v).size(); i++) cin >> v[i];
}
void TAK(long long a[], long long n) {
  for (long long i = 0; i < n; i++) cin >> a[i];
}
void SET(long long a[], long long n, long long k) {
  for (long long i = 0; i < n; i++) a[i] = k;
}
void PT(vector<long long> const &v) {
  long long n = (long long)(v).size();
  for (long long i = (0); i < (n); i++)
    cout << ("i") << " is " << (i) << " and " << ("v[i]") << " is " << (v[i])
         << "\n";
  ;
  cout << "\n";
}
void PT(long long const a[], long long n) {
  for (long long i = (0); i < (n); i++)
    cout << ("i") << " is " << (i) << " and " << ("a[i]") << " is " << (a[i])
         << "\n";
  ;
  cout << "\n";
}
const long long MAX = 2e5 + 5;
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, 1, 0, -1};
long long n = 0, i = 0, j = 0;
void solve() {
  cin >> n;
  vector<long long> v(n);
  TAK(v);
  long long ans = 0;
  for (i = 1; i < n; i++)
    if (v[i] < v[i - 1]) ans += (v[i - 1] - v[i]);
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
