#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto i = 0; i < n; i++) cin >> a[i];
  int acc = 0;
  int min = 0;
  for (const auto num : a) {
    if (min > num) {
      acc += min - num;
    } else {
      min = num;
    }
  }
  cout << acc << endl;
  return 0;
}
