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