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