#include <bits/stdc++.h>
using namespace std;
template <typename T>
void view(T e) {
  std::cout << e << std::endl;
}
template <typename T>
void view(const std::vector<T>& v) {
  for (const auto& e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T> >& vv) {
  for (const auto& v : vv) {
    view(v);
  }
}
template <typename T, typename U>
void view(const std::map<T, U>& m) {
  for (const auto& e : m) {
    std::cout << e.first << "=>" << e.second << " ";
  }
  std::cout << std::endl;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (long long i = 0; i < (long long)(N); i++) {
    cin >> A[i];
  }
  int total = 0;
  int min = 0;
  for (long long i = 0; i < (long long)(N); i++) {
    if (A[i] > min) {
      min = A[i];
    } else {
      total += min - A[i];
    }
  }
  cout << total << endl;
  return 0;
}
