#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
string binary(int n) {
  string bin;
  while (n) {
    bin += (n % 2 ? '1' : '0');
    n /= 2;
  }
  reverse(bin.begin(), bin.end());
  return bin;
}
bool isPalindrome(string s) {
  for (int i = 0, j = s.size() - 1; i < j; i++, j--)
    if (s[i] != s[j]) return 0;
  return 1;
}
bool isPrime(long long n) {
  if (n <= 1) return false;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
inline vector<int> sieve(int n) {
  vector<bool> isPrime(n + 1, true);
  for (int i = 2; i <= sqrt(n); i++) {
    if (isPrime[i])
      for (int j = 2 * i; j <= n; j += i) {
        isPrime[j] = false;
      }
  }
  vector<int> primes;
  for (int i = 2; i <= n; i++)
    if (isPrime[i]) primes.push_back(i);
  return primes;
}
inline vector<int> primeFactors(int n) {
  vector<int> factors;
  vector<int> primes = sieve(100000);
  int i = 0;
  while (n != 1 && i < primes.size()) {
    if (n % primes[i] == 0) {
      factors.push_back(primes[i]);
      n /= primes[i];
    } else {
      i++;
    }
  }
  return factors;
}
void shift_right(vector<int>& A, int x) {
  int n = A.size();
  x %= n;
  vector<int> B(n);
  for (int i = 0; i < n; i++) B[(i + x) % n] = A[i];
  A = B;
}
inline vector<bool> sieveQuery(int n) {
  vector<bool> isPrime(n + 1, true);
  for (int i = 2; i <= sqrt(n); i++) {
    if (isPrime[i])
      for (int j = 2 * i; j <= n; j += i) {
        isPrime[j] = false;
      }
  }
  return isPrime;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) cin >> A[i];
  int mx = A[0];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (A[i] < mx) ans += mx - A[i];
    mx = max(mx, A[i]);
  }
  cout << ans;
}
