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