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