#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  std::cin >> N;
  std::vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    std::cin >> A.at(i);
  }
  int d = 0;
  int n;
  for (long long i = 1; i < N; i++) {
    if (A.at(i - 1) > A.at(i)) {
      n = (A.at(i - 1) - A.at(i));
      d += n;
      A.at(i) += n;
    }
  }
  std::cout << d << '\n';
  return 0;
}
