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