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