#include <bits/stdc++.h>
using namespace std;
template <typename... T>
void read(T&... args) {
  ((cin >> args), ...);
}
template <typename T>
T get(T& a) {
  read(a);
  return a;
}
struct Converter {
  template <typename T>
  T toNum(string s, T& n) {
    stringstream ss;
    ss << s;
    ss >> n;
    return n;
  }
  template <typename T>
  string toString(T& n) {
    stringstream ss;
    ss << n;
    string s;
    ss >> s;
    return s;
  }
} convert;
struct Prime {
  bool isPrime[10000007] = {0};
  std::vector<int> primeContain;
  void sieve(int n) {
    memset(isPrime, true, sizeof(isPrime));
    for (int p = 2; p * p <= n; p++) {
      if (isPrime[p] == true) {
        for (int i = p * p; i <= n; i += p) isPrime[i] = false;
      }
    }
    isPrime[0] = 0, isPrime[1] = 0;
    for (int i = 2; 1 > 0 ? i < n + 7 : i >= n + 7; i += 1)
      if (isPrime[i]) primeContain.push_back(i);
  }
  bool primeDigit(int n) {
    while (n) {
      int t = n % 10;
      if (t != 2 && t != 3 && t != 5 && t != 7) return false;
      n /= 10;
    }
    return true;
  }
} prime;
struct Algo {
  int josephusDP[100005];
  int josephus(int n, int k) {
    if (josephusDP[n] != -1) return josephusDP[n];
    if (n == 1) return 1;
    return josephusDP[n] = (josephus(n - 1, k) + k - 1) % n + 1;
  }
} algo;
void cppCheck() {
  if (__cplusplus == 201103L)
    std::cout << "C++11\n";
  else if (__cplusplus == 199711L)
    std::cout << "C++98\n";
  else
    std::cout << "pre-standard C++\n";
}
int main() {
  int n;
  cin >> n;
  int x;
  cin >> x;
  int currentMax = x, ans = 0;
  for (int i = 0; 1 > 0 ? i < n - 1 : i >= n - 1; i += 1) {
    cin >> x;
    if (x < currentMax) {
      ans += (currentMax - x);
    }
    currentMax = max(currentMax, x);
  }
  cout << ans << endl;
  return 0;
}
