#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int last = A.at(0);
  int sum = 0;
  for (int i = 0; i < A.size(); i++) {
    if (last > A.at(i)) {
      sum += (last - A.at(i));
    } else {
      last = A.at(i);
    }
  }
  cout << sum << endl;
  return 0;
}
