#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N;
  cin >> N;
  vector<long long int> A(N);
  for (auto i : A) cin >> A[i];
  long long int ans = 0;
  for (long long int i = 1; i < N; i++) {
    if (A[i] < A[i - 1]) {
      ans += A[i - 1] - A[i];
      A[i] = A[i - 1];
    }
  }
  cout << ans << endl;
}
