#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, i, j, k, l;
  cin >> n;
  vector<int> a;
  for (int i = (0); i < (n); i++) {
    cin >> k;
    a.push_back(k);
  }
  l = a[0];
  int z = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < l) {
      z += abs(l - a[i]);
    } else
      l = a[i];
  }
  cout << z << "\n";
}
int main() {
  auto start = clock();
  ios_base::sync_with_stdio(0), cin.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  auto stop = clock();
  cerr << "time taken:" << double(stop - start) / double(CLOCKS_PER_SEC) << " s"
       << "\n";
  return 0;
}
