#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &e : A) cin >> e;
  int64_t ans = 0, now = A[0];
  for (int i = 1; i < N; ++i) {
    int64_t dif = now - A[i];
    if (dif > 0) {
      ans += dif;
      now = A[i - 1];
    } else {
      now = A[i];
    }
  }
  cout << ans << '\n';
  return 0;
}
