#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> X;
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    X.push_back(x);
  }
  int H = 0;
  for (int64_t i = 0; i < N - 1; ++i) {
    if (X.at(i) > X.at(i + 1)) {
      H += X.at(i) - X.at(i + 1);
      X.at(i + 1) = X.at(i);
    }
  }
  cout << H << endl;
}
