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