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