#include <bits/stdc++.h>
int main() {
  int n;
  int *data;
  int dif, count = 0;
  scanf("%d", &n);
  data = (int *)malloc(n * sizeof(int));
  for (int i = 0; i < n; i++) {
    scanf("%d", &data[i]);
    if (i != 0) {
      dif = data[i] - data[i - 1];
      if (dif < 0) {
        dif *= -1;
        count += dif;
        data[i] += dif;
      }
    }
  }
  printf("%d\n", count);
  free(data);
  return 0;
}
