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