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