#include <bits/stdc++.h>
using namespace std;
long long fast_exp(long long base, long long exp) {
  long long res = 1;
  while (exp > 0) {
    if (exp % 2 == 1) res = (res * base) % 1000000007;
    base = (base * base) % 1000000007;
    exp /= 2;
  }
  return res % 1000000007;
}
long long fib(long long n) {
  if (n < 2) return n;
  long long a = 0, b = 1, pre;
  int i = 1;
  while (i < n) {
    pre = (a + b) % 1000000007;
    a = b;
    b = pre;
    i++;
  }
  return pre;
}
long long fac(long long n) {
  if (n % 2 == 0) return 2;
  for (long long i = 3; i * i <= n; i += 2) {
    if (n % i == 0) return i;
  }
  return n;
}
long long fact(long long n) {
  if (n > 1)
    return n * fact(n - 1);
  else
    return 1;
}
bool sortbysec(const tuple<int, int, int> &a, const tuple<int, int, int> &b) {
  return (get<1>(a) < get<1>(b));
}
int last_digit(int n) {
  int ans = 0;
  while (n != 0) {
    ans = n % 10;
    n /= 10;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  int a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  ;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] >= a[i - 1]) {
      continue;
    } else {
      ans += abs(a[i] - a[i - 1]);
      a[i] = a[i - 1];
    }
  }
  cout << ans;
}
