#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  for (int64_t& a : A) {
    cin >> a;
  }
  int64_t ans = 0;
  for (int64_t i = 1; i < N; i++) {
    int64_t step = max(A[i] - A[i - 1], (int64_t)0);
    ans += step;
    A[i] += step;
  }
  cout << ans << endl;
}
