#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; ++i) cin >> A[i];
  ll s = 0;
  for (int i = 1; i < n; ++i) {
    if (A[i - 1] > A[i]) s += A[i - 1] - A[i];
  }
  cout << s << endl;
}
