#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
const int MOD = 1000000009;
const double pi = acos(-1);
const int dx[] = {0, 1, -1, 0};
const int dy[] = {-1, 0, 0, 1};
const int dxc[] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dyc[] = {-1, 0, 0, 1, -1, 1, -1, 1};
long long min(long long a, long long b) {
  if (a > b) return b;
  return a;
}
long long max(long long a, long long b) {
  if (a > b) return a;
  return b;
}
long long gcd(long long a, long long b) {
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n == 2) return true;
  if (n % 2 == 0) return false;
  for (long long i = 3; i * i <= n; i += 2) {
    if (n % i == 0) return false;
  }
  return true;
}
set<long long> gen(long long n) {
  set<long long> divs;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      divs.insert(i);
      divs.insert(n / i);
    }
  }
  return divs;
}
map<long long, int> fact(long long n) {
  map<long long, int> first;
  for (long long p = 2; p * p <= n; p++) {
    while (n % p == 0) {
      first[p]++;
      n /= p;
    }
  }
  if (n > 1) first[n]++;
  return first;
}
long long sum(long long n) { return (n * (n + 1)) / 2; }
long long binaryExp(long long a, long long b, long long c) {
  if (b == 0) return 1;
  if (b % 2 == 0) {
    long long temp = binaryExp(a, b / 2, c);
    return (temp * temp) % c;
  } else {
    long long temp = binaryExp(a, b - 1, c);
    return (temp * a) % c;
  }
}
bool isPal(string& cad) {
  int p1 = (int)(cad).size() - 1;
  for (long long i = 0; i < (int)(cad).size() / 2; i += 1) {
    if (cad[i] != cad[p1]) return false;
    p1--;
  }
  return true;
}
double dis(double x1, double y1, double x2, double y2) {
  double ff = pow(x2 - x1, 2) + pow(y2 - y1, 2);
  return sqrt(ff);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<int> vec(n);
  for (long long i = 0; i < n; i += 1) cin >> vec[i];
  int ant = vec[0];
  int res = 0;
  for (long long i = 1; i < n; i += 1) {
    if (vec[i] < ant)
      res += (ant - vec[i]);
    else
      ant = vec[i];
  }
  cout << res << "\n";
  return 0;
}
