#include <bits/stdc++.h>
int main() {
  int n = 0;
  std::cin >> n;
  std::vector<int> array(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> array.at(i);
  }
  int prev = array[0];
  int sum = 0;
  for (unsigned int i = 1; i < array.size(); ++i) {
    int dif = prev - array[i];
    if (dif > 0) {
      sum += dif;
    } else {
      prev = array[i];
    }
  }
  std::cout << sum;
  return 0;
}
