#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int count = 0;
  int ki = vec.at(0);
  for (int i = 0; i < N; i++) {
    if (ki < vec.at(i)) {
      ki = vec.at(i);
      continue;
    } else if (ki > vec.at(i)) {
      count += ki - vec.at(i);
      continue;
    }
  }
  cout << count << endl;
}
