#include <bits/stdc++.h>
int main() {
  int n, b;
  scanf("%d", &n);
  long int ar[n], c = 0;
  for (b = 0; b < n; b++) scanf("%ld", &ar[b]);
  for (b = 1; b < n; b++) {
    if (ar[b] < ar[b - 1]) {
      c += ar[b] - ar[b - 1];
      ar[b - 1] = ar[b];
    }
  }
  printf("%ld", c);
}
