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