#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, prev, cur, res = 0;
  cin >> n;
  cin >> prev;
  for (int i = 1; i < n; ++i) {
    cin >> cur;
    if (cur < prev) res += prev - cur;
    prev = max(prev, cur);
  }
  cout << res << "\n";
  return 0;
}
