#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, bool>;
int main() {
  int n;
  std::cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < (n); ++i) std::cin >> a[i];
  int ma = a[0];
  int sum = 0;
  for (int i = 1; i < n; i++) {
    if (ma > a[i]) {
      sum += ma - a[i];
    } else
      ma = a[i];
  }
  std::cout << sum << '\n';
  return 0;
}
