#include <bits/stdc++.h>
using namespace std;
long long gcd(long long p, long long q) {
  if (q == 0) return p;
  return gcd(q, p % q);
}
bool ispoweroftwo(long long n) {
  if (n == 0) return false;
  return (ceil(log2(n)) == floor(log2(n)));
}
long long logarithm(long long a, long long b) {
  long long c = 0;
  while (a > 0) {
    c++;
    a = a / b;
  }
  return c;
}
vector<vector<long long> > adj;
void dfs(long long cur = 1, long long par = -1) {
  cout << cur << '\n';
  for (auto neighb : adj[cur]) {
    if (neighb == par)
      continue;
    else
      dfs(neighb, cur);
  }
}
signed main() {
  long long t = 1;
  while (t--) {
    long long n, x, op = 0, flag = 1;
    cin >> n;
    vector<long long> v(n + 1);
    for (long long i = 1; i < n + 1; i++) {
      cin >> v[i];
    }
    long long s = 1;
    long long mx = 0;
    for (long long i = 2; i < n + 1; i++) {
      if (v[i] < v[i - 1]) {
        op += v[i - 1] - v[i];
      }
    }
    cout << op << '\n';
  }
  return 0;
}
