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