#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  int stoolsum = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (i != 0) {
      if (A[i] < A[i - 1]) {
        stoolsum += (A[i - 1] - A[i]);
        A[i] += (A[i - 1] - A[i]);
      }
    }
  }
  cout << stoolsum;
  return 0;
}
