#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> f(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int now = a[0];
  for (int i = 0; i < n; i++) {
    if (now > a[i]) {
      f[i] = now - a[i];
    } else if (now < a[i]) {
      now = a[i];
      f[i] = 0;
    } else {
      f[i] = 0;
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt = cnt + f[i];
  }
  cout << cnt << endl;
  return 0;
}
