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