#include <bits/stdc++.h>
int nextint(void) {
  int n;
  scanf("%d", &n);
  return n;
}
void printint(int d) { printf("%d\n", d); }
int main(void) {
  int n = nextint();
  int sum = 0;
  long long a[210000];
  a[0] = nextint();
  for (int i = 1; i < n; i++) {
    a[i] = nextint();
    if (a[i - 1] > a[i]) {
      sum += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  printint(sum);
  return 0;
}
