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