#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int last;
  cin >> last;
  int tot = 0;
  for (int i = 1; i < n; ++i) {
    int ai;
    cin >> ai;
    if (ai <= last)
      tot += last - ai;
    else
      last = ai;
  }
  cout << tot << endl;
  return 0;
}
