#include <bits/stdc++.h>
const long long mod = 1000000007;
using namespace std;
using namespace std::chrono;
int32_t main() {
  auto start = high_resolution_clock::now();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    long long sum = 0;
    for (long long i = n - 1; i >= 1; i--) {
      long long p = a[i] - a[i - 1];
      if (p > 0) {
        sum += p;
      }
    }
    cout << sum << "\n";
    ;
  }
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  cerr << "Time taken by function: " << duration.count() << " milliseconds"
       << "\n";
  ;
  return 0;
}
