#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<int> a(t);
  for (int i = 0; i < t; i++) {
    cin >> a[i];
  }
  int s = 0;
  int mx = a[0];
  for (int i = 1; i < t; ++i) {
    mx = max(mx, a[i]);
    s += mx - a[i];
  }
  cout << s << endl;
  return 0;
}
