#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
void dump(T e) {
  std::cout << e << std::endl;
}
template <typename T>
void dump(const std::vector<T>& v) {
  for (const auto& e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T>
void dump(const std::vector<std::vector<T> >& vv) {
  for (const auto& v : vv) {
    dump(v);
  }
}
template <typename T>
void dump_stack(std::stack<T>& stack) {
  std::stack<T> temp;
  while (!stack.empty()) {
    T top = stack.top();
    stack.pop();
    std::cout << top << " ";
    temp.push(top);
  }
  std::cout << endl;
  while (!temp.empty()) {
    T top = temp.top();
    temp.pop();
    stack.push(top);
  }
}
template <typename T>
void dump_queue(std::queue<T>& queue) {
  std::queue<T> temp;
  while (!queue.empty()) {
    T top = queue.front();
    queue.pop();
    std::cout << top << " ";
    temp.push(top);
  }
  std::cout << endl;
  while (!temp.empty()) {
    T top = temp.front();
    temp.pop();
    queue.push(top);
  }
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
  vector<int> ans;
  int before = 0;
  for (int(i) = 0; (i) < (a.size()); (i)++) {
    if (i == 0) continue;
    if (a[i - 1] > a[i]) {
      ans.push_back(a[i - 1] - a[i]);
      a[i] += a[i - 1] - a[i];
    }
  }
  int r = 0;
  bool b = ans.empty();
  if (b == false) {
    for (int(i) = 0; (i) < (ans.size()); (i)++) r += ans[i];
  }
  cout << r << endl;
}
