#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, cnt = 0;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  long mx = 0;
  for (int i = 0; i < n; i++) {
    if (mx < v[i]) {
      mx = v[i];
      continue;
    }
    if (mx > v[i]) cnt += mx - v[i];
  }
  cout << cnt;
}
