#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 b = 0;
  int c;
  for (int i = 0; i < N - 1; i++) {
    if (A[i + 1] < A[i]) {
      c = A[i] - A[i + 1];
      A[i + 1] = A[i];
      b += c;
    }
  }
  cout << b << endl;
}
