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