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