#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int sum, worst = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] < worst) {
      sum += worst - A[i];
      A[i] = worst;
    }
    worst = max(worst, A[i]);
  }
  cout << sum << endl;
}
