#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 total = 0;
  int n = A.at(0);
  for (int i = 1; i < N; i++) {
    if (n > A.at(i)) {
      total += n - A.at(i);
    }
    n = max(n, A.at(i));
  }
  cout << total << endl;
  return 0;
}
