#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9 + 7, N1 = 1e7 + 5;
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] > a[i + 1]) {
      sum += a[i] - a[i + 1];
      a[i + 1] = a[i];
    }
  }
  cout << sum;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t1 = 1;
  while (t1--) {
    solve();
    cout << endl;
  }
}
