#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, co = 0;
  cin >> n;
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long maax = a[0];
  for (int i = 1; i < n; i++) {
    maax = max(maax, a[i]);
    if (a[i] < a[i - 1]) {
      co += maax - a[i];
    }
  }
  cout << co;
  return 0;
}
