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