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