#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, sum, curmax;
  cin >> N;
  vector<int> A(N);
  sum = 0;
  for (size_t i = 0; i < A.size(); i++) cin >> A[i];
  curmax = A[0];
  for (size_t i = 1; i < A.size(); i++) {
    if (A[i] < curmax) sum += curmax - A[i];
    curmax = max(A[i], curmax);
  }
  cout << sum << endl;
  return 0;
}
