#include <bits/stdc++.h>
int main(void) {
  int n;
  unsigned long long ans;
  std::cin >> n;
  std::vector<long long> a(n);
  for (int i = 0; i < n; i++) std::cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      ans += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  std::cout << ans;
  return 0;
}
