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