#include <bits/stdc++.h>
int main(void) {
  int n;
  std::cin >> n;
  std::vector<int64_t> a;
  for (int i = 0; i < n; i++) {
    int64_t num;
    std::cin >> num;
    a.push_back(num);
  }
  int sum = 0;
  for (int i = 1; i < n; i++) {
    int64_t diff = a[i] - a[i - 1];
    if (diff < 0) {
      sum += std::abs(diff);
      a[i] += std::abs(diff);
    }
  }
  std::cout << sum << std::endl;
  return 0;
}
