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