#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> num(N);
  for (long long int i = 0; i < (N); i++) {
    cin >> num.at(i);
  }
  int max = num.at(0);
  int height = 0;
  for (int i = 1; i < N; i++) {
    if (num.at(i) > max) max = num.at(i);
    if (max > num.at(i)) {
      height += max - num.at(i);
      num.at(i) += max - num.at(i);
    }
  }
  cout << height << endl;
}
