#include <bits/stdc++.h>
int main(int argc, char *argv[]) {
  int n;
  std::cin >> n;
  std::vector<int> xs(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> xs[i];
  }
  int h = xs[0];
  int sum = 0;
  for (int i = 1; i < n; ++i) {
    if (xs[i] < h) {
      sum += h - xs[i];
    } else {
      h = xs[i];
    }
  }
  std::cout << sum << std::endl;
  return 0;
}
