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