#include <bits/stdc++.h>
using namespace std;
template <typename T>
T fact(T n) {
  if (n == 1)
    return 1;
  else
    return (n * fact(n - 1));
}
template <typename T>
T nCr(T n, T r) {
  if (r == 0 || r == n)
    return 1;
  else if (r == 1)
    return n;
  return (nCr(n - 1, r - 1) + nCr(n - 1, r));
}
template <typename T>
void swap(T *a, T *b) {
  T tmp;
  tmp = *a;
  *a = *b;
  *b = tmp;
  return;
}
template <typename T>
T gcd(T a, T b) {
  if (a == 0)
    return b;
  else if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <typename T>
vector<T> divisor(T n) {
  vector<T> ret;
  for (T i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
template <typename T>
vector<T> eratosthenes(T n) {
  vector<bool> is_prime(n + 2, true);
  vector<T> primes;
  for (T i = 2; i <= n; i++) {
    if (is_prime[i]) {
      for (T j = 2 * i; j <= n; j += i) is_prime[j] = false;
      primes.push_back(i);
    }
  }
  return primes;
}
int main(int argc, char const *argv[]) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n;
  vector<int> a;
  int in;
  int ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> in;
    a.push_back(in);
  }
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i]) {
      ans += (a[i - 1] - a[i]);
      a[i] += (a[i - 1] - a[i]);
    }
  }
  cout << (ans) << endl;
  return 0;
}
