#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  long long ans = 0;
  for (long long i = 0; i < N - 1; ++i) {
    if (A.at(i + 1) >= A.at(i)) {
      ans += A.at(i + 1) - A.at(i);
      A.at(i + 1) = A.at(i + 1), A.at(i);
    }
  }
  cout << ans << "\n";
}
