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