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