#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, s = 0, k;
  cin >> n;
  long long m[n];
  for (i = 0; i < n; i++) {
    cin >> m[i];
  }
  k = m[n - 1];
  for (i = n - 2; i >= 0; i--) {
    if (m[i] > k) {
      s = s + m[i] - k;
    } else {
      k = m[i];
    }
  }
  cout << s << endl;
  return 0;
}
