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