#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, a[100000000], sum = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      sum = sum + (a[i] - a[i + 1]);
      a[i + 1] = a[i];
    }
  }
  cout << sum << "\n";
}
