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