#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " -> " << a << endl;
  err(++it, args...);
}
void testcase() {
  int n, a[200005];
  cin >> n;
  for (int i{0}; i < n; i++) cin >> a[i];
  int ans = 0;
  for (int i{1}; i < n; i++) {
    if (a[i] < a[i - 1]) {
      ans += abs(a[i] - a[i - 1]);
      a[i] = a[i - 1];
    }
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    testcase();
  }
  return 0;
}
