#include <bits/stdc++.h>
using namespace std;
template <typename T>
void print(T x) {
  std::cout << x << " ";
}
template <typename T>
void println(T x) {
  std::cout << x << "\n";
}
signed main(void) {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < (n); i++) {
    cin >> a[i];
  }
  long long ans = 0;
  long long maxheight = a[0];
  for (long long i = 1; i < (n); i++) {
    cout << "maxheight= " << maxheight << endl;
    cout << "a[i]= " << a[i] << endl;
    if (maxheight > a[i]) {
      ans += maxheight - a[i];
    } else {
      maxheight = a[i];
    }
  }
  print(ans);
}
