#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto& a : A) {
    cin >> a;
  }
  int prev = A[0];
  int sumHeight = 0;
  for (auto i = 1; i < N; ++i) {
    if (A[i] < prev) {
      int diff = prev - A[i];
      sumHeight += diff;
    } else {
      prev = A[i];
    }
  }
  cout << sumHeight << endl;
}
