#include <bits/stdc++.h>
int main(void) {
  int n;
  scanf("%d", &n);
  unsigned int* a = (unsigned int*)malloc(sizeof(unsigned int) * n);
  int i;
  for (i = 0; i < n; i++) {
    scanf("%u", &a[i]);
  }
  unsigned int mae_height = a[0];
  unsigned int fumidai_sum = 0;
  int sa;
  for (i = 1; i < n; i++) {
    if (a[i] < mae_height) {
      sa = mae_height - a[i];
      fumidai_sum += sa;
      mae_height = a[i] + sa;
    } else if (a[i] >= mae_height) {
      mae_height = a[i];
    }
  }
  printf("%u\n", fumidai_sum);
  return 0;
}
