#include <bits/stdc++.h>
using namespace std;
int C(int N, vector<int>& A) {
  int cnt = 0, max_h = 0;
  for (int& x : A) {
    cnt += max(0, max_h - x);
    max_h = max(x, max_h);
  }
  return cnt;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) cin >> A[i];
  cout << C(N, A);
  return 0;
}
