#include <bits/stdc++.h>
using namespace std;
class Utl {
 public:
  template <class mixed>
  static void print(mixed value) {
    std::cout << value;
  }
  template <class mixed, class padd>
  static void print(mixed prefix, padd suffix) {
    std::cout << prefix << suffix;
  }
  template <class mixed>
  static void print(mixed value, bool space) {
    if (space)
      std::cout << value << " ";
    else
      std::cout << value;
  }
  template <class mixed>
  static void println(mixed value) {
    std::cout << value << std::endl;
  }
  static void println() { std::cout << endl; }
  template <class mixed>
  static void log(vector<mixed> arr, bool nl = false) {
    for (int i = 0; i <= arr.size() - 1; i++) Utl::print(arr[i], " ");
    if (nl) Utl::println();
  }
  template <class mixed>
  static void log(mixed arr[], int len, bool nl = false) {
    for (int i = 0; i <= len - 1; i++) Utl::print(arr[i], " ");
    if (nl) Utl::println();
  }
  template <class mixed>
  static vector<mixed> sort(vector<mixed> vec, int dir = 0) {
    if (dir == 1)
      std::sort(vec.begin(), vec.end(), greater<mixed>());
    else
      std::sort(vec.begin(), vec.end());
    return vec;
  }
  static vector<int> sort(vector<int> vec, std::function<bool(int, int)> dir) {
    std::sort(vec.begin(), vec.end(), dir);
    return vec;
  }
  static vector<char> split(string str) {
    vector<char> vec;
    for (int i = 0; i < str.size(); i++) vec.push_back(str[i]);
    return vec;
  }
  template <class mixed>
  static mixed min(vector<mixed> vec) {
    return *min_element(vec.begin(), vec.end());
  }
  template <class mixed>
  static mixed max(vector<mixed> vec) {
    return *max_element(vec.begin(), vec.end());
  }
  template <class mixed>
  static mixed sum(vector<mixed> vec) {
    return accumulate(vec.begin(), vec.end(), 0);
  }
  template <class mixed>
  static vector<mixed> reverse(vector<mixed> vec) {
    std::reverse(vec.begin(), vec.end());
    return vec;
  }
  template <class mixed>
  static bool every(vector<mixed> vec,
                    std::function<bool(const mixed)> callback) {
    return all_of(vec.begin(), vec.end(), callback);
  }
  template <class mixed>
  static bool some(vector<mixed> vec,
                   std::function<bool(const mixed)> callback) {
    return any_of(vec.begin(), vec.end(), callback);
  }
  template <class mixed>
  static vector<mixed> map(vector<mixed> vec,
                           std::function<mixed(const mixed)> callback) {
    vector<mixed> ret;
    for (auto &el : vec) ret.push_back(callback(el));
    return ret;
  }
  template <class mixed>
  static vector<mixed> filter(vector<mixed> vec,
                              std::function<mixed(const mixed)> callback) {
    vector<mixed> ret;
    for (auto &el : vec) {
      if (callback(el)) ret.push_back(el);
    }
    return ret;
  }
  static string toString(int num) { return to_string(num); }
  static int parseInt(string num) { return stoi(num); }
};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i <= n - 1; i++) {
    cin >> vec[i];
  }
  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    if (vec[i] < vec[i - 1]) {
      ans += vec[i - 1] - vec[i];
      vec[i] = vec[i - 1];
    }
  }
  Utl::println(ans);
  return 0;
}
