#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[i];
  int mae = 0;
  int step = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] >= mae) {
      mae = A[i];
    } else {
      step += mae - A[i];
    }
  }
  cout << step << endl;
  return 0;
}
