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