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