#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;
  for (int i = 1; i<n; i++) {
    s = A.at(i-1) - A.at(i);
    if (A.at(i) < A.at(i-1)) {
      A.at(i) += s;
      sum += s;
    }
  }
  cout << sum << endl;
}